Problems with NaN (not a number)

  1. ‹ Older
  2. 6 years ago

    Ramon S

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

    Thanks Syed.
    Let's wait for any clarification from Xojo's behalf.

  3. Kem T

    9 May 2014 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    To recap, this is the function I recommend:

    Function IsTrueNumeric (value As String) As Boolean
      static rx as RegEx
      if rx is nil then
        rx = new RegEx
        rx.SearchPattern = "(?i)\A[\d\-+e\.]+\z"
      end if
      return rx.Search( value ) <> nil and IsNumeric( value )
    End Function
  4. Norman P

    9 May 2014 Xojo Inc, Xojo Pro Seeking work. npalardy@great-w...

    Str without a format string does at the very lowest level use an OS supplied routine for turning the value into a string

  5. Ramon S

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

    Thanks Kem and Norman,

    But the truth is the deeper I go in this subject the weirder it is.
    I've just tried this code:

     dim a as double = sqrt(-1)
      dim at1 as string = format (a, "-0.00e")
      dim at2 as string = str(a)
      Dim b as Boolean = IsNumeric(a)
      Dim c as Boolean = IsNumeric(at1)
      Dim d as Boolean = IsNumeric(at2)

    I get this:

    -image-

    So it seems that IsNumeric detects non numbers when stringed but not when in numerical format.
    May be I can use this, but I'd like to know what happens in OSX and Linux.
    Can anyone check it?
    TIA

  6. Kem T

    9 May 2014 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    On OS X:

    -image-

  7. Kem T

    9 May 2014 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    FYI, this returns True:

      dim e as Boolean = IsNumeric( "NAN" )
  8. The code below results in the listed values on SuSE SLES 11 Linux in Xojo2014R1.1.

      Dim a As Double = Sqrt(-1)
      Dim at1 As String = Format (a, "-0.00e")
      Dim at2 As String = Str(a)
      Dim b As Boolean = IsNumeric(a)
      Dim c As Boolean = IsNumeric(at1)
      Dim d As Boolean = IsNumeric(at2)
    
    a       NaN.
    at1     NAN.00
    at2     NaN
    b       True
    c       False
    d       True
  9. Ramon S

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

    Thanks Kem and Syed,

    With your information I have enough to understand the problem.
    I definitely will ask for a feature to do have such a function in Xojo.
    Meanwhile I'll do something myself.

  10. 5 years ago

    Michael D

    4 Nov 2015 Testers, Xojo Pro

    Is there a way to create a NaN constant?

    I recently ran into some OS X routines which expect CGRectNull constant, which is not a Nil pointer but rather a CGRect that is filled with {NaN,NaN,NaN,NaN}. I know MBS has a function to make it, but I was curious how to create that in pure Xojo code.

  11. Tim H

    4 Nov 2015 Portland, OR USA

    NaN is a particular 8-byte representation of a Double. Use an 8-byte memoryblock, set the appropriate byte values, and pull the DoubleValue out of it. (Or use 4 bytes and SingleValue if it expects a 4-byte representation.)

  12. Norman P

    4 Nov 2015 Xojo Inc, Xojo Pro Seeking work. npalardy@great-w...
    Edited 5 years ago

    NaN can be several different representations
    https://en.wikipedia.org/wiki/NaN

  13. Michael D

    4 Nov 2015 Testers, Xojo Pro

    Interesting : this page https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html gives the hex values, and for a Single it looks like it's 7fc00000, for a double it's 7ff80000 00000000.

    Does Ctype work here? Can one say
    dim s as single = Ctype("NaN") ?

  14. Will S

    4 Nov 2015 Santa Cruz 
    Edited 5 years ago

    @Michael D expect CGRectNull constant

    From what I've found CGRectNull is [Inf, Inf, 0, 0].

    You can use CGRectIntersection with 2 disjoint rects and that's what I get back.

  15. Norman P

    4 Nov 2015 Xojo Inc, Xojo Pro Seeking work. npalardy@great-w...

    IEEE 754 NaNs are represented with the exponent field filled with ones (like infinity values), and some non-zero number in the significand (to make them distinct from infinity values); this representation allows the definition of multiple distinct NaN values, depending on which bits are set in the significand, but also on the value of the leading sign bit (not all applications are required to provide distinct semantics for those distinct NaN values).

    There can be MANY different NaN's :)

  16. Michel B

    4 Nov 2015 Testers, Xojo Pro

    @Norman P NaN can be several different representations
    https://en.wikipedia.org/wiki/NaN

    Very nice.

  17. Michael D

    4 Nov 2015 Testers, Xojo Pro

    @Will S From what I've found CGRectNull is [Inf, Inf, 0, 0].
    You can use CGRectIntersection with 2 disjoint rects and that's what I get back.

    Here's what I've found:

      const CGPoint CGPointZero = {0,0};
      const CGSize CGSizeZero = {0,0};
      const CGRect CGRectZero = {{0,0},{0,0}};
      const CGRect CGRectNull = {{NAN,NAN},{NAN,NAN}};
      const CGRect CGRectInfinite = {{INFINITY,INFINITY},{INFINITY,INFINITY}};
  18. Will S

    4 Nov 2015 Santa Cruz 
    Edited 5 years ago

    @Michael D Here's what I've found:

    const CGRect CGRectNull = {{NAN,NAN},{NAN,NAN}};

    Did you try CGRectIntersection? The docs say it returns CGRectNull and when I stringify such a result I get Inf, Inf, 0, 0

  19. Will S

    5 Nov 2015 Santa Cruz 
    Edited 5 years ago

    INF can be made like this, for Single

    m = new MemoryBlock(4)
    m.UInt32Value(0) = &h7F800000
    v = m.SingleValue(0)

    for Double

    m = new MemoryBlock(8)
    m.UInt64Value(0) = &h7FF0000000000000
    v = m.DoubleValue(0)

    but also v = 1 / 0 works. So this should make a CGRectNull in both 32 and 64 bit.

    dim r As CGRect
    r.x = 1 / 0
    r.y = 1 / 0
    r.w = 0
    r.h = 0
  20. Michael D

    5 Nov 2015 Testers, Xojo Pro
    Edited 5 years ago

    Will, it seems you may be correct?

      dim r as CGRectMBS = CGRectMBS.Null
      dim mb as MemoryBlock = r.Binary
      break

    In the debugger, mb has the bytes

    0000 807F 0000 807F 0000 0000 0000 0000

    I wonder if this is a documentation bug or something else? Perhaps the OS just looks at the 2 first Singles and if they are Inf it doesn't matter what the last two are?

  21. Will S

    5 Nov 2015 Santa Cruz 

    @Michael D I wonder if this is a documentation bug or something else?

    I couldn't find a definition in Apples docs or Xcode and turned to tweets like this
    https://twitter.com/MSch/status/271528930035507201
    Docs like this define a CGRectNull with NaNs but it's the Free Software Foundation, Inc. not Apple
    http://svn.gna.org/svn/gnustep/libs/opal/trunk/Source/OpalGraphics/CGGeometry.m

    Also it seems only one of x y needs to be INF to be considered a CGRectNull...

      dim r As CGRect
      r.x = 9
      r.y = 1 / 0
      r.w = 6
      r.h = 2
      
      declare function CGRectIsNull lib "Cocoa" (r As CGRect) As Boolean
      
      MsgBox "null " + Str(CGRectIsNull(r))     //True
  22. Newer ›

or Sign Up to reply!