perlbaski has asked for the wisdom of the Perl Monks concerning the following question:
I ran below code with use integer line commented and uncommented.
#!/usr/bin/perl #use integer; my $max_neg_num=((2**32)-1); print "\nmax neg num is $max_neg_num\n"; $error=4294967263 -$max_neg_num-1; print "error is $error \n";
With use integer commented: max neg num is 4294967295 error is -33
With use integer uncommented. max neg num is -2 error is -32
When I looked at integer pragma documentation, it says numbers are wrapped around after largest +ve number i.e. 2^31-1. By that logic max neg number should be -1 represented by 2^32-1.
Also, I am surprised that without integer pragma, how come I am able to see the results correctly even though it is 32 bit machine? I think its a naive question, but I am afraid I will remain naive if I dont ask it. I would like to know how such arithmetic is handled on 32 bit machines. Is there a potability issue if I write the code as above with use integer commented?
Please enlighten me!!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Question on simple arithmetic using perl
by Anonymous Monk on Sep 28, 2011 at 21:34 UTC | |
by perlbaski (Sexton) on Sep 28, 2011 at 21:43 UTC | |
by Marshall (Canon) on Sep 28, 2011 at 23:08 UTC | |
by zwon (Abbot) on Sep 29, 2011 at 02:20 UTC | |
by Marshall (Canon) on Sep 29, 2011 at 03:29 UTC | |
Re: Question on simple arithmetic using perl
by eyepopslikeamosquito (Archbishop) on Sep 29, 2011 at 10:58 UTC |