Hello,

I am having a hard time to write a simple function that determines if a Double value is Int64:

function IsInt64(const aNumber: Double): Boolean;

I know about precision problems. I basically want to know if Round(aNumber) overflows.

In Delphi, Round() doesn't throw an exception if the value exceeds Int64 range, so it is safe to compare SameValue(aNumber, Round(aNumber)):

function OXmlIsInt64(const aNumber: Double): Boolean;
begin
    Result := not IsNan(aNumber) and not IsInfinite(aNumber) and SameValue(Frac(aNumber), 0) and SameValue(aNumber, Round(aNumber));
end;

That's OK for me.

But in FPC, I get:
Project TestProject raised exception class 'External: FLT INVALID OPERATION'
in Round().

Is there a chance to not throw this exception? I expect this problem so I don't want to do something like:

function OXmlIsInt64(const aNumber: Double): Boolean;
begin
  try
    Result := not IsNan(aNumber) and not IsInfinite(aNumber) and SameValue(Frac(aNumber), 0) and SameValue(aNumber, Round(aNumber));
  except
    Result := False;
  end;
end;

I tried to switch overflow checks off:
{$OVERFLOWCHECKS OFF}

But obviously it doesn't help (it's a different exception).

----

I'd really like to get rid of the try-except block... It's the same with StrToInt() vs TryStrToInt(). IMO there should be a possibility to catch expected errors without exceptions.

Any ideas?

Thanks
Ondrej

_______________________________________________
fpc-devel maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to