3

Suppose I have big numbers such as this: 10.295.419.305.791.578.917.358.173.519.057.891.283.897.623.986

library(gmp)
as.bigz(10295419305791578917358173519057891283897623986)

And this might outputs:

Big Integer ('bigz') :
[1] 10295419305791579051260295299235939155628261376

Visually the value 10.295.419.305.791.579.051.260.295.299.235.939.155.628.261.376 is already different compared the input (begins after 15 digits from beginning)

My question: are there any explanation to this? is it because of numeric error before it being converted to big integer?

While I still find a way to get the accurate value by adding quotes.. such as:

as.bigz("10295419305791578917358173519057891283897623986")
Big Integer ('bigz') :
[1] 10295419305791578917358173519057891283897623986
Jovan
  • 763
  • 7
  • 26
  • Yes, R typically reads numbers as double precision floating point numbers, so when you run `as.bigz(######)` it's like you're running `as.bigz(convert_to_floating_point(######))` -- so you will already lose precision. In contrast, a character string in R can be as long as you want and will not be altered en route to your function. – Jon Spring Dec 29 '22 at 08:00
  • `options(scipen = 50); 10295419305791578917358173519057891283897623986` Note how just inputing a very large number will often output a different number starting in the ~15th decimal place due to the floating point conversion. – Jon Spring Dec 29 '22 at 08:03
  • I see.. So every number either its numeric and integer small or big that we input in console, will always take a floating point conversion process first? @JonSpring – Jovan Dec 29 '22 at 08:10
  • 4
    Right -- base R doesn't have a conception of "numbers" that are more precise than 53 bits; it will work with numbers using their IEEE 754 double-precision floating-point representation: https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f As you noted, if you want to feed a more precise number into a function, it has to be as a character string, which can be effectively unlimited in precision. – Jon Spring Dec 29 '22 at 08:19
  • Put the input number between quotes: `gmp::as.bigz("10295419305791578917358173519057891283897623986")`. – Rui Barradas Dec 29 '22 at 10:03
  • 1
    This type of question has come up in various forms many times. See https://stackoverflow.com/q/51490022/4408538 https://stackoverflow.com/q/68050209/4408538 https://stackoverflow.com/q/67951564/4408538 https://stackoverflow.com/q/65434297/4408538 to name a few – Joseph Wood Dec 30 '22 at 23:07

0 Answers0