Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   sprintf bug in 1.9.0? (http://www.velocityreviews.com/forums/t813627-sprintf-bug-in-1-9-0-a.html)

Mark Hubbart 02-16-2004 10:49 AM

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...

mark@imac% /usr/local/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION'
11111110
1.9.0
mark@imac% /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




nobu.nokada@softhome.net 02-16-2004 11:05 AM

Re: sprintf bug in 1.9.0?
 
Hi,

At Mon, 16 Feb 2004 19:49:36 +0900,
Mark Hubbart wrote in [ruby-talk:92961]:
> I just compiled the latest release of Ruby 1.9.0, and I'm getting
> unexpected behavior from it...
>
> mark@imac% /usr/local/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION'
> 11111110
> 1.9.0
> mark@imac% /usr/bin/ruby -e'puts sprintf("%08b",0),RUBY_VERSION'
> 00000000
> 1.6.8


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



ts 02-16-2004 11:12 AM

Re: sprintf bug in 1.9.0?
 
>>>>> "n" == nobu nokada <nobu.nokada@softhome.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



Mark Hubbart 02-16-2004 11:41 AM

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:

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





Mark Hubbart 02-16-2004 11:50 AM

Re: sprintf bug in 1.9.0?
 

On Feb 16, 2004, at 3:12 AM, ts wrote:

>>>>>> "n" == nobu nokada <nobu.nokada@softhome.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
>
>

Hmmmm... I hadn't thought to check 1.8, but I had it hanging around,
too, so:
mark@imac% /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...




ts 02-16-2004 12:46 PM

Re: sprintf bug in 1.9.0?
 
>>>>> "t" == ts <decoux@moulon.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




All times are GMT. The time now is 03:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.