开发者

Floating point again

开发者 https://www.devze.com 2022-12-11 07:13 出处:网络
Yesterday I asked a floating point question, and I have another one. I a开发者_如何学Pythonm doing some computations where I use the results of the math.h (C language) sine, cosine and tangent functio

Yesterday I asked a floating point question, and I have another one. I a开发者_如何学Pythonm doing some computations where I use the results of the math.h (C language) sine, cosine and tangent functions.

One of the developers muttered that you have to be careful of the return values of these functions and I should not make assumptions on the return values of the gcc math functions. I am not trying to start a discussion but I really want to know what I need to watch out for when doing computations with the standard math functions.

x


You should not assume that the values returned will be consistent to high degrees of precision between different compiler/stdlib versions.

That's about it.


You should not expect sin(PI/6) to be equal to cos(PI/3), for example. Nor should you expect asin(sin(x)) to be equal to x, even if x is in the domain for sin. They will be close, but might not be equal.


Floating point is straightforward. Just always remember that there is an uncertainty component to all floating point operations and functions. It is usually modelled as being random, even though it usually isn't, but if you treat it as random, you'll succeed in understanding your own code. For instance:

a=a/3*3;

This should be treated as if it was:

a=(a/3+error1)*3+error2;

If you want an estimate of the size of the errors, you need to dig into each operation/function to find out. Different compilers, parameter choice etc. will yield different values. For instance, 0.09-0.089999 on a system with 5 digits precision will yield an error somewhere between -0.000001 and 0.000001. this error is comparable in size with the actual result.

If you want to learn how to do floating point as precise as posible, then it's a study by it's own.


The problem isn't with the standard math functions, so much as the nature of floating point arithmetic.

Very short version: don't compare two floating point numbers for equality, even with obvious, trivial identities like 10 == 10 / 3.0 * 3.0 or tan(x) == sin(x) / cos(x).


you should take care about precision:

  • Structure of a floating-point number
  • are you on 32bits, 64 bits Platform ?
  • you should read IEEE Standard for Binary Floating-Point Arithmetic
  • there are some intersting libraries such GMP, or MPFR.
  • you should learn how Comparing floating-point numbers
  • etc ...


Agreed with all of the responses that say you should not compare for equality. What you can do, however, is check if the numbers are close enough, like so:

if (abs(numberA - numberB) < CLOSE_ENOUGH)
{
  // Equal for all intents and purposes
}

Where CLOSE_ENOUGH is some suitably small floating-point value.

0

精彩评论

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