Full Calculations for Large Numbers

I am working with the R programming language.

I noticed the following:

 > 3^83
[1] 3990838394187339732048206468684268226400


> 2^51
[1] 2251799813685248

The problem happens when I try to subtract these two numbers:

> 3^83 - 2^51
[1] 3990838394187339732048206468684268226400

As we can see, the subtraction of these two numbers results in a number that is equal to the bigger number - this is impossible!

Can someone please show me why this is happening and what can I do to correct this error so that the subtraction is correct?

Thanks!

> sessionInfo()

R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

> options()$digits
[1] 7

Note : Is it possible that the GMP library (CRAN - Package gmp) might be able to fix this problem?

library(gmp)

 num = as.bigz(3)

 a=num^96
 #a
# Big Integer ('bigz') :
# [1] 6362685441135942358474828762538534230890216321

 num1=as.bigz(2)
 b=3*(num1^96)
 # b
#  Big Integer ('bigz') :
# [1] 237684487542793012780631851008

 a-b

#  Big Integer ('bigz') :
# [1] 6362685441135942120790341219745521450258365313

Computers have limited precision. In this case though, I think you have an issue with how the output is being displayed. I get

> 3^83 - 2^51
[1] 3.990838e+39

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.