[Rd] floor and ceiling can't handle more than 15 decimal pla
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Sun Feb 12 16:37:57 CET 2006
On 12-Feb-06 benphalan at gmail.com wrote:
> Full_Name: Ben Phalan
> Version: 2.2.1
> OS: Win XP
> Submission from: (NULL) (131.111.111.231)
>
>
> I have noticed that floor returns the wrong number when there are more
> than 15
> decimal places:
>
>> floor(6.999999999999999)
> [1] 6
>> floor(6.9999999999999999)
> [1] 7
>
> There is a similar problem with ceiling, so this may apply to all/most
> rounding functions?
>
>> ceiling (2.000000000000001)
> [1] 3
>> ceiling (2.0000000000000001)
> [1] 2
This is not a problem (nor a bug) with 'floor' or 'ceiling'.
The "problem" (in quotes because the real problem is the user's)
is in R, intrinsic to the finite-length floating-point arithmetic
which is used. See:
> 6.999999999999999 - 7
[1] -8.881784e-16
> 6.9999999999999999 - 7
[1] 0
> 2.000000000000001 - 2
[1] 8.881784e-16
> 2.0000000000000001 - 2
[1] 0
so, in fact, R cannot "see" the 16th decimal place when you enter
a number to that precision -- it is simply lost. Exactly the same
"problem" would arise at some point whatever the finite precision
to which a floating-point number is stored. The effect is not
confined to functions 'floor' and 'ceiling' or any similar
"rounding" functions. It applies to all functions; it is simply
more obvious with the rounding functions.
Enter
.Machine
and the first two items in the output are:
$double.eps
[1] 2.220446e-16
$double.neg.eps
[1] 1.110223e-16
showing that the smallest difference which can be "seen" by R
is greater than 1-^(-16).
So, when you type it in, you *think* you have entered
2.0000000000000001
into R, but you have not. So the user has to face the problem of
how to cope with the finite-length representation in any situation
where the distinction between 2 and 2.0000000000000001 really
matters.
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Feb-06 Time: 15:37:53
------------------------------ XFMail ------------------------------
More information about the R-devel
mailing list