in reply to Re^3: Numeric Comparisons Randomly Giving the Wrong Result
in thread Numeric Comparisons Randomly Giving the Wrong Result
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Numeric Comparisons Randomly Giving the Wrong Result
by Corion (Patriarch) on Sep 30, 2010 at 14:52 UTC | |
by mjscott2702 (Pilgrim) on Sep 30, 2010 at 14:56 UTC | |
|
Re^5: Numeric Comparisons Randomly Giving the Wrong Result
by halfcountplus (Hermit) on Sep 30, 2010 at 14:51 UTC | |
by mjscott2702 (Pilgrim) on Oct 01, 2010 at 10:59 UTC |