[R] Rounding?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jun 12 07:13:37 CEST 2007


On Mon, 11 Jun 2007, jim holtman wrote:

> your number 6.6500000000000001 is to large to fit in a floating point
> number.  It takes 56 bits and there are only 54 in a real number so the
> system see it as 6.65 and does the rounding to an even digit; 6.6

I'll take it you mean a IEC60559 double, which has 53 bits in its implied 
mantissa (it stores 52 and for normalized numbers the leading bit is 1 
and not stored).

> 6.650000000000001 does fit into a real number (takes 54 bits) and this will
> now round to 6.7

All you can say quickly is that its representation is greater than 
6.65:

> 6.650000000000001 - 6.65
[1] 8.881784e-16

But I don't think that is the explanation.  Remember that you are using 
binary arithmetic, so each of these numbers is stored with representation 
error.  As the exact number stored is not '6.65', round-to-even does not 
strictly apply.

I get

> formatC(6.65,format="f",digits=1)
[1] "6.7"
> print(6.65, digits=2)
[1] 6.7
> print(66.5, digits=1) # round to even really does apply
[1] 66
> print(67.5, digits=1)
[1] 68

on (several of) my non-Windows systems, so I think this is a Windows 
quirk.  Remember the parsing and printing software has also to run in 
binary on a limited-precision machine, and it does tend to be less 
accurate on Windows than on other ix86 OSes.  (Windows software also tends 
not to implement round-to-even rules.)


> On 6/11/07, BXC (Bendix Carstensen) <bxc at steno.dk> wrote:
>>
>> I was a bit puzzed by:
>>
>>> formatC(6.65,format="f",digits=1)
>> [1] "6.6"
>>
>> So I experimented and found:
>>
>>> formatC(6.6500000000000001,format="f",digits=1)
>> [1] "6.6"
>>> formatC(6.650000000000001,format="f",digits=1)
>> [1] "6.7"
>>>   round(6.6500000000000001,1)
>> [1] 6.7
>>>   round(6.650000000000001,1)
>> [1] 6.7
>>> version
>>               _
>> platform       i386-pc-mingw32
>> arch           i386
>> os             mingw32
>> system         i386, mingw32
>> status
>> major          2
>> minor          5.0
>> year           2007
>> month          04
>> day            23
>> svn rev        41293
>> language       R
>> version.string R version 2.5.0 (2007-04-23)
>>
>> My machine runs Windows NT.
>>
>> Is this intended or just a Windows facility?
>> ______________________________________________
>>
>> Bendix Carstensen
>> Senior Statistician
>>
>> Steno Diabetes Center
>> Niels Steensens Vej 2-4
>> DK-2820 Gentofte
>> Denmark
>> +45 44 43 87 38 (direct)
>> +45 30 75 87 38 (mobile)
>> +45 44 43 73 13 (fax)
>> bxc at steno.dk   http://www.biostat.ku.dk/~bxc
>>
>> This e-mail (including any attachments) is intended for the ...{{dropped}}
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> 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.
>>
>
>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list