[R] Rounding problem R vs Excel

Duncan Murdoch murdoch at stats.uwo.ca
Tue Jun 3 15:32:11 CEST 2003


On Tue, 3 Jun 2003 09:49:44 +0100, you wrote in message
<00b701c329ad$14ef6460$ad002850 at FSSFQCV7BGDVED>:

>Duncan
>If the numbers are not represently exactly how does R resolve problems like
>the one below? Is there something that needs to be set up in the R
>environment like the number of significant figures?
>
>> x<-4.145*100+0.5
>> x
>[1] 415
>> floor(x)
>[1] 414

R doesn't do anything to resolve this problem; it's just the way the
IEEE standard floating point formats work.  In Excel 97, 4.145*100+0.5
is exactly equal to 415; I would guess this is either because they use
a binary coded decimal format instead of the IEEE floating point
types, or they round results internally in some way.  R doesn't
support BCD formats, and doesn't do tricky rounding behind your back.
You get what you ask for.

If you want the calculation above to give you exactly 415, the
standard workaround in languages without BCD formats is to work in
some decimal multiple of the actual numbers you're interested in, e.g.
10000.  Then you would store 4.145 as 41450, multiply by 1000000 (i.e.
100*10000) and divide by 10000 to give 4145000, and add 5000, to give
4150000.  All of these numbers are exactly representable in double
precision floating point types, because they are all integers with
fewer than 53 bits in their binary representations.  

Doing this means you need to change the definitions of *, /, ^, and
lots of other low level functions, but + and - work in the usual way.
It might be an interesting project to write a package that does all of
this.

Duncan Murdoch




More information about the R-help mailing list