aspose file tools*
The moose likes Beginning Java and the fly likes float and double formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float and double formatting" Watch "float and double formatting" New topic
Author

float and double formatting

Michael Novello
Greenhorn

Joined: Mar 03, 2013
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

Joined: Jan 03, 2004
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

Joined: Mar 03, 2013
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?

Thanks for your help
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
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?

Thanks for your help


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

Joined: Mar 17, 2011
Posts: 8207
    
  23

Michael Novello wrote:Thanks for your help

You're welcome. And for more information, read this.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: float and double formatting