How to get the correct answer for 111111111 * 111111111 in R?

Viewed 1463

I was showing my daughter the magic pattern of two 111..111 number multiplication in R, e.g.

> options(scipen=999)
> 1^2
[1] 1
> 11^2
[1] 121
> 111^2
[1] 12321
> 1111^2
[1] 1234321
> 11111^2
[1] 123454321
> 111111^2
[1] 12345654321
> 1111111^2
[1] 1234567654321
> 11111111^2
[1] 123456787654321
> 111111111^2
[1] 12345678987654320

Everything works perfectly until it goes to 9 digits. As you can tell from the last line, the answer is obviously wrong; it should be 1234567898765432*1*, not 1234567898765432*0*.

I am running this in R (v3.6.1, x86_64-apple-darwin15.6.0, 64-bit) on a Macbook Pro laptop.

Someone said that it might be caused by an integer overflow. Two questions here:

  1. Can anyone explain how this happened exactly? e.g. how was the last 1 changed to 0?

  2. How can I get the correct answer in R?

Thanks.

1 Answers

My personal favorite for this kind of problem is package Rmpfr, an R package for the GNU MPFR Library.

library(Rmpfr)

## 53 bit precision
ones9 <- mpfr(1111111111, precBits = 53)

ones9^2
#1 'mpfr' number of precision  53   bits 
#[1] 1234567900987654400


## 100 bit precision
ones9b <- mpfr(1111111111, precBits = 100)
ones9b^2
#1 'mpfr' number of precision  100   bits 
#[1] 1234567900987654321
Related