Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Bug in sprintf?

Reply
Thread Tools

Bug in sprintf?

 
 
Wes Gamble
Guest
Posts: n/a
 
      08-18-2006
All,

Why does this fail?

irb(main):009:0> sprintf("%d", '08')
ArgumentError: invalid value for Integer: "08"
from (irb):9:in `sprintf'
from (irb):9

I assume it's because the 08 is being as an octal number prior to being
changed to a decimal. 01 - 07 work fine, 09 breaks.

But I'm telling sprintf to interpret it as decimal, so why can't I have
leading zeroes?

Please explain to me how this is expected behavior.

Thanks,
Wes

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
William Crawford
Guest
Posts: n/a
 
      08-18-2006
Wes Gamble wrote:
> I assume it's because the 08 is being as an octal number prior to being
> changed to a decimal. 01 - 07 work fine, 09 breaks.


Because 8 isn't an octal number. 0-7 are octal.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-18-2006
On Sat, 19 Aug 2006, Wes Gamble wrote:

> All,
>
> Why does this fail?
>
> irb(main):009:0> sprintf("%d", '08')
> ArgumentError: invalid value for Integer: "08"
> from (irb):9:in `sprintf'
> from (irb):9
>
> I assume it's because the 08 is being as an octal number prior to being
> changed to a decimal. 01 - 07 work fine, 09 breaks.
>
> But I'm telling sprintf to interpret it as decimal, so why can't I have
> leading zeroes?
>
> Please explain to me how this is expected behavior.
>
> Thanks,
> Wes


because:

harp:~ > cat a.c
main(){ char s[42]; int r = sprintf (s, "%d", 07); printf ("%d: <%s>\n", r, s); }


harp:~ > gcc a.c
harp:~ > a.out
1: <7>


