How precise can we trust floats to be?

Started by
30 comments, last by PlayGGY 20 years, 3 months ago
In other words, when comparing for equality, should I compare if each number is within .002? Or .0002, or what? Sorry if it is stupid, but I know floats get screwed up. I think .0002 is safe (Num1 - Num2 < .0001 && Num1 - Num2 > -.0001), but how safe are floats? EDIT: See my second to last reply (on page 2 by default). [edited by - PlayGGY on December 26, 2003 9:43:15 PM] [edited by - PlayGGY on December 28, 2003 3:48:11 AM] [edited by - PlayGGY on December 28, 2003 8:11:04 PM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
Advertisement
According to page 2 of C/C++ Programmer''s Reference by Herbert Schildt, 2003 3rd ed, a float has 6 digits of precision, and a double has 10 digits of precision. So I guess this means that comparing floats within about 0.00001 should be reasonable. Remember that this is relative to the magnitude of the numbers, not the difference, so testing x - y < 0.00001 is not enough, you must do something like x - y < 0.00001 * x (or y) just incase x and y are really large or really small.

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Right. I assumed that the closer to zero the number got, the more precise it got. I think I will read up on how floats work... I don't really understand how its data is stored.

[edited by - PlayGGY on December 26, 2003 9:54:54 PM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
You can compare floats using equality, if you know what you''re doing.

float f = 2;assert( f == 2.0f );float g = f / 2.0;assert( g == 1.0f ); 


Only when you use floats for lots of intermediate calculations, and/or when you don''t know exactly the order of operations (beccause of complex logic) do you need to worry about error-induced epsilons.

And, if that''s the case, then any specific constant you come up with is going to be wrong -- you have to analyze the code and get a constant per case. Not to mention that a fixed epsilon is always wrong. If your floats have the values 200,000, comparing 0.002 isn''t going to make any difference; you might as well compare with ==. Meanwhile, if the two floats have values 0.010 and 0.008, do you really think they should be considered the same? They''re off by 25% from each other.

If you find that you need fixed-precision epsilons, then you probably should be using fixed-precision math.
enum Bool { True, False, FileNotFound };
Doc is almost right. Assuming he is right baout the six digits of precision, that's in scientific notation.

0.00001 X 10^n

where n is your current power. If you're comparing 500 to 700, then the degree of precision is .001 (again, assuming the 6 digits is correct)

edit: DOH! didn't notice the last part of his message, never mind doc!

[edited by - C-Junkie on December 26, 2003 10:00:56 PM]
Man, I am confused now... I was reading about the IEEE754 format here: http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html, and it says there is a signed bit. I had heard that was no signed bit for normal numbers, and since there is one for floats, how is it that 0 = -0 (which it does)? When comparing for equality with floats, it can't be done conmpletely bit-wise, because the signed bit would throw off, like I said, -0 and 0.

EDIT: Spoke too soon... I didn't read the entire article, sorry guys.

[edited by - PlayGGY on December 26, 2003 10:07:01 PM]

[edited by - PlayGGY on December 26, 2003 11:46:12 PM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
OK, I really don't understand floats.

This is the binary representation of the float 1;

1111111000000000000000000000000

and this is it of 2:

1000000000000000000000000000000

I am reading an article about it (http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html), but I don't understand the "mantissa" part. Does anyone care to explain to me about it?





[edited by - PlayGGY on December 26, 2003 11:42:25 PM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
I don''t know the specifics of the IEEE floating-point format, but it''s essentially scientific notation in a binary number system. The two parts of a number in scientific notation are the exponent (which is a power that the number system base is raised to) and the mantissa (the number multiplied by that power).

Because the decimal system is much more familiar than the binary system, here''s a decimal example. The number 375000 would be represented in scientific notation as (3.75 * 10^5). Then, 3.75 is the mantissa, and 5 is the exponent.
EDIT: Deleted old post, too hard to read.


What I should simply ask is: In a floating point, their are essentially 24 bits to represent the mantissa, or the "4.32" part of 4.32 * 10^1. So how do you get 6 digits of precision? 1/(2^24) is about 0.00000006, so wouldn't that be 7 digits? Either way, does this mean that a float can only accuratley represent 6/7 digits, regardless of decimal place? If so, in the first replier's example, he said "x - y < 0.00001 * x (or
y)" If x was .00001, wouldn't that lead to testing against .0000000001, which is too precise? As you can see, I am super confused.




[edited by - PlayGGY on December 26, 2003 12:54:10 AM]

[edited by - PlayGGY on December 27, 2003 1:09:26 AM]

[edited by - PlayGGY on December 27, 2003 1:10:03 AM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
Yes, that means a float can only accurately represent 6-7 digits, regardless of decimal place. But the exponent determines which 6-7 digits are represented. If you look at the number written out in standard form, find the first non-zero digit from the left; the 6-7 digits that are represented start from that one.

If x was .00001 and you''re testing for an accuracy of .00001 * x, then you would test against .0000000001, but it isn''t too precise. Don''t forget that everything gets shifted into scientific notation, so you''re comparing (1 * 10^-5) with an accuracy of (1 * 10^-10) or (0.00001 * 10^-5) , which is within the 6-digit rule of thumb.

This topic is closed to new replies.

Advertisement