Problems with NaN (not a number)

  1. 5 years ago

    Ramon S

    8 May 2014 Pre-Release Testers, Xojo Pro UPC, Europe (Barcelona, Spain)
    Edited 5 years ago

    Although we can read in LR:

    NaN (i.e. "Not a Number"): occurs if you attempted to perform an illegal mathematical operation, like getting the square root of a negative number. Any further calculation made with a NaN will lead to a NaN value. Str or Format methods return a string beginning with "NaN", e.g. "NaN(021)".

    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,

  2. Norman P

    8 May 2014 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

    The fun part is there are several kinds of Nan's and Inf's :P
    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)

  3. Ramon S

    8 May 2014 Pre-Release Testers, Xojo Pro UPC, Europe (Barcelona, Spain)
    Edited 5 years ago

    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)

  4. Norman P

    8 May 2014 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...
    Edited 5 years ago

    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

  5. Ramon S

    8 May 2014 Pre-Release Testers, Xojo Pro UPC, Europe (Barcelona, Spain)

    Thanks Norman.

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

  6. Harrie W

    8 May 2014 Crossville, Tennessee, USA

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

  7. Kem T

    8 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    @Norman P 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

    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]+)?$
  8. Norman P

    8 May 2014 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...
    Edited 5 years ago

    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 :)

    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")

  9. Kem T

    8 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut
    Edited 5 years ago

    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.

  10. Norman P

    8 May 2014 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

    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)

  11. Kem T

    8 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  12. Kem T

    8 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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
  13. Norman P

    8 May 2014 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

    @Kem T 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

    And the regex should make "NaN" , "-1.#IND" and "Inf" fail

  14. Kem T

    8 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  15. Thomas E

    9 May 2014 Pre-Release Testers

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

    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

    Comments?

  16. Ramon S

    9 May 2014 Pre-Release Testers, Xojo Pro UPC, Europe (Barcelona, Spain)

    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?

  17. Christian S

    9 May 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

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

  18. Thomas E

    9 May 2014 Pre-Release Testers

    @Ramon S 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

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

    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.

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

  19. Ramon S

    9 May 2014 Pre-Release Testers, Xojo Pro UPC, Europe (Barcelona, Spain)
    Edited 5 years ago

    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?)

    -image-

    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

  20. 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.

  21. Newer ›

or Sign Up to reply!