in reply to strange sprintf behavior?

print sprintf( "%.30f", 66.5 * ( ( 100 + 19 ) / 100 ) ), "\n79.135 +000"; print sprintf( "%.2f", 66.5 * ( ( 100 + 19 ) / 100 ) ), " <-- wrong, + expected 79.14\n"; ; print sprintf( "%.30f", 66.5 * ( 100 + 19 ) / 100 ), "\n79.135 +000"; print sprintf( "%.2f", 66.5 * ( 100 + 19 ) / 100 ), " <-- ok = 7 +9.14\n";

produces:

79.134999999999990905052982270718 79.13500079.13 <-- wrong, expected 79.14 79.135000000000005115907697472721 79.13500079.14 <-- ok = 79.14

Which may make the difference more obvious.

update: deleted incorrect example about integer Vs floating point calculations.

Replies are listed 'Best First'.
Re^2: strange sprintf behavior?
by oseeliger (Initiate) on Oct 18, 2010 at 06:19 UTC

    Thank you all very much. It's now clearer and I think I understood it.

    The links are very informative.