printf vs. big numbers

Discussion in 'Perl Misc' started by jidanni@jidanni.org, Sep 21, 2009.

  1. Guest

    What does one need to do to make this not blow up when $k reaches 10?
    $ perl -wle 'for $k(10){for(qw/b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
    %b 11111111111111111111111111111111
    %d -1
    %lld -1
    %ld -1
    %Ld -1
    %o 37777777777
    %u 4294967295
    %x ffffffff
     
    , Sep 21, 2009
    #1
    1. Advertisements

  2. Uri Guttman Guest

    >>>>> "j" == jidanni <> writes:

    j> What does one need to do to make this not blow up when $k reaches 10?
    j> $ perl -wle 'for $k(10){for(qw/b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
    j> %b 11111111111111111111111111111111
    j> %d -1
    j> %lld -1
    j> %ld -1
    j> %Ld -1
    j> %o 37777777777
    j> %u 4294967295
    j> %x ffffffff

    perl -wle 'for $k(10){for(qw/f g b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
    %f 10000000000.000000
    %g 1e+10
    %b 1001010100000010111110010000000000
    %d 10000000000
    %lld 10000000000
    %ld 10000000000
    %Ld 10000000000
    %o 112402762000
    %u 10000000000
    %x 2540be400

    get a 64 bit machine!

    perl overflows integers to floats and you are seeing that. notice i
    added f and g formats. try those. printing an integer format of a too
    large float is going to generate nonsense since there aren't enough bits
    to convert the number correctly.

    uri
     
    Uri Guttman, Sep 21, 2009
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    876
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,686
  3. Replies:
    6
    Views:
    483
  4. Shaguf
    Replies:
    0
    Views:
    791
    Shaguf
    Dec 24, 2008
  5. Shaguf
    Replies:
    0
    Views:
    682
    Shaguf
    Dec 26, 2008
  6. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    629
  7. Shaguf
    Replies:
    0
    Views:
    431
    Shaguf
    Dec 26, 2008
  8. Shaguf
    Replies:
    0
    Views:
    434
    Shaguf
    Dec 24, 2008
Loading...