[LUNI] Help with a perl comparison...

Donald M. Weber dmw at geodesic.com
Fri Mar 22 08:29:01 CST 2002


I don't think it's the leading zeros that are tripping you up, the
assignment of $two is not doing what you'd expect.

my $one = 50;
my $two = 0000050;
print $one;
print "\n";
print $two;
print "\n";

#  Force an arithmetic comparision
my $diff = $one - $two;
my $zerotol = 0.0001;

if ($one == $two) {print "it works\n";}
else {print "its broken\n";}

print $diff;
print "\n";
print $zerotol;
print "\n";

if ($diff <= $zerotol) {print "Numerically close enough!";}
else {print "difference greater than tolerance";}

When you execute this....

dmw at duke# ./foo.pl
50
40
its broken
10
0.0001
difference greater than tolerance

See how $two is equal to 40?  Perl interprets the second assignment as
setting an octal value, therefore 50(base8)=40(decimal)



> -----Original Message-----
> From: luni-admin at luni.org [mailto:luni-admin at luni.org]On Behalf Of Joel
> F. Hacker
> Sent: Friday, March 22, 2002 12:13 AM
> To: luni at luni.org
> Cc: Joel Hacker-QA6240
> Subject: [LUNI] Help with a perl comparison...
>
>
> Anyone know how to make this little comparison become true?
>
> Thanks!
>
> Joel
>
> #!/usr/local/bin/perl
>
> my $one = 50;
> my $two = 0000050;
>
> if ($one == $two) {print "it works";}
> else {print "its broken";}
>
>
> It always trips up on the leading zeros
> and prints out "its broken".
>
>
>
>
> ______________________________________________________________________
> Linux Users Of Northern Illinois - Technical Discussion
> luni at luni.org
> http://luni.org/mailman/listinfo/luni
>




More information about the luni mailing list