Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do lt and gt in Perl not work for comparing real numbers?

Tags:

perl

Lets examine the following perl code

if ($a lt 0.00 or $a gt 100.000)
{
    print "a must be between 0 and 100 \n";
    exit 1
}
exit 0

Lets say a equals 5. The above code will exit with failure status because a isn't between 0 and 100.

Simply replacing the lt and gt with the actual operators they represent, < and > respectively, yields the expected results. Replacing the 100 with a number starting with a 9 will also yield the expected result.

Why are Perl's comparison operators telling me 5 is not between 0 and 100?

like image 412
DeepDeadpool Avatar asked Nov 10 '15 21:11

DeepDeadpool


1 Answers

lt and gt are string operators, with numbers you want to use plain old < and >. Perl is polymorphic on values, so it's monomorphic on operators (unlike for example python which is the other way around).

like image 183
Leon Timmermans Avatar answered Oct 03 '22 06:10

Leon Timmermans