in reply to Re^3: perl subtraction
in thread perl subtraction
Consider:
If you have the value 1/3, or 0.333333333... (using a periodic decimal number rather than a periodic binary number for simplicity), of a dollar and you multiply it by 100, you get 33.333333... cents, which is still not equal to 33 cents. You have to round the 33.333333... off to just 33 before they'll be equal.
In this particular example, you could use int $val to convert the floating-point 33.333333... to the integer 33, but, in the general case, you'd probably want to use int ($val + 0.5) for simple rounding like I was taught in elementary school (always round 1/2 up), assuming you're only working with positive values. If you have to cope with negative numbers or if the accumulation of errors from always rounding 0.5 up would be an issue, then you should check CPAN for modules which implement a rounding technique appropriate to your needs.
|
|---|