Closed p5pRT closed 21 years ago
Hi\,
Return value of sprintf( "%2.0f"\, 0.5 ) is 0! But *printf for the value 1.5 works great! Many numbers are also rounded badly. You can play adding some 0's and 1 after 0.5\, say 0.501 or 0.5000000000000001. :}
Cheers\, -- Shige\, ts@icu.ac.jp
On Fri\, Apr 21\, 2000 at 03:04:20PM +0900\, TOMITA Shigenari wrote:
Return value of sprintf( "%2.0f"\, 0.5 ) is 0! But *printf for the value 1.5 works great! Many numbers are also rounded badly. You can play adding some 0's and 1 after 0.5\, say 0.501 or 0.5000000000000001. :}
% perldoc -q round
=head2 Does Perl have a round() function? What about ceil() and floor()? Trig functions?
[...]
Rounding in financial applications can have serious implications\, and the rounding method used should be specified precisely. In these cases\, it probably pays not to trust whichever system rounding is being used by Perl\, but to instead implement the rounding function you need yourself.
To see why\, notice how you'll still have an issue on half-way-point alternation:
for ($i = 0; $i \< 1.01; $i += 0.05) { printf "%.1f "\,$i}
0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1.0 1.0
Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.
Ronald
On a related topic\, I am putting the finishing touches on a module which will deal with the floating point/rounding issues completely in many normal situations. I have already written and distributed Math::Currency\, which deals with currency values as integers\, using strict financial style rounding. I have been using it in a production environment for nearly a year now.
I have now (mostly) generalized that to be Math::FixedPrecision\, which will allow arbitrary precision to be maintained\, without either rounding errors or floating point nonsense (e.g.\, what is 100/9?). I will then turn Math::Currency into a wrapper and use the Posix Locale data to use the appropriate currency display. I foresee Math::FixedPrecision to also be useful in scientific situations\, where the result of a calculation should be no more accurate than the least accurate datum.
Watch CPAN for an announcement soon...
John Peacock
____________________Reply Separator____________________ Subject: Re: [ID 20000420.006] rounding floating numbers by *printf i Author: Ronald J Kimball \rjk@​linguist\.dartmouth\.edu Date: 4/21/2000 10:48 AM
On Fri\, Apr 21\, 2000 at 03:04:20PM +0900\, TOMITA Shigenari wrote:
Return value of sprintf( "%2.0f"\, 0.5 ) is 0! But *printf for the value 1.5 works great! Many numbers are also rounded badly. You can play adding some 0's and 1 after 0.5\, say 0.501 or 0.5000000000000001. :}
% perldoc -q round
=head2 Does Perl have a round() function? What about ceil() and floor()? Trig functions?
Thank you\, Ronald!
NOOOO\, I don't blame Perl! I had no idea to type 'perldoc -q round' at all... X{ I'm really sorry for my poor programing experience. Have a nice day! -- Shige\, I like Perl & integers.
Date: Fri\, 21 Apr 2000 10:48:15 -0400 From: Ronald J Kimball \rjk@​linguist\.dartmouth\.edu
On Fri\, Apr 21\, 2000 at 03:04:20PM +0900\, TOMITA Shigenari wrote:
> Return value of sprintf( "%2.0f"\, 0.5 ) is 0!
% perldoc -q round
Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.
Ronald
not-a-bug
@iabyn - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#3134 (status was 'resolved')
Searchable as RT3134$