Integer Nan

Further to Xojo Can't Compare

curious about something on TOF (Programming Humor 3 - #33 by Rick_A - Off-Topic - Xojo Programming Forum) so I went looking to see what Xojo does

Suppose you do something silly & end up with Nan in a double

Dim d As Double = Sqrt(-1) // d holds NaN

Dim i As Integer = d // assigning NaN to an integer ?


no exception
no error
just a silent assignment of -9223372036854775808 to i in this case

If instead you divide by 0 and generate Inf

Dim d As Double = 1/0 // d holds Inf

Dim i As Integer = d // assigning Inf to an integer ?


again no exception
no error
just silently assigns -8818512963818684373 to i

still think there are operations that should fail & fail loudly - like this


Exactly, as other languages do. Only languages where types as Ints aren’t scalars, but objects/structs, can carry alternative values as Null, Nan and Inf in their structural value behind the scenes. If you can’t keep the value, you must report the error, not silently “transform its value” to something else.

Are those numbers “expected”?
Where do they come from, “mathematically”?

I suspect from taking the bit representation of a Nan, as a double, and converting it into an integer

What are these bits?

Not really, -9223372036854775808 is just the lower limit of a signed int64 type so… No mistery.

This behavior is not exclusive to Xojo, other languajes do the same.

1 Like

Any garbage float, non zero, with the top 9 bits set is a NaN (if the payload value is zero, then it represents a -Infinity)). So any non-zero float as Int32 or 0xFF800000 is a NaN. As you can notice, a lot of higher value bits is set, whatever NaN value gets resulted by any calculation error setting those bits and ignoring the rest, it could be translated to a huge value. For 64bit doubles it’s 12 bits instead of 9. So if you set the top 13 bits as 1, you guarantee a NaN.

Yes, after getting an exception in some impossible calculation, ignoring the error, you get a NaN that you could do some trick to use that NaN as a value in an int resulting in a large value (maybe random depending on the platform) that you can’t trust.

IEEE 754 specifies them
Not sure wikipedia lists them
I suspect this one might be superceded

Great and interesting explanations here, thanks!

Thank you for confirming my assumptions. Don’t know if liking will have the same tremendous impact on a ticket like spending points, but in case: