Equality operators

S

skerrick32

I am performing some basic arithmetic on some variables and then using
a numeric equality operator to see if it returns what I think it should
be.
I am printing out the results and sure enough it is as expected. My
problem is when I use the numeric equality operator it returns false
even though my print statement showed that it should be true. The
string equality operator works(does return true) but I am curious what
is going on that is preventing the numeric version to work. Since I am
working strictly with numbers I would like to use the numeric version.

Here is the code that I am testing with.

use strict;

# define some variables
my $var_1 = 596181.10112 ;
my $var_2 = 600;
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;
my $var_5 = 200;
my $var_6 = 3;

# perform some arithmetic
my $my_num = (($var_1*$var_2)/$var_3);
$my_num = $my_num + $var_4 - $var_5;
$my_num = $my_num * $var_6;

# print result
print "my_num: $my_num\n";

# See which equality operator works. Why does numeric version not
work?????
if ($my_num == 600) {
print "Numeric equality operator evaluated to true. good.\n";
}
if ($my_num eq 600) {
print "String equality operator evaluated to true. \n";
}


Thanks
Eric
 
S

Scott Bryce

I am performing some basic arithmetic on some variables and then using
a numeric equality operator to see if it returns what I think it should
be.
I am printing out the results and sure enough it is as expected. My
problem is when I use the numeric equality operator it returns false
even though my print statement showed that it should be true. The
string equality operator works(does return true) but I am curious what
is going on that is preventing the numeric version to work. Since I am
working strictly with numbers I would like to use the numeric version.

Here is the code that I am testing with.

use strict;

# define some variables
my $var_1 = 596181.10112 ;
my $var_2 = 600;
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;
my $var_5 = 200;
my $var_6 = 3;

# perform some arithmetic
my $my_num = (($var_1*$var_2)/$var_3);
$my_num = $my_num + $var_4 - $var_5;
$my_num = $my_num * $var_6;

# print result
print "my_num: $my_num\n";

# See which equality operator works. Why does numeric version not
work?????
if ($my_num == 600) {
print "Numeric equality operator evaluated to true. good.\n";
}
if ($my_num eq 600) {
print "String equality operator evaluated to true. \n";
}

Add one more line of code:

print 600 - $my_num;

Then look here:

http://faq.perl.org/perlfaq4.html#Why_am_I_getting_lon
 
E

Eden Cardim

# print result
print "my_num: $my_num\n";

# See which equality operator works. Why does numeric version not
work?????
if ($my_num == 600) {
print "Numeric equality operator evaluated to true. good.\n";
}
if ($my_num eq 600) {
print "String equality operator evaluated to true. \n";
}
This has to do with the internal numeric representation of real numbers
in Perl.
Add this to your code right before the quote above and you'll
understand whats going on a little better:

$my_num = int($my_num);

for a detailed explanation:
perldoc -f int
perldoc perlnumber
 
S

skerrick32

Thanks Scott! Makes sense now. Curious why the string eq. op
works(return true) though.

Eric
 
M

marc.chapaux

Thanks Scott! Makes sense now. Curious why the string eq. op
works(return true) though.

Hi Eric,

Stringwise equality stringifies the number. This converts the number
from binary to decimal and it can involve rounding.

Add this to your code:
printf "\$my_num is about %.20f\n", $my_num;
You'll get something like this:
$my_num is about 599.99999999999977000000

Add this now:
printf "\$my_num is about %.20f\n", "$my_num";
and you get this:
$my_num is about 600.00000000000000000000

Marc
 
J

Jürgen Exner

I am performing some basic arithmetic on some variables and then using
a numeric equality operator to see if it returns what I think it
should be. [...]
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;

You must have missed the first class of "Introduction to Computer Numerics"
"Thou shalt not use equality test on floating point number"
And you must have missed the frequent postings about this topic

For further details see "perldoc -q 999"

jue
 
E

Eric J. Roode

I am performing some basic arithmetic on some variables and then
using a numeric equality operator to see if it returns what I think
it should be. [...]
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;

You must have missed the first class of "Introduction to Computer
Numerics"
"Thou shalt not use equality test on floating point number"
And you must have missed the frequent postings about this topic

Still, I have to say, I often wish perl had a floating-point "is close
enough to" equality operator. It would be in keeping with Perl's "the easy
things should be easy" philosophy.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
A

axel

Eric J. Roode said:
I am performing some basic arithmetic on some variables and then
using a numeric equality operator to see if it returns what I think
it should be. [...]
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;
You must have missed the first class of "Introduction to Computer
Numerics"
"Thou shalt not use equality test on floating point number"
And you must have missed the frequent postings about this topic
Still, I have to say, I often wish perl had a floating-point "is close
enough to" equality operator. It would be in keeping with Perl's "the easy
things should be easy" philosophy.

You could always use a simple subroutine...

#!/usr/local/bin/perl

use strict;
use warnings;

local $\ ="\n";

sub approx ($$$) {

# Returns 1 if a +/- limit == b

my ($a_op, $limit, $b_op) = @_;

($a_op + $limit >= $b_op)
&& ($a_op - $limit <= $b_op)
? 1 : 0;
}

print approx (10.001, 0.001, 10.002);
print approx (10.001, 0.0011, 10.002);

__END__

Results:

0
1

Of course there is bound to be something better on CPAN.

Axel
 
A

Anno Siegel

Newsgroups: comp.lang.perl.misc
Expires:
References: <[email protected]> <my7lf.1504$Oq3.384@trnddc05> <[email protected]>
Sender:
Followup-To:
Distribution:
Organization: TU-Berlin
Cc:
Subject: Re: Equality operators
Summary:
Keywords:

Eric J. Roode said:
I am performing some basic arithmetic on some variables and then
using a numeric equality operator to see if it returns what I think
it should be. [...]
my $var_3 = 715793.0585;
my $var_4 = -99.7375378599037;

You must have missed the first class of "Introduction to Computer
Numerics"
"Thou shalt not use equality test on floating point number"
And you must have missed the frequent postings about this topic

Still, I have to say, I often wish perl had a floating-point "is close
enough to" equality operator. It would be in keeping with Perl's "the easy
things should be easy" philosophy.

There is, and the OP has incidentally discovered it.

Use string comparison (eq) on float-point numbers. That's a quick and
dirty way to introduce some unspecified epsilon-fuzz. Its philosophy
is "if they print the same, they are the same".

Anno
 
A

Anno Siegel

Sherm Pendley said:
Could PPI help with that, maybe? According to its docs, there are only 28
non-Acme modules on CPAN that it cannot parse.

<http://search.cpan.org/~adamk/PPI-1.104/lib/PPI.pm>

Anything that parses Perl would, of course, help in making a solid source
filter. It may still not be the right tool for something conceptually
light-weight like float-point comparison. I'd use the overload route
for that.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top