Often precision, rounding, and accuracy in
floating-point calculations can generate unexpected results. To
avoid this situation, follow these rules:
In a calculation involving both
single and double precision, the result will not usually be any
more accurate than single precision. If double precision is
required, be certain all terms in the calculation, including
constants, are specified in double precision.
Never assume that a simple
numeric value is accurately represented in the computer. Most
floating-point values can't be precisely represented as a finite
binary value. For example .1 is .0001100110011... in binary (it
repeats forever), so it can't be represented with complete accuracy
on a computer using binary arithmetic, which includes all PCs.
Never assume that the result is
accurate to the last decimal place. There are always small
differences between the "true" answer and what can be calculated
with the finite precision of any floating point processing
Never compare two floating-point
values to see if they are equal or not-equal. This is a corollary
to rule three. There are almost always going to be small
differences between numbers that "should" be equal. Instead, always
check to see if the numbers are nearly equal. That is, check to see
if the difference between them is very small or insignificant.