Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comparison operator bug #7564

Closed
p5pRT opened this issue Oct 28, 2004 · 5 comments
Closed

comparison operator bug #7564

p5pRT opened this issue Oct 28, 2004 · 5 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 28, 2004

Migrated from rt.perl.org#32190 (status was 'resolved')

Searchable as RT32190$

@p5pRT
Copy link
Author

p5pRT commented Oct 28, 2004

From gilal@md.huji.ac.il

Hi,
I sent this message by using perlbug, but I'm not sure if it was
properly sent from my account. If you're the wrong address, please let
me know.

I have been having trouble with the perl numerical comparison operators
(e.g. ==).

For example, if I run the following program (enclosed between the
lines), the answer I get is​:
6 is not equal to 6


#!/usr/bin/perl -w

my $ten = 10;
my @​a = (0.4,0.4,0.4);
my $point4 = mean(@​a);

tmpSub($ten,$point4);

sub tmpSub {
  ($num1,$num2) = @​_;
  my $num3 = $num1 * (1-$num2);
  if ($num3 == 6) {
  print "$num3 is equal to 6\n";
  }
  else {
  print "$num3 is not equal to 6\n";
  }
}

sub mean {
  my @​arr = @​_;
  my $mean = 0;
  foreach my $val (@​arr) {
  $mean += $val;
  }
  if (@​arr > 0) {
  $mean /= @​arr;
  }
  else {
  $mean = undef;
  }
  return $mean;
}


I have tried running this for​:
perl, v5.8.0 built for i386-linux-thread-multi
and
perl, v5.6.1 built for irix-n32

I haven't quite managed to pinpoint the source of the bug, which is why
the example is a little funny. I think it's the result of using a value
returned from a subroutine, since when I replace the call to the
subroutine with actual numbers (tmpSub(10,0.4)), I get the answer I'd
expect​: 6 is equal to 6. I have also encountered a similar problem with
the < operator.

Thanks,

Gila

@p5pRT
Copy link
Author

p5pRT commented Nov 4, 2004

From @smpeters

[gilal@​md.huji.ac.il - Thu Oct 28 00​:58​:12 2004]​:

Hi,
I sent this message by using perlbug, but I'm not sure if it was
properly sent from my account. If you're the wrong address, please let
me know.

I have been having trouble with the perl numerical comparison operators
(e.g. ==).

For example, if I run the following program (enclosed between the
lines), the answer I get is​:
6 is not equal to 6

---------------------------------------------------------------
#!/usr/bin/perl -w

my $ten = 10;
my @​a = (0.4,0.4,0.4);
my $point4 = mean(@​a);

tmpSub($ten,$point4);

sub tmpSub {
($num1,$num2) = @​_;
my $num3 = $num1 * (1-$num2);
if ($num3 == 6) {
print "$num3 is equal to 6\n";
}
else {
print "$num3 is not equal to 6\n";
}
}

sub mean {
my @​arr = @​_;
my $mean = 0;
foreach my $val (@​arr) {
$mean += $val;
}
if (@​arr > 0) {
$mean /= @​arr;
}
else {
$mean = undef;
}
return $mean;
}
--------------------------------------------------------------------

I have tried running this for​:
perl, v5.8.0 built for i386-linux-thread-multi
and
perl, v5.6.1 built for irix-n32

I haven't quite managed to pinpoint the source of the bug, which is why
the example is a little funny. I think it's the result of using a value
returned from a subroutine, since when I replace the call to the
subroutine with actual numbers (tmpSub(10,0.4)), I get the answer I'd
expect​: 6 is equal to 6. I have also encountered a similar problem with
the < operator.

Thanks,

Gila

I made a slight change to your program above to find out what $num3 is
equal to if it isn't 6. I also took a look at $point4, $num1, and $num2
as well.

$ perl comp.pl
point4​: 0.40000000000000007772
num1​: 10
num2​: 0.40000000000000007772
6 is not equal to 6
num3​: 5.99999999999999822364

The problem is not that "==" is not working. Its that floats are not
always exact. This is not a bug in Perl, but simply the facts of life
when dealing with floating point numbers.

@p5pRT
Copy link
Author

p5pRT commented Nov 4, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 4, 2004

@smpeters - Status changed from 'open' to 'resolved'

@p5pRT p5pRT closed this as completed Nov 4, 2004
@p5pRT
Copy link
Author

p5pRT commented Nov 6, 2004

From @schwern

On Thu, Nov 04, 2004 at 04​:37​:10PM -0000, Steve Peters via RT wrote​:

The problem is not that "==" is not working. Its that floats are not
always exact. This is not a bug in Perl, but simply the facts of life
when dealing with floating point numbers.

See perlfaq4 for more details.

--
Michael G Schwern schwern@​pobox.com http​://www.pobox.com/~schwern/
Moosy doom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant