i*****y 发帖数: 188 | 1 Hi,
I was wondering if someone could help me with the following problem I
encountered...
> a<-502.167
> b<-526.167
> c=478.167
> b-a==24
[1] FALSE
> a-c==24
[1] TRUE
I do not understand why I got "FALSE" for b-a==24 while "TRUE" for a-c==24?
Thanks a lot. |
k*******a 发帖数: 772 | 2 这个好像没啥好办法, 见R-FAQ
7.31 Why doesn't R think these numbers are equal?
The only numbers that can be represented exactly in R's numeric type are
integers and fractions whose denominator is a power of 2. Other numbers have
to be rounded to (typically) 53 binary digits accuracy. As a result, two
floating point numbers will not reliably be equal unless they have been
computed by the same algorithm, and not always even then. For example
R> a <- sqrt(2)
R> a * a == 2
[1] FALSE
R> a * a - 2
[1] 4.440892e-16
The function all.equal() compares two objects using a numeric tolerance of .
Machine$double.eps ^ 0.5. If you want much greater accuracy than this you
will need to consider error propagation carefully. |
i*****y 发帖数: 188 | 3 Hi,
Thank you so much. It is really helpful. |
e****z 发帖数: 119 | 4 I guess it is because R uses a different data type (maybe long int) to store
a value above a cutoff. Based on your example, the cutoff is mostly likely
to be 512. |
c*****m 发帖数: 4817 | 5 浮点数精度问题,任何语言都有
【在 i*****y 的大作中提到】 : Hi, : Thank you so much. It is really helpful.
|