Just another Perl shrine | |
PerlMonks |
Re^2: How to calculate the sum of columns to be equal to 100?by Laurent_R (Canon) |
on Aug 15, 2013 at 13:14 UTC ( [id://1049586]=note: print w/replies, xml ) | Need Help?? |
One floating-point technique that is sometimes used is called banker’s rounding,” which rounds even-ending numbers one way, odd-ending numbers the other. (I don’t rightly recall if Perl implements it.) Yes, Perl implements it, because it uses C libraries which implement the rounding recommended by IEEE. The idea is the following. Suppose, to take a simple case, that you want to round to the unit numbers which have only one decimal digit. Any number where the decimal digit is less than 5 will be rounded down and any number where the decimal digit is larger than 5 is rounded up. But what do you do if the decimal part is exactly 5? Say, for example, how do you round 3.5? The most usual method rounds such a number up. But bankers claim that this introduces a bias towards rounding up: out of ten possible decimal digits, one will not be rounded (0), 4 will be rounded down (1, 2, 3 and 4) and 5 will be rounded up (5 to 9). This can make a difference if you add a long series of numbers. So they decided that the rounding of the 5 decimal digit will be rounded up or down, depending on whether the previous digit is odd or even. This is what you can see in the somewhat strange output of a Perl one-liner below:
I actually once had to write a special rounding module just because my client considered the above to be simply wrong and wanted 2.5 to be rounded to 3.
In Section
Seekers of Perl Wisdom
|
|