Professional Web Applications Themes

sprintf bug in 1.9.0? - Ruby

I just compiled the latest release of Ruby 1.9.0, and I'm getting unexpected behavior from it... markimac% /usr/local/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION' 11111110 1.9.0 markimac% /usr/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION' 00000000 1.6.8 it looks like sprintf, when you ask it to pad binary values with zeros, is instead padding with ones! It doesn't seem t have a problem with hex, just binary. -Mark...

  1. #1

    Default sprintf bug in 1.9.0?

    I just compiled the latest release of Ruby 1.9.0, and I'm getting
    unexpected behavior from it...

    markimac% /usr/local/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION'
    11111110
    1.9.0
    markimac% /usr/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION'
    00000000
    1.6.8

    it looks like sprintf, when you ask it to pad binary values with zeros,
    is instead padding with ones! It doesn't seem t have a problem with
    hex, just binary.

    -Mark



    Mark Guest

  2. #2

    Default Re: sprintf bug in 1.9.0?

    Hi,

    At Mon, 16 Feb 2004 19:49:36 +0900,
    Mark Hubbart wrote in [ruby-talk:92961]: 

    What's your platform?

    $ ruby -v -e'puts sprintf("%08b",0),RUBY_VERSION'
    ruby 1.9.0 (2004-02-15) [i686-linux]
    00000000
    1.9.0

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  3. #3

    Default Re: sprintf bug in 1.9.0?

    >>>>> "n" == nobu nokada <net> writes:

    n> $ ruby -v -e'puts sprintf("%08b",0),RUBY_VERSION'

    moulon% ruby -ve 'puts sprintf("%08b",0),RUBY_VERSION'
    ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]
    11111110
    1.8.1
    moulon%


    Guy Decoux


    ts Guest

  4. #4

    Default Re: sprintf bug in 1.9.0?

    > What's your platform?

    Guess I should have mentioned that :) Again, but with full
    version/release/platform tag:

    markimac% /usr/bin/ruby -ve 'puts sprintf("%08b",0)'
    ruby 1.6.8 (2002-12-24) [powerpc-darwin7.0]
    00000000
    markimac% /usr/local/bin/ruby -ve 'puts sprintf("%08b",0)'
    ruby 1.9.0 (2004-02-14) [powerpc-darwin]
    11111110




    Mark Guest

  5. #5

    Default Re: sprintf bug in 1.9.0?


    On Feb 16, 2004, at 3:12 AM, ts wrote:
     [/ref]
    >
    > n> $ ruby -v -e'puts sprintf("%08b",0),RUBY_VERSION'
    >
    > moulon% ruby -ve 'puts sprintf("%08b",0),RUBY_VERSION'
    > ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]
    > 11111110
    > 1.8.1
    > moulon%
    >
    >
    > Guy Decoux
    >
    >[/ref]
    Hmmmm... I hadn't thought to check 1.8, but I had it hanging around,
    too, so:
    markimac% /usr/local-old/bin/ruby -ve 'puts sprintf("%08b",0)'
    ruby 1.8.0 (2003-08-04) [powerpc-darwin]
    11111110

    there we are! I hadn't noticed that bug before, but then I guess I
    don't convert to binary strings very often...



    Mark Guest

  6. #6

    Default Re: sprintf bug in 1.9.0?

    >>>>> "t" == ts <inra.fr> writes:

    t> moulon% ruby -ve 'puts sprintf("%08b",0),RUBY_VERSION'
    t> ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]

    Not really sure (the problem is on line 566 in sprintf.c)

    if (bignum && !RBIGNUM(val)->sign)
    c = sign_bits(base, p);


    moulon% diff -u sprintf.c~ sprintf.c
    --- sprintf.c~ Thu Jul 3 13:00:18 2003
    +++ sprintf.c Mon Feb 16 13:40:08 2004
    -345,6 +345,7
    long v = 0;
    int base, bignum = 0;
    int len, pos;
    + VALUE tmp;

    switch (*p) {
    case 'd':
    -472,8 +473,8
    }

    if (sign) {
    - val = rb_big2str(val, base);
    - s = RSTRING(val)->ptr;
    + tmp = rb_big2str(val, base);
    + s = RSTRING(tmp)->ptr;
    if (s[0] == '-') {
    s++;
    sc = '-';
    -493,8 +494,8
    val = rb_big_clone(val);
    rb_big_2comp(val);
    }
    - val = rb_big2str(val, base);
    - s = RSTRING(val)->ptr;
    + tmp = rb_big2str(val, base);
    + s = RSTRING(tmp)->ptr;
    if (*s == '-') {
    if (base == 10) {
    rb_warning("negative number for %%u specifier");
    -502,8 +503,8
    }
    else {
    remove_sign_bits(++s, base);
    - val = rb_str_new(0, 3+strlen(s));
    - t = RSTRING(val)->ptr;
    + tmp = rb_str_new(0, 3+strlen(s));
    + t = RSTRING(tmp)->ptr;
    if (!(flags&FPREC)) {
    strcpy(t, "..");
    t += 2;
    -520,7 +521,7
    bignum = 2;
    }
    }
    - s = RSTRING(val)->ptr;
    + s = RSTRING(tmp)->ptr;

    format_integer:
    pos = -1;
    moulon%


    Guy Decoux


    ts Guest

Similar Threads

  1. printf sprintf
    By Paul Kraus in forum PERL Beginners
    Replies: 9
    Last Post: January 9th, 08:05 PM
  2. BUG: sprintf(%u) and ip2long
    By Daevid in forum PHP Development
    Replies: 3
    Last Post: October 14th, 10:46 PM
  3. #25364 [Opn->Fbk]: sprintf bug
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: September 2nd, 05:41 PM
  4. #25364 [NEW]: sprintf bug
    By reha at bilgiparki dot com in forum PHP Development
    Replies: 0
    Last Post: September 2nd, 05:35 PM
  5. sprintf help
    By Matt Oliverius in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 15th, 10:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139