开发者

float numerical error. How does c++ know 0.99999982 is 1?

开发者 https://www.devze.com 2023-04-06 20:08 出处:网络
I normalized a vector and length of it should be 1. But the result of length method is 0.99999982 I don\'t know it\'s right or wrong. But if I print it out, the result is 1. Not 0.99999982( printed b

I normalized a vector and length of it should be 1. But the result of length method is 0.99999982

I don't know it's right or wrong. But if I print it out, the result is 1. Not 0.99999982( printed by cout )

But how std::cout knows it's 1? [This is my first question]

And another question is why the result of comparing function is false.

I have a comparing me开发者_运维百科thod like below. And lhs is the length of a vector and the rhs is just 1.

return (fabsf(rhs-lhs) <= FLT_EPSILON) ? true : false;

Result of this method is false.

Is length of the normalized vector is already wrong to be considered normalized? or epsilon is too small?

What did I wrong?


The epsilon is too small. That's because epsilon is the smallest as it can be. (by definition)

So you will need a larger tolerance.


The IEEE standard requires that the result of addition, subtraction, multiplication and division be exactly rounded. The result must then be rounded to the nearest floating-point number. Floating point error is accumulated in the many operators it takes to normalize a vector. Like the other poster said FLT_EPSILON is too small.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号