#1: max value of an integer scalar
Posted on 20051103 09:50:00 by JeeBee
Dear Perl experts,
The following subroutine contains a while loop that continues to
multiply $p by 2, until some condition is met.
(It's for computing a Collatz tree)
$p turns 0 in this loop, however, and it remains zero for ever, of course.
My question is, what is exactly its maximum value? As I have been browsing
the web for a while, but am unable to find this information for Perl data
types. (I know I could perhaps use BigIntegers, but don't know whether
that would be really necessary).
Is there a difference between 'long' and 'int' or something in Perl???
Thanks in advance.
sub left_child_of($) {
my $p = shift;
print "Left of $p\n";
die("3 / p1 (p = $p)") if ($p1) % 3 != 0;
$p = ($p1) / 3;
print "L$p\n";
# keep multiplying by 2, until 3p1 and 2 / p1
while((($p1) % 3 != 0)  (($p1) % 2 == 0)) {
print "L$p: 3 / p1\n" if ($p1) % 3 != 0;
print "L$p: 2  p1\n" if ($p1) % 2 == 0;
$p <<= 1;
}
return $p;
}

To unsubscribe, email: beginnersunsubscribe@perl.org
For additional commands, email: beginnershelp@perl.org
<http://learn.perl.org/> <http://learn.perl.org/firstresponse>
Report this message 

#2: Re: max value of an integer scalar
Posted on 20051103 11:34:09 by krahnj
JeeBee wrote:
> Dear Perl experts,
Hello,
> The following subroutine contains a while loop that continues to
> multiply $p by 2, until some condition is met.
> (It's for computing a Collatz tree)
> $p turns 0 in this loop, however, and it remains zero for ever, of course.
>
> My question is, what is exactly its maximum value?
$ perl le'use POSIX;
print for SHRT_MAX, INT_MAX, LONG_MAX, FLT_MAX, DBL_MAX;
'
32767
2147483647
2147483647
3.40282346638529e+38
1.79769313486232e+308
> As I have been browsing
> the web for a while, but am unable to find this information for Perl data
> types. (I know I could perhaps use BigIntegers, but don't know whether
> that would be really necessary).
>
> Is there a difference between 'long' and 'int' or something in Perl???
perldoc perlnumber
> sub left_child_of($) {
> my $p = shift;
> print "Left of $p\n";
> die("3 / p1 (p = $p)") if ($p1) % 3 != 0;
> $p = ($p1) / 3;
> print "L$p\n";
> # keep multiplying by 2, until 3p1 and 2 / p1
> while((($p1) % 3 != 0)  (($p1) % 2 == 0)) {
> print "L$p: 3 / p1\n" if ($p1) % 3 != 0;
> print "L$p: 2  p1\n" if ($p1) % 2 == 0;
> $p <<= 1;
You are not multiplying $p by 2 like you said which would do what you want:
$p *= 2;
> }
> return $p;
> }
John

use Perl;
program
fulfillment

To unsubscribe, email: beginnersunsubscribe@perl.org
For additional commands, email: beginnershelp@perl.org
<http://learn.perl.org/> <http://learn.perl.org/firstresponse>
Report this message 
#3: Re: max value of an integer scalar
Posted on 20051103 12:02:21 by JeeBee
Thank you, John!
I see the limit is 32 bits now.
I just added 'use bigint', how easy!
Further, I was wondering about why you said I wasn't just multiplying by
2 using $p<<=1. Isn't it exactly equal to $p*=2 ???
This has anything to do with overflow or the representation of a number?
I though using twos complement it wouldn't make any difference.
Thanks for you help.
>> $p <<= 1;
>
> You are not multiplying $p by 2 like you said which would do what you want:
>
> $p *= 2;
use strict;
use bigint;
my $max_depth = shift;
$max_depth = 7 unless defined($max_depth);
sub output_tree($$);
sub left_child_of($) {
my $p = shift;
die("3 / p1 (p = $p)") if ($p1) % 3 != 0;
$p = ($p1) / 3;
if($p % 3 == 0) {
# stop, there is no left tree
return $p;
}
# keep multiplying by 2, until 3p1 and 2 / p1
while((($p1) % 3 != 0)  (($p1) % 2 == 0)) {
$p <<= 1;
}
return $p;
}

To unsubscribe, email: beginnersunsubscribe@perl.org
For additional commands, email: beginnershelp@perl.org
<http://learn.perl.org/> <http://learn.perl.org/firstresponse>
Report this message 
#4: Re: max value of an integer scalar
Posted on 20051103 12:14:45 by Adriano Ferreira
On 11/3/05, JeeBee <JeeBee@troefpunt.nl> wrote:
>
> Thank you, John!
> I see the limit is 32 bits now.
> I just added 'use bigint', how easy!
>
> Further, I was wondering about why you said I wasn't just multiplying by
> 2 using $p<<=3D1. Isn't it exactly equal to $p*=3D2 ???
It is equal just up to the moment the result goes over the maximum
integer value. This is a builddependent parameter, being 2**32  1 or
2**64  1, according to the Config value ivsize.
perl V:ivsize
That is, the maximum integer is something like 2**(ivsize*8)  1.
The shift is an integertype operation and will produce the same weird
results you see in programming C with an integer of the same type. For
example, in a perl which uses 64bit integers:
$ perl e 'print 1<<63, " ", 1<<64'
9223372036854775808 1
But the multiplication operator (*) is smarter and does an upgrade
from integer to floating point when needed, (possibly) increasing the
range of the correct results.
Adriano.

To unsubscribe, email: beginnersunsubscribe@perl.org
For additional commands, email: beginnershelp@perl.org
<http://learn.perl.org/> <http://learn.perl.org/firstresponse>
Report this message 
#5: Re: max value of an integer scalar
Posted on 20051103 15:19:37 by Shawn Corey
Adriano Ferreira wrote:
> But the multiplication operator (*) is smarter and does an upgrade
> from integer to floating point when needed, (possibly) increasing the
> range of the correct results.
>
> Adriano.
>
What JeeBee has stumbled across is a field of study called Numerical
Analysis. For a brief introduction see
http://en.wikipedia.org/wiki/Numerical_analysis
You are correct in that the multiplication operator automatically
converts from integer to float for large values of $p but JeeBee needs
an integer algorithm. For example, he uses ($p1) % 3, which is only
defined for nonnegative integers. The problem is for large values of
$p, $p == $p  1. This is because floats only store a fixed number of
digits. For example, if it stores 3 digits and $p = 1_000_000, then $p1
is 1.000e6  1 or 1.000e6. The one's column has dropped off the bottom;
it is too small to be recorded in the float.
I recommend that JeeBee takes his algorithm to a mailing list on
Numerical Analysis. They will have a lot more experience with the
foibles of computers doing simple arithmetic.

Just my 0.00000002 million dollars worth,
 Shawn
"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

To unsubscribe, email: beginnersunsubscribe@perl.org
For additional commands, email: beginnershelp@perl.org
<http://learn.perl.org/> <http://learn.perl.org/firstresponse>
Report this message 