[Rd] Floating point issue

GILLIBERT, Andre Andre@G||||bert @end|ng |rom chu-rouen@|r
Sun Jul 10 16:28:27 CEST 2022


> 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 using r-project.org> de la part de Dirk Eddelbuettel <edd using 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 using debian.org

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


	[[alternative HTML version deleted]]



More information about the R-devel mailing list