Recent

Author Topic: val return code  (Read 2477 times)

dhatlestad

  • Newbie
  • Posts: 2
val return code
« on: May 21, 2025, 08:41:24 pm »
I am not sure what the return code from val() is supposed to be. If I try to convert a number in val() that is too large for a floating point Double, say 1e310, I do not get an error and the return code is 0. The converted Double is '+inf' in the debugger. Seems reasonable to convert to positive infinite for an overflow but it seems like throwing an overflow exception would be better. The return code from val() does not appear to have an expected meaning or value. Maybe the {$R+} directive would help? The trunc() function does not like the value 1e42, of course, because it is too large for an int64 value, that is, larger than 9,223,372,036,854,775,807. The trunc() function throws an exception, as expected, when the value is too large. Perhaps it would be better to check in your code to see if the converted real number is less than the maximum for an int64.

Tested on Lazarus version 4.0; FPC version 3.2.2; win64.

dsiders

  • Hero Member
  • *****
  • Posts: 1440
Re: val return code
« Reply #1 on: May 21, 2025, 10:43:31 pm »
I am not sure what the return code from val() is supposed to be. If I try to convert a number in val() that is too large for a floating point Double, say 1e310, I do not get an error and the return code is 0. The converted Double is '+inf' in the debugger. Seems reasonable to convert to positive infinite for an overflow but it seems like throwing an overflow exception would be better. The return code from val() does not appear to have an expected meaning or value. Maybe the {$R+} directive would help? The trunc() function does not like the value 1e42, of course, because it is too large for an int64 value, that is, larger than 9,223,372,036,854,775,807. The trunc() function throws an exception, as expected, when the value is too large. Perhaps it would be better to check in your code to see if the converted real number is less than the maximum for an int64.

Tested on Lazarus version 4.0; FPC version 3.2.2; win64.

This has nothing to do with Release 4.0 of the IDE. This is a discussion about RTL.

Start another thread.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: val return code
« Reply #2 on: May 22, 2025, 12:39:50 pm »
I splitted it off

Thaddy

  • Hero Member
  • *****
  • Posts: 17196
  • Ceterum censeo Trump esse delendam
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

dhatlestad

  • Newbie
  • Posts: 2
Re: val return code
« Reply #4 on: May 22, 2025, 07:18:57 pm »
Thank you for splitting this topic to another thread. For context, my post about the val() function is in reference to https://forum.lazarus.freepascal.org/index.php/topic,71050.msg555506.html#msg555506. Perhaps that should have been the first post in this new thread.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6004
  • Compiler Developer
Re: val return code
« Reply #5 on: May 22, 2025, 08:46:06 pm »
The return code from val() does not appear to have an expected meaning or value.

No, Val does not use exceptions. And for floating point it's perfectly valid to convert it to Infinity if the value is too large (same as values that aren't exactly representable will just be converted to the next best floating point number). The result code is for parsing errors.

 

TinyPortal © 2005-2018