huge fractions from string to number

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?

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

[quote=405375:@Sascha S]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[/quote]

Yes. It fails with UInt64

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

With Xojo 2018r2…

[quote=405377:@Emile Schwarz]
Did you read: http://documentation.xojo.com/index.php/CDBL (and the Read Also part ?)
Or:
http://developer.xojo.com/xojo-math ?

With Xojo 2018r2…[/quote]

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

Hmm… in a 64Bit App a Double (or CGFloat in 32Bit Apps on macOS) 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

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

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

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

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

[quote=405387:@Emile Schwarz]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).[/quote]
It is 7:30 AM here right now

Dim d As Double d = Double.FromText("156348578434374084375")
results in:

d = 156348578434374074368

[quote=405381:@Sascha S]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")[/quote]

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.

[quote=405390:@Alberto De 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.

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

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

[code]// 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)[/code]

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.

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)