Float vs Double
Nature of the number format doesn't allow for certain numbers to be exact. Ignoring the exponent and sign bits of a float or double and just look at the bits for the actual number (sign and exponent are integer like so nothing fancy there.) So lets say in the case of 3; or for just the number .3 since 3, 30, 300, .03 is just a power of .3; how will it be encoded in the float/double. (Note there is a slight difference between how a float and double are encoded but in general the idea is the same.)
For integers each bit is a power of 2
Bit Value
1 2
2 4
3 8
...
N 2^N
To get the total value of the int you add them up. Similarly with floats but the values are represented differently.
Bit Value
1 1/2
2 1/4
3 1/8
...
N 1/N^2
So (first number is bit value) .3 = 0*1/2 + 1*1/4 + 0*1/8 + 0*1/16 + 1*1/32 + 1*1/64 ... In the end the value is very very near to .3 but may end up being actually .299999[some numbers] or .300000[some numbers]. This also can add to some grief because you may have a threshold or precalculated number, that is very accurate, and you compare that with a nasty calculation which may be less accurate due to the earlier point about precision and or other calculations that continue to propagate the errors of numbers that can't be exactly represented and the two numbers may be a bit off. This is where you may end up adding some fuzzy logic to say, eehh, close enough.
E.G.:
/* This will fail at times.*/
if (doubleValue == 0) {...}
/* This will be a bit better.*/
if (FABS(doubleValue) < SOME_SMALL_DOUBLE_CLOSE_TO_ZERO) {...}
Also when converting to an integer, you need to be aware that 2.9999 will translate to 2 while 3.00001 will translate to 3. You may need to do some rounding to get the right 'integer' value from the float/double.
Weee, fun with floating point types.
For integers each bit is a power of 2
Bit Value
1 2
2 4
3 8
...
N 2^N
To get the total value of the int you add them up. Similarly with floats but the values are represented differently.
Bit Value
1 1/2
2 1/4
3 1/8
...
N 1/N^2
So (first number is bit value) .3 = 0*1/2 + 1*1/4 + 0*1/8 + 0*1/16 + 1*1/32 + 1*1/64 ... In the end the value is very very near to .3 but may end up being actually .299999[some numbers] or .300000[some numbers]. This also can add to some grief because you may have a threshold or precalculated number, that is very accurate, and you compare that with a nasty calculation which may be less accurate due to the earlier point about precision and or other calculations that continue to propagate the errors of numbers that can't be exactly represented and the two numbers may be a bit off. This is where you may end up adding some fuzzy logic to say, eehh, close enough.
E.G.:
/* This will fail at times.*/
if (doubleValue == 0) {...}
/* This will be a bit better.*/
if (FABS(doubleValue) < SOME_SMALL_DOUBLE_CLOSE_TO_ZERO) {...}
Also when converting to an integer, you need to be aware that 2.9999 will translate to 2 while 3.00001 will translate to 3. You may need to do some rounding to get the right 'integer' value from the float/double.
Weee, fun with floating point types.
| Messages In This Thread |
|
Float vs Double - bwalters - Dec 10, 2005, 10:04 AM
Float vs Double - Cochrane - Dec 10, 2005, 10:38 AM
Float vs Double - bwalters - Dec 10, 2005, 10:52 AM
Float vs Double - zKing - Dec 10, 2005, 12:18 PM
Float vs Double - OneSadCookie - Dec 10, 2005, 03:56 PM
Float vs Double - zKing - Dec 10, 2005, 04:29 PM
Float vs Double - jfaller - Dec 12, 2005, 10:58 AM
Float vs Double - Zekaric - Dec 12, 2005, 11:18 AM
Float vs Double - zKing - Dec 12, 2005, 11:40 AM
Float vs Double - Oblivion - Dec 12, 2005, 03:58 PM
Float vs Double - Najdorf - Dec 19, 2005, 11:08 AM
Float vs Double - ThemsAllTook - Dec 19, 2005, 12:40 PM
Float vs Double - Najdorf - Dec 19, 2005, 03:05 PM
Float vs Double - Fenris - Dec 19, 2005, 04:15 PM
Float vs Double - jfaller - Dec 20, 2005, 07:07 AM
Float vs Double - Fenris - Dec 20, 2005, 09:21 AM
Float vs Double - Zekaric - Dec 20, 2005, 10:25 AM
Float vs Double - Fenris - Dec 20, 2005, 12:29 PM
Float vs Double - OneSadCookie - Dec 20, 2005, 12:39 PM
Float vs Double - skyhawk - Dec 20, 2005, 01:10 PM
Float vs Double - Zekaric - Dec 20, 2005 01:45 PM
Float vs Double - Fenris - Dec 20, 2005, 05:40 PM
Float vs Double - Zekaric - Dec 20, 2005, 06:10 PM
Float vs Double - jfaller - Jan 3, 2006, 02:44 PM
Float vs Double - DoG - Jan 3, 2006, 03:49 PM
|
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| float value changes when passed to function | kendric | 5 | 3,130 |
Nov 15, 2009 01:57 PM Last Post: kendric |
|