harp:~ > cat a.c
main(){ char s[42]; int r = sprintf (s, "%d", 0; printf ("%d: <%s>\n", r, s); }


harp:~ > gcc a.c
a.c: In function `main':
a.c:1: numeric constant contains digits beyond the radix


ruby uses 'Integer(s)' to convert strings to ints and


harp:~ > ri Kernel.Integer
--------------------------------------------------------- Kernel#Integer
Integer(arg) => integer
------------------------------------------------------------------------
Converts _arg_ to a +Fixnum+ or +Bignum+. Numeric types are
converted directly (with floating point numbers being truncated).
If _arg_ is a +String+, leading radix indicators (+0+, +0b+, and
+0x+) are honored. Others are converted using +to_int+ and +to_i+.
This behavior is different from that of +String#to_i+.

Integer(123.999) #=> 123
Integer("0x1a") #=> 26
Integer(Time.new) #=> 1049896590


harp:~ > ruby -e' Integer "07" '

harp:~ > ruby -e' Integer "08" '
-e:1:in `Integer': invalid value for Integer: "08" (ArgumentError)
from -e:1

so it's expected, documented, and consistent with c.

a good pattern to use is:


harp:~ > ruby -e' atoi = lambda{|s| Integer(s) rescue %r/(\d+)/.match(s)[1].to_i rescue raise ArgumentError, s}; p(atoi["08"]); p(atoi["fubar"]) '
8
-e:1: fubar (ArgumentError)
from -e:1


because simply falling back on to_i yields


harp:~ > ruby -e' atoi = lambda{|s| Integer(s) rescue s.to_i}; p(atoi["08"]); p(atoi["fubar"]) '
8
0


regards.

-a
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

 
Reply With Quote
 
William Crawford
Guest
Posts: n/a
 
      08-18-2006
William Crawford wrote:
> Wes Gamble wrote:
>> I assume it's because the 08 is being as an octal number prior to being
>> changed to a decimal. 01 - 07 work fine, 09 breaks.

>
> Because 8 isn't an octal number. 0-7 are octal.


-slaps self- Think before you reply, William.

On second thought, this -does- seem wrong. It doesn't matter if you
tell it to be decimal, octal or hex, it all comes up with an error on 08
and 09, and not 00 to 07.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
William Crawford
Guest
Posts: n/a
 
      08-18-2006
William Crawford wrote:
> William Crawford wrote:
>> Wes Gamble wrote:
>>> I assume it's because the 08 is being as an octal number prior to being
>>> changed to a decimal. 01 - 07 work fine, 09 breaks.

>>
>> Because 8 isn't an octal number. 0-7 are octal.

>
> -slaps self- Think before you reply, William.
>
> On second thought, this -does- seem wrong. It doesn't matter if you
> tell it to be decimal, octal or hex, it all comes up with an error on 08
> and 09, and not 00 to 07.


Last time, I swear.
http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/145509 Says
that it IS trying to be octal because the 0 in front means that. (Why
it means that, I don't know.)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Carlos
Guest
Posts: n/a
 
      08-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Sat, 19 Aug 2006, Wes Gamble wrote:
>
>> All,
>>
>> Why does this fail?
>>
>> irb(main):009:0> sprintf("%d", '08')
>> ArgumentError: invalid value for Integer: "08"
>> from (irb):9:in `sprintf'
>> from (irb):9

[...]
>> But I'm telling sprintf to interpret it as decimal, so why can't I have
>> leading zeroes?
>>
>> Please explain to me how this is expected behavior.

[...]
> because:

[...]
> ruby uses 'Integer(s)' to convert strings to ints and

[...]
> Kernel#Integer

[...]
> If _arg_ is a +String+, leading radix indicators (+0+, +0b+, and
> +0x+) are honored.

[...]
> so it's expected, documented, and consistent with c.


It is not expected, nor documented, because the documentation of sprintf
says:
d | Convert argument as a decimal number.

...which is very explicit in that the argument will be interpreted as a
decimal number, not octal.

Greetings.



 
Reply With Quote
 
Wes Gamble
Guest
Posts: n/a
 
      08-18-2006
Thanks for the comprehensive response.

Well, I can't really argue with the C implementation, of course...

BUT

it feels like if I declare my intent to interpret the sprintf input as a
_decimal_ number with %d (there's a %o if I wanted octal), that is
should allow for leading zeroes.

My intent in writing sprintf("%d", '08') is to say take the string 08
and turn it into the number 8.

It isn't to say here's an octal number, please convert it to decimal for
me. If I think the string is octal, I'll use %o for that.

So in the spirit of what the user expects, I still think this smells
bad.

Feel free to convince me otherwise.

Thanks again,
Wes

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Wes Gamble
Guest
Posts: n/a
 
      08-18-2006
What Carlos said

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jan Svitok
Guest
Posts: n/a
 
      08-18-2006
On 8/18/06, Wes Gamble <(E-Mail Removed)> wrote:
> Thanks for the comprehensive response.
>
> Well, I can't really argue with the C implementation, of course...
>
> BUT
>
> it feels like if I declare my intent to interpret the sprintf input as a
> _decimal_ number with %d (there's a %o if I wanted octal), that is
> should allow for leading zeroes.
>
> My intent in writing sprintf("%d", '08') is to say take the string 08
> and turn it into the number 8.
>
> It isn't to say here's an octal number, please convert it to decimal for
> me. If I think the string is octal, I'll use %o for that.
>
> So in the spirit of what the user expects, I still think this smells
> bad.
>
> Feel free to convince me otherwise.
>
> Thanks again,
> Wes
>


As I see it, there are two separate things: WHAT number you want to
print, and HOW you want it print.

The latter one is specified by the format string.
The former one is specified by argument.

Think of sprintf("%d", 0x1234) or sprintf("%x", 123).

And it happens to be that '08' is in this case the same as 08 and that
is invalid octal number. QED.

Jano

 
Reply With Quote
 
Matthias Reitinger
Guest
Posts: n/a
 
      08-18-2006
Carlos wrote:

> It is not expected, nor documented, because the documentation of sprintf
> says:
> d | Convert argument as a decimal number.
>
> ...which is very explicit in that the argument will be interpreted as a
> decimal number, not octal.


I disagree on that. %d tells sprintf to expect an Integer as the
corresponding argument. Being given a String instead it tries to convert
it by calling Kernel#Integer. This fails for the reasons already
metioned.

Greetings, Matthias


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments