in reply to >= returning unexpected result
Did any of the values involved arise from a floating point computation? print can produce misleading output in such situations. For example:
In this case, >= is correct; print is just misleading you. If this diagnosis is correct, the Numbers section of perlfaq4 is worth reading, as is perlnumber. To get trustier ints the general solution is to use sprintf. For example:$x = 1742-1e-12; $y = 0; $z = 1742; print "$x + $y >= $z is ", ( $x + $y >= $z ? 'true' : 'false', "\n"; __END__ 1742 + 0 >= 1742 is false
sub round { my $number = shift; my $precision = shift || 0; # optional precision my $template = "%.${precision}f"; return sprintf $template, $number; } $x = round( 1742-1e-12 ); $y = round( 0 ); $z = round( 1742 ); print "$x + $y >= $z is ", ( $x + $y >= $z ? 'true' : 'false', "\n"; __END__ 1742 + 0 >= 1742 is true
the lowliest monk
|
|---|