Skip to content
Prev 29089 / 63424 Next

significant digits (PR#9682)

simon.urbanek at r-project.org wrote:
But your multiplication isn't accurate:  10000 is 16 times 625.  
Multiplying by 16 will shift the binary representation by 4 places, but 
multiplying by an odd number should leave the last 1 bit in place.  If 
you go through the following series of operations, you get a different 
answer than multiplying by 10000:

 > x <- 0.12345
 > x <- 16*x
 > y <- x
 > x <- 624*x
 > x + y - 1234.5
[1] 2.273737e-13

versus

 > x <- 0.12345
 > x <- 10000*x
 > x - 1234.5
[1] 0

I get identical results for both of these calculations on a Mac and on 
Windows.  Looks strange, but I think the explanation is that multiplying 
by 625 (or 10000) needs one more bit than multiplying by 624 needs, so 
we lose a bit at that point.  Windows does the multiplication by 10000 
accurately in its 64 bit registers, and then loses the last bit on 
storing the result into x in 53 bits.  But in the signif() calculation, 
everything stays in 64 bit precision, so Windows gets the right result 
(0.1235), while those systems that round intermediate results get it 
wrong (0.1234).

It's all FP calculations, but in Windows we've programmed the run-time 
to keep extra bits around for intermediate results and get more accurate 
results in cases where things stay in registers, whereas the other 
systems throw away the bits and get more consistent results regardless 
of the execution path. 

Duncan Murdoch