Constant NaNs returning strange result

Archives Forums/BlitzMax Bug Reports/Constant NaNs returning strange result

Hamish(Posted 2015) [#1]
I use NaNs a fair bit to return an invalid result in floats and I'm getting some strange results. I've narrowed the problem down to constants, as NaNs calculated with normal variables still work correctly.

'NaN calcuated using a variable returns '-1.#IND0000' as expected
Zero# = 0.0
DebugLog(Zero/Zero)

'assigns to another variable correctly
Nan# = Zero/Zero
DebugLog(Nan)

'constant NaN prints as 'nan' instead of '-1.#IND0000'
DebugLog(0.0/0.0)
Const ConstNan# = 0.0/0.0
DebugLog(ConstNan)

'assigning this constant NaN to a variable sets it to 0 instead of NaN
Nan# = 0.0/0.0
DebugLog(Nan)
Nan# = ConstNan
DebugLog(Nan)


I'm not sure if this is a problem with only on my system as I've only been able to test it here. I can still use variables for NaNs but it's annoying as I like to use a 'Nan' constant for clarity. BlitzMax version 1.50


Floyd(Posted 2015) [#2]
This is actually about debug and release builds doing different things.

Defining a Const as NaN is really abusing the system. NaN means literally not a number. It isn't any specific thing. In practical terms it means the 32-bits holding the alleged floating point value do not in fact hold a valid number.

Did you intend to use ConstNan to compare with floating point values to determine if they are NaN? In that case you should use the IsNan() function. There is also IsInf() to check for infinities.

Here is an example showing the different debug/release handling of ConstNan. But the attempt to create ConstNan probably should not be allowed. In fact Debug mode doesn't, although the result is zero instead of an error. I think the fact that it still works in release mode is for the sake of legacy code, written before IsNan() existed.

ConstNaN# = 0.0 / 0.0

z# = 0
FloatNaN# = z/z

t$ = "This is RELEASE mode."

?debug
t$ = "This is DEBUG mode."
?


Print
Print "Try both DEBUG and RELEASE."
Print
Print t
Print
Print "Display ConstNaN and FloatNaN"
Print

Print ConstNaN
Print FloatNaN
Print

DebugLog ConstNaN
DebugLog FloatNaN

Print 
Print "What does IsNan() say about them?"
Print

Print IsNan( ConstNan )
Print IsNan( FloatNaN )



Hamish(Posted 2015) [#3]
Thanks for the reply.
I use IsNan to check if it's NaN or not, I just like to assign it to a const when I need to return it eg

Method GetValue#()
	If ValueIsValid Then Return Value Else Return Nan
End Method


It seemed like a clever way to return an invalid result as a float, in the same way String.Find() returns 0 and up for a valid index and -1 for no result. It saves a fair bit of typing in some places where I would otherwise need two methods, one to check for validity and one to actually get the value.