From edd @end|ng |rom deb|@n@org  Sun Jul 10 16:09:45 2022
From: edd @end|ng |rom deb|@n@org (Dirk Eddelbuettel)
Date: Sun, 10 Jul 2022 09:09:45 -0500
Subject: [Rd] Floating point issue
In-Reply-To: <CAEKh8uhNTF3RczavV-jh82tK9e=iDr2tzys=wGSk6ZD5e2tndg@mail.gmail.com>
References: <CAEKh8uhNTF3RczavV-jh82tK9e=iDr2tzys=wGSk6ZD5e2tndg@mail.gmail.com>
Message-ID: <25290.56745.942106.634125@rob.eddelbuettel.com>


On 10 July 2022 at 16:00, Antoine Fabri wrote:
| Dear r-devel,
| 
| For some numbers, the printed value is not equivalent to the input :
| 
| options(scipen = 999)
| ## GOOD
| 1e24
| #> [1]  999999999999999983222784
| 1e24 == 999999999999999983222784
| #> [1] TRUE
| 
| ## BAD
| 1e25
| #> [1] 10000000000000000905969664
| 1e25 == 10000000000000000905969664
| #> [1] FALSE
| 
| ## STILL BAD
| 10000000000000000905969664
| #> [1] 10000000000000003053453312
| 
| ## GOOD AGAIN
| 10000000000000003053453312
| #> [1] 10000000000000003053453312
| 
| # Additionally
| 10000000000000000000000000 == 1e25
| #> [1] FALSE
| 
| Are these bugs ?

No, that is how computers work (with floating point numbers).

Please R FAQ 7.31 "Why doesn?t R think these numbers are equal?" at
  https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
and the references therein for more.

Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org



[Rd] Floating point issue
Dirk Eddelbuettel