Hello everyone,
In my knowledge, float could only be represented by 7 decimal bits. But why in debugger, I can see more than 7 decimal bits?
(in my sample, Result2 is of 9 bits, other than 7, why?)
[Code]
float TotalBonus = 199.321F;
float Worker1 = 100F;
float Worker2 = 300F;
// Result1 49.83025 float
float Result1 = TotalBonus * Worker1 / (Worker1 + Worker2);
// Result2 149.490753 float
float Result2 = TotalBonus * Worker2 / (Worker1 + Worker2);
[/Code]
thanks in advance,
George
George GeorgePosted Jun 23, 2008, 6:17 AM
Thanks Ryan!
Great reply! 3 more comments.
1.
"maintained internally" -- internally you mean something like Visual Studio debugger? And this is why I see 9 digits in debugger?
2.
"Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different." -- what means "least significant digits"? Could you show me a sample float-point and what parts are "least significant digits" please? From the link, I can not find the answer. :-)
3.
"A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number." -- I am totally confused.
Does it mean something like this?
float a = something;
float b = somethingelse;
float result1 = b + a;
decimal result2 = (decimal) b + (decimal) a;
result1 and result2 are different? :-)
regards,
George
Ryan AlfordPosted Jun 20, 2008, 2:06 PM
____________________________________________________________
wow....this was really hard.
straight from the MSDN
http://msdn.microsoft.com/en-us/library/system.single.aspx
"Remember that a floating-point number can only approximate a decimal number, and that the precision of a floating-point number determines how accurately that number approximates a decimal number. By default, a Single value contains only 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. The precision of a floating-point number has several consequences:
-
Two floating-point numbers that appear equal for a particular precision
might not compare equal because their least significant digits are
different.
- A mathematical or comparison operation that uses a
floating-point number might not yield the same result if a decimal
number is used because the floating-point number might not exactly
approximate the decimal number.
- A value might not roundtrip if
a floating-point number is involved. A value is said to roundtrip if an
operation converts an original floating-point number to another form,
an inverse operation transforms the converted form back to a
floating-point number, and the final floating-point number is equal to
the original floating-point number. The roundtrip might fail because
one or more least significant digits are lost or changed in a
conversion."
now how much longer are we going to have to spoon feed you the answers to your questions?Posted Jun 19, 2008, 2:34 PM
George GeorgePosted Jun 19, 2008, 2:52 AM
Thanks.
I understand how IEEE represents a float number. In more precise, I do not think float has enough significand to represent 9 decimal digits, it has only 7. Any ideas?
http://en.wikipedia.org/wiki/Floating_point#Implementation_in_actual_computers:_IEEE_floating-point
regards,
George
Posted Jun 19, 2008, 2:42 AM