Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Question on simple arithmetic using perl

by perlbaski (Sexton)
on Sep 28, 2011 at 21:27 UTC ( #928429=perlquestion: print w/replies, xml ) Need Help??
perlbaski has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

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

    When the number becomes too large for an integer it is automagically promoted to a float.

      Thank you Monk!! So does this mean its fine to use such arithmetic and not worry about whether perl can handle it correctly?
        No. A float can represent a wider range of values, but a lesser number of significant digits than an integer.

        I'm not quite sure what the question is:

        #!/usr/bin/perl -w use strict; my $all_bits_are_on = -1; printf ("all_bits => %X\n", $all_bits_are_one); my $x = $all_bits_are_on + 1; print "Adding -1 to +1 is: $x\n"; __END__ all_bits => FFFFFFFF Adding -1 to +1 is: 0
Re: Question on simple arithmetic using perl
by eyepopslikeamosquito (Chancellor) on Sep 29, 2011 at 10:58 UTC

    Also, be aware that Perl supports arbitrary size integers via the core Math::BigInt module. For example:

    use Math::BigInt; my $x = Math::BigInt->new('123456789012345678901234567890'); my $y = '123456789012345678901234567890'; $x += 1; $y += 1; print "x=$x\n"; print "y=$y\n";
    x=123456789012345678901234567891 y=1.23456789012346e+029
    Update: See also bigint and bignum.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928429]
Approved by ww
[Eily]: you could have tried randomly swapping your fingers
[Eily]: enigma style
[choroba]: so it was you who typed the whole phrases?
[Yaerox]: I feel like i'm not seeing the forest for the trees. Can someone give me a hint how to research converting all kinds of files into ansi? If origin file is utf8 convert to ansi, is origin file is ansi then skip file.
[1nickt]: Yaerox probably best to whip up an SSCCE demonstrating the failure in what you have now and post to SoPW as a question.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (17)
As of 2017-03-28 13:09 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (331 votes). Check out past polls.