Problems with NaN (not a number)

Although we can read in LR:

This is no true. There is an error, since if I write this code:

msgbox str(Sqrt(-5))

I get

-1.#IND

This error is not very important unless you pretend to detect NaN numbers in your program. Both Nan and Infinity use “#” when converting to string.
I’ve created a function like this:

Function IsNotANumber (x as double) as Boolean if Str(x).InStr("#") = 0 Then Return False else Return True end function
This works very well. But, can I trust Xojo will keep this conversion to string for ever?
Have you created your own function for this? How do you do it?
Should I use “IsNumeric”?
Thanks in advance,

The fun part is there are several kinds of Nan’s and Inf’s :stuck_out_tongue:
http://en.wikipedia.org/wiki/NaN
http://en.wikipedia.org/wiki/IEEE_floating-point (+? and ??)

And this check will fail on OS X where
str(1/0) -> returns “inf”
str(log(-1)) -> returns “NaN”

Are you on Windows ? (which may give different string results)

Yes, I’m in Windows but the program pretends to be Mac & Windows.
And I know that this is not a simple thing.
That’s why I ask for help, because I fear that my function may fail very easily.

Norman, do you suggest anything about it?
Do you think “IsNumeric” will work in all cases?
(My context is solving big numerical matrices, where sometimes results are NaN)

No
NaN is actually numeric - go figure - so IsNumeric(“NaN”) returns true

If what you want to test for is “it contains a value that is represented with digits (maybe a decimal point and a leading +/-)” thats something slightly different

believe it or not a regex like

[\+\-]?[0-9]+(\.[0-9])?(E[\+\-]?[0-9]+(\.[0-9])?)?

(which I’m sure Kem will revise for me) should identify “valid numbers” as far as Xojo understands them

Thanks Norman.

I will redo my function and I’ll check it in Windows and OS X.

Doesn’t NaN only work with floating point, (Single, Double) values in Xojo.

[quote=86919:@Norman Palardy]believe it or not a regex like

[\+\-]?[0-9]+(\.[0-9])?(E[\+\-]?[0-9]+(\.[0-9])?)?

(which I’m sure Kem will revise for me) should identify “valid numbers” as far as Xojo understands them[/quote]

That pattern will match something like:

1e1.44

I’ve never seen that form and the IDE rejects it as syntax error. I’d also want anchors to prevent partial matches, so I propose this instead:

(?i)^[+-]?[0-9]+(\\.[0-9]*)?(E[+-]?[0-9]+)?$

Whether you’ve seen it really doesn’t matter to the compiler or IDE
We don’t have a “check with Kem” syntax check module :slight_smile:

I would however check what VAL allows which is not quite the same as what you can type in the IDE
It definitely allows
dim d as double = val(“1.0e2.0”)

Well, you did ask, and we’re hashing it out, right?

Yes, that’s allowed as string, not as a literal, but the decimal is truncated just as any other character is removed. These lines result in exactly the same value:

d = Val( "1e1" )
d = Val( "1e1.0" )
d = Val( "1e1.9" )
d = Val( "1e1x" )
d = Val( "10y" )
// d = 10.0 in all cases

If the goal is to validate a string as a number that would be acceptable as a literal, I recommend the pattern as posted. If the goal is to validate something that Val will accept, you don’t need a pattern at all since it will return a value with almost anything, if not anything.

Its not though
It to figure out what Str might return since it MIGHT return NaN etc and IsNumeric treats NaN as numeric (since it is its just not represented as digits)

IsNumeric( "1e1.0" ) returns false.

How about something like this then:

dim rx as new RegEx
rx.SearchPattern = "(?i)\\A[\\d\\-+e\\.]+\\z"
return rx.Search( s ) <> nil and IsNumeric( s )

This will check to make sure the string only contains numbers, +, -, ., or e in any order, and, if so, runs it through IsNumeric. This seems the safest course if you just want to eliminate things like “NaN” and “INF”.

BTW, I just realized that my first pattern would not validate “.1” as a number. Bad.

I just took another whack at a complete RegEx and come up with this pattern. When I compare various strings to IsNumeric, the results match, pass or fail.

(?mi-Us)\\A[+-]?(?:\\.\\d+|\\d+(?:\\.\\d*)?)(?:e[+-]?\\d+)?\\z

My test values as output by my code:

