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

infinite loop when modulus used on very large numbers #429

Closed
p5pRT opened this issue Aug 24, 1999 · 2 comments
Closed

infinite loop when modulus used on very large numbers #429

p5pRT opened this issue Aug 24, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 24, 1999

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

Searchable as RT1270$

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From eric@fooz.com

#! /opt/perl/bin/perl

my ($i) = 457396837198630;

# Round to the next 100
while (($i % 100) != 0) {
  $i++;
}

print "$i\n";

exit;

+-----------------------------------------------------------+
| I'd give my right arm to be ambidextrous. | eric@​fooz.com |
+-----------------------------------------------------------+

@p5pRT
Copy link
Author

p5pRT commented Aug 25, 1999

From [Unknown Contact. See original ticket]

On Tue, 24 Aug 1999 at 13​:44​:07 -0700, Eric Desch wrote​:

The following code results in an infinite loop (it works with smaller
values of $i, but I don't know the limit for when it works/doesn't work).
This was on SunOS 5.5.1, in case that matters.

my ($i) = 457396837198630;
while (($i % 100) != 0) {
$i++;
}

Perl uses floating point for normal numbers, which suits most uses. It
is prepared to calculate the modulus of one of these for you, but you
have to be aware that at some point it's going to be approximate.
Read a general computer science book on floating point if this confuses
or bothers you.

You can use Math​::BigInt if you want to manipulate very large integers
exactly​:

  use Math​::BigInt;
  my $i = new Math​::BigInt '457396837198630';

... and the rest of your program is the same.

(Older versions of BigInt spit out 'undefined' warnings all over the
place. This has been fixed in the current development version of perl.)

Ian

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

No branches or pull requests

1 participant