Michael Novello

Greenhorn

Posts: 9

posted 4 years ago

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!

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!

posted 4 years ago

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.

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 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

posted 4 years ago

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.

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.