1 passes
+1 passes
-1 passes
1. passes
+1. passes
-1. passes
.1 passes
+.1 passes
-.1 passes
1.1 passes
+1.1 passes
-1.1 passes
1e1 passes
+1e1 passes
-1e1 passes
1.e1 passes
+1.e1 passes
-1.e1 passes
.1e1 passes
+.1e1 passes
-.1e1 passes
1.1e1 passes
+1.1e1 passes
-1.1e1 passes
-1.1e+1 passes
-1.1e-1 passes
 fails RegEx
 fails IsNumeric
  fails RegEx
  fails IsNumeric
1.1e1.1 fails RegEx
1.1e1.1 fails IsNumeric
..1 fails RegEx
..1 fails IsNumeric
1..0 fails RegEx
1..0 fails IsNumeric
+-1.0 fails RegEx
+-1.0 fails IsNumeric
1.1e+-4 fails RegEx
1.1e+-4 fails IsNumeric

[quote=87032:@Kem Tekinay]I just took another whack at a complete RegEx and come up with this pattern. When I compare various strings to IsNumeric, the results match, pass or fail.

(?mi-Us)\\A[+-]?(?:\\.\\d+|\\d+(?:\\.\\d*)?)(?:e[+-]?\\d+)?\\z

[/quote]
And the regex should make “NaN” , “-1.#IND” and “Inf” fail

That latest iteration would fail those because of the anchors \A (start of document) and \z (end of document).

I still prefer using the RegEx to check for exclusive characters, then running it through IsNumeric if that’s satisfied. It keeps from recreating IsNumeric and would adapt if some future version of Xojo decided that “1.” was no longer valid.

Is it really needed to use such complicated RegEx stuff? How about a simpler way like this one:

[code]Function isNAN(x as Double) As Boolean
'NAN
if x<>x then return false

'INF
dim s as string = str(x)
if s=“INF” or s="-INF" then return false

return true
End Function
[/code]

Comments?

Thanks Norman and Kem for your dissertation on RegEx.
The truth is that my problem is not so complex.
All the numbers I want to check have been produced by Xojo, so there is no possibility to have strange combinations, except if Xojo produces them itself.

What Thomas says should work some times but not always.
Just have a look at what Xojo produces in my PC (Windows 7).

5/0 >> 1.#INF00e+
-5/0 >> -1.#INF00e+
sqrt(-1) >> -1.#IND
log(-1) >> -1.#IND
ACos(-4) >> 1.#QNAN

I didn’t know, but according to what Norman says it is different in Windows and OSX.
Who produces this output? Xojo or the OS?

I think there should be a simple way to know if a result (not a text) is a real number or just one of these strange outputs.
Should I post a feature request for it?

if you use MBS Plugins, you can check NaN Status with IsNANMBS function.
Also we have IsFiniteMBS and IsInfMBS.

[quote=87120:@Ramon SASTRE]Just have a look at what Xojo produces in my PC (Windows 7).

5/0 >> 1.#INF00e+
-5/0 >> -1.#INF00e+
sqrt(-1) >> -1.#IND
log(-1) >> -1.#IND
[/quote]

Well, I’m on a Mac but the documentation clearly states:

[quote]Infinity: some calculations lead to an infinite result (positive or negative), e.g. Log( 0 ), or you may exceed the maximum value which can be hold. In such a case, a double will be set to a special value, whose Str will return “INF” (for INFinity) or “-INF” (negative INFinity). Any further calculation will lead to a NaN or infinity value.
[/quote]

In case it work different on Windows it seems to be a bug.

Thomas,

That’s what I pointed at the beginning of this thread: What I get is not what LR says (at least on Windows).
But it is still worse. If you debug your program you don’t see that this is not a number.
Look a the image: a = 5 / 0, that is an infinity, but you see 1. Only if you try to string it you notice the problem.
So the problem is not the LR, but the string you get in Windows, that seems is different from the one you get in OSX (and Linux?)

Who generates this string?
If it is Xojo, then it should be the same in any OS, since Xojo is cross-platform. If it is the OS, then it should be said in the LR, warning about the different possible values you can get.

I would appreciate that someone from Xojo tells me which is the answer to the previous question, so that I can write a function that suits my problem and it is cross-platform

The same code on SuSE SLES 11 Linux in Xojo2014R1.1 shows the following.

a inf. at inf

It seems unlikely that these values are supplied by the OS.