Richard,

I think it was fairly clear I was explaining how people do arithmetic manually 
and often truncate or round to some number of decimal places. I said nothing 
about what R does or what the IEEE standards say and I do not particularly care 
when making MY point.

My point is that humans before computers also had trouble writing down any 
decimals that continue indefinitely. It cannot be expected computer versions of 
arithmetic can do much better. Different people can opt to do the calculation 
with the same or different numbers of digits ad when compared to each other 
they may not match.

I do care what it does in my programs, of course. My goal here was to explain 
to someone that the anomaly found was not really an anomaly and that careful 
coding may be required in these situations.


-----Original Message-----
From: Richard M. Heiberger <r...@temple.edu>
To: Avi Gross <avigr...@verizon.net>
Cc: Nathan Boeger <nboe...@gmail.com>; r-help@r-project.org 
<r-help@r-project.org>
Sent: Tue, Feb 1, 2022 2:44 pm
Subject: Re: [External] [R] Funky calculations


RShowDoc('FAQ') 


then search for 7.31


This statement
"If you stop at a 5 or 7 or 8 and back up to the previous digit, you round up. 
Else you leave the previous result alone."
is not quite right.  The recommendation in IEEE 754, and this is how R does 
arithmetic, is to Round Even.

I ilustrate here with decimal, even though R and other programs use binary.

> x <- c(1.4, 1.5, 1.6, 2.4, 2.5, 2.6, 3.4, 3.5, 3.6, 4.4, 4.5, 4.6)
> r <- round(x)
> cbind(x, r)
        x r
 [1,] 1.4 1
 [2,] 1.5 2
 [3,] 1.6 2
 [4,] 2.4 2
 [5,] 2.5 2
 [6,] 2.6 3
 [7,] 3.4 3
 [8,] 3.5 4
 [9,] 3.6 4
[10,] 4.4 4
[11,] 4.5 4
[12,] 4.6 5
> 

Numbers whose last digit is not 5 (when in decimal) round to the nearest 
integer.
Numbers who last digit is 5 (1.5, 2.5, 3.5, 4.5 above) 
round to the nearest EVEN integer.
Hence 1.5 and 3.5 round up to the even numbers 2 and 4.
2.5 and 4.5 round down do the even numbers 2 and 4.

This way the round ups and downs average out to 0.  If we always went up from 
.5 we would have
an updrift over time.

For even more detail click on the link in FAQ 7.31 to my appendix
https:// link.springer.com/content/pdf/bbm%3A978-1-4939-2122-5%2F1.pdf
and search for "Appendix G".

Section G.5 explains Round to Even.
Sections G.6 onward illustrate specific examples, such as the one that started 
this email thread.

Rich
 

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to