English 中文(简体)
Floating point again
原标题:

Yesterday I asked a floating point question, and I have another one. I am 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.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签