in reply to perl numeric expressions
The particular FAQ answers you'll need are:
Why am I getting long decimals (eg, 19.9499999999999) instead of the + numbers I should be getting (eg, 19.95)? Internally, your computer represents floating-point numbers in bin +ary. Digital (as in powers of two) computers cannot store all numbers exactly. Some real numbers lose precision in the process. This is +a problem with how computers store numbers and affects all computer languages, not just Perl. perlnumber show the gory details of number representations and conversions. To limit the number of decimal places in your numbers, you can use + the printf or sprintf function. See the "Floating Point Arithmetic" fo +r more details. printf "%.2f", 10/3; my $number = sprintf "%.2f", 10/3; Why is int() broken? Your int() is most probably working just fine. It's the numbers th +at aren't quite what you think. First, see the above item "Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?". For example, this print int(0.6/0.2-2), "\n"; will in most computers print 0, not 1, because even such simple nu +mbers as 0.6 and 0.2 cannot be presented exactly by floating-point numbe +rs. What you think in the above as 'three' is really more like 2.9999999999999995559.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
|
|---|