use strict; use warnings; use diagnostics; my $amount = 1e9; my $months = 12; my $repayment = $amount/$months; print "Monthly repayment = $repayment\n"; use integer; $repayment = $amount/$months; print "Monthly repayment = $repayment\n";
prints out:
Monthly repayment = 83333333.3333333 Monthly repayment = 83333333
which is not the same thing!
On a different, but related note, increasing the loan to 1e12 Galactic Credits, gives this:
Monthly repayment = 83333333333.3333 Monthly repayment = 0
That's because the loan amount is too big to hold in an integer (on my machine anyway):
perl -le'use POSIX;print for SHRT_MAX, INT_MAX, LONG_MAX, FLT_MAX, DBL +_MAX;' 32767 2147483647 2147483647 3.40282346638529e+38 1.79769313486232e+308
So use the integer pragma with caution!
In reply to Re^4: Numeric Comparisons Randomly Giving the Wrong Result
by mjscott2702
in thread Numeric Comparisons Randomly Giving the Wrong Result
by Likeless
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |