oseeliger has asked for the wisdom of the Perl Monks concerning the following question:
I'm pretty confused about these 4 statements ( or I'm just a blockhead in understanding the evaluation order / precedence of operators and parentheses ).
The first and third statement print a simple arithmetic opertation (using differently placed parentheses) in a float value interpreted sprintf instruction. That is ok.
But why does the second and fourth statement print different rounded values? I have no clue and don't know how to find out.
Please, could someone advise?
perl -e 'print sprintf( "%f", 66.5 * ( ( 100 + 19 ) / 100 ) ), "\n +79.135000"' perl -e 'print sprintf( "%.2f", 66.5 * ( ( 100 + 19 ) / 100 ) ), " < +-- wrong, expected 79.14\n"' perl -e 'print sprintf( "%f", 66.5 * ( 100 + 19 ) / 100 ), "\n +79.135000"' perl -e 'print sprintf( "%.2f", 66.5 * ( 100 + 19 ) / 100 ), " < +-- ok = 79.14\n"'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: strange sprintf behavior?
by Anonymous Monk on Oct 15, 2010 at 12:59 UTC | |
|
Re: strange sprintf behavior?
by TomDLux (Vicar) on Oct 15, 2010 at 14:27 UTC | |
|
Re: strange sprintf behavior?
by ig (Vicar) on Oct 15, 2010 at 16:01 UTC | |
by oseeliger (Initiate) on Oct 18, 2010 at 06:19 UTC |