From Andre@G||||bert @end|ng |rom chu-rouen@|r  Sun Jul 10 16:28:27 2022
From: Andre@G||||bert @end|ng |rom chu-rouen@|r (GILLIBERT, Andre)
Date: Sun, 10 Jul 2022 14:28:27 +0000
Subject: [Rd] Floating point issue
In-Reply-To: <25290.56745.942106.634125@rob.eddelbuettel.com>
References: <CAEKh8uhNTF3RczavV-jh82tK9e=iDr2tzys=wGSk6ZD5e2tndg@mail.gmail.com>,
 <25290.56745.942106.634125@rob.eddelbuettel.com>
Message-ID: <cb4678b2d846400194e23034c6cf7674@chu-rouen.fr>

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


The fact that not all values are representable by floating point does not mean that outputing a number with maximum accuracy, then reading it back, should yield a different number.


I would like to point that I cannot reproduce this "bug" on the official R 4.2.0 Windows x86_64 build on an AMD Ryzen 1700 on Windows 10.


> 1e25
[1] 10000000000000000906640224
> 1e25 == 10000000000000000906640224
[1] TRUE
>

I could also test:
> as.double(as.character(1e25)) == 1e25
[1] TRUE

For ECMAScript, there is a rule that guarantee that converting a floating point value to a string, and converting back to a floating point value, yields to an equal number.
https://262.ecma-international.org/5.1/#sec-9.8.1

"If x is any Number value other than ?0, then ToNumber<https://262.ecma-international.org/5.1/#sec-9.3>(ToString<https://262.ecma-international.org/5.1/#sec-9.8>(x)) is exactly the same Number value as x."


This avoids some data loss when repeatedly writing/reading floating point values to a text file.


Of course, R is a different language, and I do not think that it has such rule. However, this does not mean that this rule is useless or impossible to implement.


--

Sincerely

Andr? GILLIBERT

________________________________
De : R-devel <r-devel-bounces at r-project.org> de la part de Dirk Eddelbuettel <edd at debian.org>
Envoy? : dimanche 10 juillet 2022 16:09:45
? : Antoine Fabri
Cc : R-devel
Objet : Re: [Rd] Floating point issue

ATTENTION: Cet e-mail provient d?une adresse mail ext?rieure au CHU de Rouen. Ne cliquez pas sur les liens ou n'ouvrez pas les pi?ces jointes ? moins de conna?tre l'exp?diteur et de savoir que le contenu est s?r. En cas de doute, transf?rer le mail ? ? DSI, S?curit? ? pour analyse. Merci de votre vigilance


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

______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


	[[alternative HTML version deleted]]



[Rd] Floating point issue
GILLIBERT, Andre