Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Why does printf() do this to big numbers?

by Cody Fendant (Friar)
on Mar 17, 2010 at 06:11 UTC ( #829093=perlquestion: print w/replies, xml ) Need Help??
Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

printf( "%s\n", 2810337464 ); ## prints 2810337464 printf( "%d\n", 2810337464 ); ## prints -1484629832

Using ActiveState Perl 5.10 on Windows XP.

Replies are listed 'Best First'.
Re: Why does printf() do this to big numbers?
by Anonymous Monk on Mar 17, 2010 at 06:13 UTC
Re: Why does printf() do this to big numbers?
by fullermd (Priest) on Mar 17, 2010 at 06:31 UTC

    Because that's what you asked for.

    %d is asking for a signed int. On pretty much any platform out there, int is 32 bits. 2.8 billion is larger than can be represented in a signed int, ergo wraparound.

      On pretty much any platform out there, int is 32 bits

      Not necessarily.  With a "64-bit perl", "%d" can handle ints larger than 32 bits just fine:

      $ perl -e "printf qq(%d\n), 2810337464" 2810337464
      $ perl -V ... intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +6 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 ... Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_64_BIT_ +ALL USE_64_BIT_INT USE_ITHREADS USE_LARGE_FILES ...

        Ah, true. I guess my C-brain leaks through sometimes :) I guess that's probably the USE_64_BIT_INT.

        But at any rate, overflow is what the OP is seeing.

      $ perl -V:.+?size d_chsize='define'; d_malloc_good_size='undef'; d_malloc_size='undef'; doublesize='8'; fpossize='8'; gidsize='4'; i16size='2'; i32size='4'; i64size='8'; i8size='1'; intsize='4'; ivsize='4'; longdblsize='12'; longlongsize='8'; longsize='4'; lseeksize='8'; nvsize='8'; ptrsize='4'; shortsize='2'; sig_size='27'; sizesize='4'; u16size='2'; u32size='4'; u64size='8'; u8size='1'; uidsize='4'; uvsize='4';
Re: Why does printf() do this to big numbers?
by ikegami (Pope) on Mar 17, 2010 at 16:06 UTC

    Max 32-bit signed integer: 2,147,483,647
    Max 32-bit unsigned integer: 4,294,967,295

    You want to print 2,810,337,464, which lands between the two. You have a 32-bit build of Perl and you told Perl to print a signed integer. It started by converting the input to an integer if it wasn't already, then it treated the result as a signed integer. The result is 2810337464 - 2**32

    Ask for an unsigned integer or a floating point number:

    $ perl -e'printf( "%d\n", 2810337464 );' -1484629832 $ perl -e'printf( "%u\n", 2810337464 );' 2810337464 $ perl -e'printf( "%.0f\n", 2810337464 );' 2810337464
Re: Why does printf() do this to big numbers?
by ssandv (Hermit) on Mar 17, 2010 at 18:38 UTC
    This might be of some interest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829093]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-10-17 21:32 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (238 votes). Check out past polls.