# dunno why this is the output..??

Jack Daniel

Ranch Hand

Posts: 163

posted 12 years ago

Computers (well,

Now, the problem is that many rational numbers -- 1/3 for example -- are infinitely repeating fractions: 1/3 is 0.333333333... (forever.) You can't represent such a number exactly in floating-point notation, because there aren't an infinite number of bits. So sometimes in computer arithmetic you see something like 0.99999999999999123 when you expected 1.0 -- this is just the way it is. If you need to display floating-point numbers, you should never just println() them; you should use Java's formatting tools (like DecimalFormat, for instance) which can round the numbers appropriately for display.

For similar reasons, it's bad idea to compare floating-point numbers using ==; 0.9999999999999999123 and 1.0 are different values as far as the computer is concerned.

*most*computers) use what is called a*floating point representation*for real numbers. If you're familiar with scientific notation -- i.e., 1.5E-3 is 0.0015 -- then you understand how it works. There's a*mantissa*(the first number in scientific notation) and an*exponent*(the second number.) A floating point representation allocates a fixed number of bits for the mantissa, and a fixed number for the exponent.Now, the problem is that many rational numbers -- 1/3 for example -- are infinitely repeating fractions: 1/3 is 0.333333333... (forever.) You can't represent such a number exactly in floating-point notation, because there aren't an infinite number of bits. So sometimes in computer arithmetic you see something like 0.99999999999999123 when you expected 1.0 -- this is just the way it is. If you need to display floating-point numbers, you should never just println() them; you should use Java's formatting tools (like DecimalFormat, for instance) which can round the numbers appropriately for display.

For similar reasons, it's bad idea to compare floating-point numbers using ==; 0.9999999999999999123 and 1.0 are different values as far as the computer is concerned.

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 12 years ago
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

What makes the situation even more complicated is that most often (as in this case), the floating point numbers are stored in the binary system (instead of the decimal). 0.1 decimal is in fact an infinitely repeating fraction in the binary system: 0.000110001100011.... (whereas in the ternary system, 1/3 decimal would be representable without any loss of precision: 0.1)