Hello, i need some good information/link/paper about floating point precision calculus. Someone tell me : "be aware that using float point comparaison in C programming could be dangerous, for example compare two floating point number could be a mistake !!!!" Someone anderstood what it means ? What is the common mistakes of using floating point number ? How to avoid them ? Why does it occurs ? Can someone help me anderstood..... ? Thinks
float/double precision
Started by ●November 30, 2004
Reply by ●December 1, 20042004-12-01
In article <23925133.0411301902.59935857@posting.google.com>, seb <germain1_fr@yahoo.fr> wrote:>Someone tell me : "be aware that using float point comparaison in C >programming could be dangerous, for example compare two floating point >number could be a mistake !!!!"Comparing two floating point results for EQUALITY could be a mistake, because two floating point results may look the same in the precision which you display them, but may be different in the lowest significant bits. The problem is worse for single-precision floating point because the accumulation of error in the last few bits is a larger percentage of the total bits in the number, compared to the last few bits of a double-precision number. You can demonstrate the problem on a hand calculator. Divide 1 by 3, and then multiply the result by 3. The result will display as 1. But is it really equal to 1? No! Subtract 1 from this result and you won't get 0. You'll get something like -2.8043e-12, which is the residual roundoff error accumulated from the calculation (1/3)*3. If you had attempted to compare the equality of this result with 1.0, the comparison would have failed because the result isn't exactly equal to 1.0. -A
Reply by ●December 1, 20042004-12-01
seb wrote:> Hello, > > i need some good information/link/paper about floating point precision > calculus. > Someone tell me : "be aware that using float point comparaison in C > programming could be dangerous, for example compare two floating point > number could be a mistake !!!!" > > Someone anderstood what it means ? > What is the common mistakes of using floating point number ? How to > avoid them ? Why does it occurs ? > > Can someone help me anderstood..... ? > > ThinksA language point: in the US and Great Britain, "calculus" refers to that branch of mathematics concerned with integration and differentiation, differential equations, and the like. It is not a synonym for calculation. Floating-point numbers are less precise that integer or fixed-point numbers that use the same number of bits. Because of round-off errors, it is not safe to assume that A+B-A=B, nor it is not safe to assume that A*B/A=B. Therefor, the test if(X==Y) is likely to fail when you think it should pass. If you must compare X to Y, write if(|X-Y|<delta), where delta is a number suitably small for your application. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●December 1, 20042004-12-01
Jerry Avins wrote:> seb wrote: > > >>Hello, >> >>i need some good information/link/paper about floating point precision >>calculus. >>Someone tell me : "be aware that using float point comparaison in C >>programming could be dangerous, for example compare two floating point >>number could be a mistake !!!!" >> >>Someone anderstood what it means ? >>What is the common mistakes of using floating point number ? How to >>avoid them ? Why does it occurs ? >> >>Can someone help me anderstood..... ? >> >>Thinks > > > A language point: in the US and Great Britain, "calculus" refers to that > branch of mathematics concerned with integration and differentiation, > differential equations, and the like. It is not a synonym for calculation. > > Floating-point numbers are less precise that integer or fixed-point > numbers that use the same number of bits. Because of round-off errors, > it is not safe to assume that A+B-A=B, nor it is not safe to assume that > A*B/A=B. Therefor, the test if(X==Y) is likely to fail when you think it > should pass. If you must compare X to Y, write if(|X-Y|<delta), where > delta is a number suitably small for your application. > > JerryAnd pay attention to the amount of precision you have available -- your average single-precision floating point number ("float" on most C compilers) has a 24-bit mantissa; this number of bits is few enough to cause you grief with 16-bit data and some common filter topologies. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Reply by ●December 1, 20042004-12-01
in article 31519gF376leeU1@individual.net, Jerry Avins at jya@ieee.org wrote on 11/30/2004 23:57:> A language point: in the US and Great Britain, "calculus" refers to that > branch of mathematics concerned with integration and differentiation, > differential equations, and the like.unless you're a dental hygienist. r b-j :-)
Reply by ●December 1, 20042004-12-01
germain1_fr@yahoo.fr (seb) wrote in message news:<23925133.0411301902.59935857@posting.google.com>...> Hello, > > i need some good information/link/paper about floating point precision > calculus. > Someone tell me : "be aware that using float point comparaison in C > programming could be dangerous, for example compare two floating point > number could be a mistake !!!!" > > Someone anderstood what it means ? > What is the common mistakes of using floating point number ? How to > avoid them ? Why does it occurs ?You usually don't want to avoid floating point numbers if you know how to deal with them. The problem is that floating point numbers are *approximations* of the "true" number. If you, for instance, want to check if a result of a computation is zero, the naive way to do things is float a; if (a==0) /* Bad! */ { .... } The problem is that the variable a never becomes exactly zero. The number could be 1e-298, which for practical purposes is zero, but the result of a computation is never exactly 0. So the test above will never evaluate to "true". The correct way to do things is float a; float epsilon = 1e-6; /* some small number, depends on number format */ if (abs(a)<epsilon) /* Good */ { .... } Similarly, you can't compare two floating point numbers directly. You have to make a detour like float a,b; float epsilon = 1e-6; if (abs(a-b)<epsilon) { .... } The "small value" for epsilon must be chosen with care. For the 32 bit 'float' data type, it should be no smaller than 1e-8. For the 64 bit 'double' data type, it should be no smaller than 1e-15. I prefer to use 1e-6 for 'float' and 1e-12 for 'double'. Apart from that, you need to be aware of typecasting. Whenever you divide integers, make sure you typecast them *before* the division. For instance, int a,b; float c,d; a=1; b=3; c=a/b; /* The result here *might* be 0. Some compilers does an integer division before typecasting the result to float.*/ d= (float) a/(float) b; /* The compiler is forced to convert a and b to floats before doing any computations. The result here is guaranteed to be 0.333333...*/ As for references, I don't really know where to find good reading on such issues. I've learned these things "the hard way". I have volume 2 of Knuth's "The art of computer programming" where he talks about floaing point formats etc. You might find that book in a good library. The problem is that Knuth has chosen to use a very arcane computer system in his texts, so I find his books unnescessary hard to read. Floating point numbers appear to be right at the intersection between maths and computer science. The mathematicians regard this as a problem for computer scientists to take care of, while the computer scientists think it's a maths problem. Which means everyody takes for granted that somebody else know these things, and no one take the time to learn or write about it. Rune
Reply by ●December 1, 20042004-12-01
Hi, What was meant was probably for comparison only. E.g. if a and b are floating point numbers, you normally don't do stuff like: if (a==b). Instead you take into consideration that a could be 0.99999999 and b could be 1.000000001. So you would do "if(abs(a-b)<epsilon)" and you set epsilon as a small constant (how small depends on what application this is for and how much is the error expected...) That is what this meant. S�b Hello,> >i need some good information/link/paper about floating point precision >calculus. >Someone tell me : "be aware that using float point comparaison in C >programming could be dangerous, for example compare two floating point >number could be a mistake !!!!" > >Someone anderstood what it means ? >What is the common mistakes of using floating point number ? How to >avoid them ? Why does it occurs ? > >Can someone help me anderstood..... ? > >Thinks >This message was sent from http://www.DSPRelated.com
Reply by ●December 1, 20042004-12-01
seb wrote:>Hello, > >i need some good information/link/paper about floating point precision >calculus. >Someone tell me : "be aware that using float point comparaison in C >programming could be dangerous, for example compare two floating point >number could be a mistake !!!!" > >Someone anderstood what it means ? >What is the common mistakes of using floating point number ? How to >avoid them ? Why does it occurs ? > >Can someone help me anderstood..... ? > >Thinks > >It rather depends on what you are doing and where. I think your source really means floating point comparison to equal could be a mistake. Rounding issues means that answers rarely match exactly. Comparing for < or > is usually OK. However, floating point for real time DSP on some processors has real problems. On Pentiums of all kinds, and I believe on AMD devices too, working with extremely small floating point numbers causes *massive* slowdowns. I am talking about numbers so small the exponent has hit the end stop and the mantissa is no longer normalised. Many CPUs handle this situation through exceptions, in weird and horrible ways. This behaviour cannot generally be disabled. Working around the problem, by testing that your numbers are never so close to zero, slows your code down, but at least its speed is then predictable. Regards, Steve
Reply by ●December 1, 20042004-12-01
This problem arises from rounding errors.
Do not compare floating point numbers for equality,
because, although they might be notionally equal
in principle, rounding errors will cause them to
differ in practice.
Do not use
if ( fFirst == fSecond)
but go for
if ( abs(fFirst - fSecond) < fEPSILON)
where fEPSILON is a constant that you choose,
typically....
#define fEPSILON 0.000001.
AND DEFINITELY DO NOT USE
if ( fFirst = fSecond)
BECAUSE IT IS AN ASSIGNMENT AND NOT A COMPARISON.
(Very common C bug)
seb wrote:
>i need some good information/link/paper about floating point precision
>calculus.
>Someone tell me : "be aware that using float point comparaison in C
>programming could be dangerous, for example compare two floating point
>number could be a mistake !!!!"
Reply by ●December 1, 20042004-12-01
Rune Allnor wrote: ...> Which means everyody takes for granted that > somebody else know these things, and no one take the time to learn or > write about it.The OP might be interested in this: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm There are also some interesting links at the bottom of that page. Regards, Andor






