huge fractions from string to number

  1. 9 weeks ago

    Aaron H

    Sep 13 Pre-Release Testers Germany

    I'd like to read in an enormous fraction like this one from a text file as a string ...

    156348578434374084375/147573952589676412928

    ... and get a float from it. If I try to parse this as two integers and do the math, there is an overflow and I get a NaN or division by zero error. I know it's possible to use libraries like Bob Delaney's fp plugin. My question is whether there is there any way to do it in pure Xojo. Someone told me they can do this in C, and I told them I don't believe them, because in order to get a value out of the string, the following has to happen:

    1. convert the string to numbers
    2. do the math

    ... and doing the math simply fails. What am I missing?

  2. Sascha S

    Sep 13 Pre-Release Testers, Xojo Pro Germany/W'haven
    Edited 9 weeks ago

    I assume you use standard Integer Type? Have you tried it with Int64?

    Oh, and i ask because of http://developer.xojo.com/integer-size-specific

  3. Aaron H

    Sep 13 Pre-Release Testers Germany

    @SaschaSchneppmueller I assume you use standard Integer Type? Have you tried it with Int64?

    Oh, and i ask because of http://developer.xojo.com/integer-size-specific

    Yes. It fails with UInt64

  4. Emile S

    Sep 13 Europe (France, Strasbourg)

    Did you read: http://docs.xojo.com/index.php/CDBL (and the Read Also part ?)
    Or:
    http://developer.xojo.com/xojo-math ?

    With Xojo 2018r2…

  5. Aaron H

    Sep 13 Pre-Release Testers Germany
    Edited 9 weeks ago

    @Emile S Did you read: http://docs.xojo.com/index.php/CDBL (and the Read Also part ?)
    Or:
    http://developer.xojo.com/xojo-math ?

    With Xojo 2018r2…

    Yes, I've read it. Also I am using the latest Xojo 2018r2

  6. Sascha S

    Sep 13 Pre-Release Testers, Xojo Pro Germany/W'haven
    Edited 9 weeks ago

    Hmm.... in a 64Bit App a Double (or CGFloat in 32Bit Apps on macOS) should be able to work with such big numbers, or?

  7. Aaron H

    Sep 13 Pre-Release Testers Germany
    Edited 9 weeks ago

    @SaschaSchneppmueller Hmm.... a Double should be able to work with such big numbers, or?

    I thought so too, but I get the same exceptions. I mean, to begin with UInt64 is supposed to be able to handle those integers, but somehow it doesn't work. ... Wait, no 156,348,578,434,374,084,375 is beyond 18,446,744,073,709,551,615 so that's why it fails with UInt64

  8. Sascha S

    Sep 13 Pre-Release Testers, Xojo Pro Germany/W'haven

    I can't test it currently, but the following (from my limited understanding) has to work:

    Dim d As Double d = Double.FromText("156348578434374084375") / Double.FromText("147573952589676412928")

  9. Emile S

    Sep 13 Europe (France, Strasbourg)

    Wait a bit more until Xojo engineers read your question.

    Only German people are here ?
    (I am not, but I live at less than 3Km from the German border, and read my name ! … and I had a lunch at Kaufland today…)

  10. Dave S

    Sep 13 San Diego, California USA

    See if you can find common prime numbers that can be used to reduce each value to manageable levels
    you might not be able to do direct operations, but doing a prime integer division should be easy enough to code
    [2, 3, 5] for example (if the last digit is even, you can divide by 2, if the sum of the digits can be divided by 3 so can the whole thing)

    just an idea... up to you to figure how to implement it

  11. Emile S

    Sep 13 Europe (France, Strasbourg)

    Hey Dave, Isn’t it early in the morning in California now ?
    (There are so many years since the last time I phoned to Cupertino at 16:20 French summer time… I forget the California time).

  12. Dave S

    Sep 13 San Diego, California USA

    @Emile S Hey Dave, Isn’t it early in the morning in California now ?
    (There are so many years since the last time I phoned to Cupertino at 16:20 French summer time… I forget the California time).

    It is 7:30 AM here right now

  13. Alberto D

    Sep 13 Pre-Release Testers, Xojo Pro
    Dim d As Double
    d = Double.FromText("156348578434374084375")

    results in:

    d = 156348578434374074368
  14. Aaron H

    Sep 13 Pre-Release Testers Germany
    Edited 9 weeks ago

    @SaschaSchneppmueller I can't test it currently, but the following (from my limited understanding) has to work:

    Dim d As Double d = Double.FromText("156348578434374084375") / Double.FromText("147573952589676412928")

    That does work! It appears to be .FromText that I was missing. <-- EDIT: but it doesn't actually work because the values are internally wrong.

  15. Aaron H

    Sep 13 Pre-Release Testers Germany

    @Alberto D;Poo Dim d As Double d = Double.FromText("156348578434374084375")
    results in:
    d = 156348578434374074368

    Yes, that is what I was afraid of. So I wasn't wrong! It actually does not work. It only *looks as if* it works.

  16. Dave S

    Sep 13 San Diego, California USA

    Double accuracy is never "certain".... and definitaly not beyond 16 digits

  17. Aaron H

    Sep 13 Pre-Release Testers Germany
    Edited 9 weeks ago

    @Dave S Double accuracy is never "certain".... and definitaly not beyond 16 digits

    Yes, in this case 3 of the last 5 digits are wrong.

    1563485784343740 ... 84375
    1563485784343740 ... 74368

  18. Alberto D

    Sep 13 Pre-Release Testers, Xojo Pro

    Because it is beyond 16 digits, even:
    Double.FromText("156348578434374074350")
    reports as
    156348578434374074368

  19. Aaron H

    Sep 13 Pre-Release Testers Germany
    Edited 9 weeks ago
    // fails
    dim x as double = 156348578434374084375 / 147573952589676412928 // too big as int literals
    MsgBox str(x)
    
    // fails
    dim uin, uid as UInt64
    uin = 156348578434374084375 // too big for Uint64
    uid = 147573952589676412928 // too big for Uint64
    x = uin / uid
    MsgBox str(x)
    
    // appears to work
    x = Double.FromText("156348578434374084375") / Double.FromText("147573952589676412928")
    MsgBox str(x)
    
    // also appears to work
    dim n, d as double
    n = val( "156348578434374084375" )
    d = val( "147573952589676412928" )
    x = n / d
    MsgBox str(x)

    So, the answer appears to be that pure Xojo CAN do this -- not that well, but just as well as any other language without using a big number library.

  20. Dave S

    Sep 13 San Diego, California USA
    Edited 9 weeks ago

    those may "appear" to work.. but the answers are probably wrong.

    I would propose that

    dim s as string= "156348578434374084375" 
    if s<>str(val(s)) then msgbox "Double is not accurate"

    your examples fail, as they all go to SciNot format (1.5eXX)

  21. Newer ›

or Sign Up to reply!