Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# float and double formatting

Michael Novello
Greenhorn
Posts: 9
Hello,

I am curious as to why the output from my second statement is different than the first:

Output is:

Floating point fun: 123.456703
Floating point fun: 123.456700

Why does my floating-point literal end up with a trailing 03, while my double literal ends up with 00 ? As far as I know, it doesn't have anything to do with %f, since this is the appropriate format specifier for both floats and doubles.

Also, what is a 'canonical representation'? I looked it up and all I found was something about Roman Catholic law.

Thank you!

Jeff Verdegan
Bartender
Posts: 6109
6
Michael Novello wrote:
Output is:

Floating point fun: 123.456703
Floating point fun: 123.456700

Why does my floating-point literal end up with a trailing 03, while my double literal ends up with 00 ?

It's because float has less precision than double, so its error in representing 123.4567 is larger than double's error--large enough to show up in the number of digits you've chosen to print.

Also, what is a 'canonical representation'? I looked it up and all I found was something about Roman Catholic law.

Root, base, default, true, official... something like that. It's when there's more than one way to represent something, and one of those ways is chosen to be the "official" way.

Michael Novello
Greenhorn
Posts: 9
So instead of simply padding the value with zeros (since float is obviously large enough to hold the 123.4567 portion) it just goes ahead and comes up with some arbitrary voodoo in its place?

Jeff Verdegan
Bartender
Posts: 6109
6
Michael Novello wrote:So instead of simply padding the value with zeros (since float is obviously large enough to hold the 123.4567 portion) it just goes ahead and comes up with some arbitrary voodoo in its place?

No, it's not arbitrary. It's a side-effect of representing base-10 fractions in a base-2 format. For example, neither float nor double can even represent 1/10 exactly. It's a repeating fraction, just like 1/3 is 0.333... in base-10. The IEEE-754 format stores values in powers of 2--positive powers and negative powers. Any fractional part is made up of a combination of 1/2 (0.5), 1/4 (0.25), 1/8 (0.125), 1/16 (0.0625), etc. That particular fraction presumably ended up as .4567025 which got rounded to .456703 by your specified formatting.

Winston Gutkowski
Bartender
Posts: 10417
63
Michael Novello wrote:Thanks for your help