Inconsistent Behavior Converting String to Integer/Float


#1

$ruby-yarv -v
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]
YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts: ]

$ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

Correct behavior

num = “+3”
Float(num) # -> 3.0
Integer(num) # -> 3

Strange behavior

num = “3e2”
Float(num) # -> 300.0
Integer(num) # -> ArgumentError

Why is an ArgumentError raised in the second example?

OT: ruby-talk seems to be rejecting my email. I sent this twice, and
both times it did not make it to the list.

– Daniel


#2

“D” == Daniel H. removed_email_address@domain.invalid writes:

D> $ruby-yarv -v
D> ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]
D> YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts: ]

You have a problem with your version of yarv

uln% ruby -ve ‘num = “+3”; p num, Float(num), Integer(num)’
ruby 1.9.0 (2006-02-24) [x86_64-linux]
“+3”
3.0
3
uln%

uln% ruby-yarv -ve ‘num = “+3”; p num, Float(num), Integer(num)’
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [x86_64-linux]
YARVCore 0.4.0 Rev: 475 (2006-02-23) [opts: ]
“+3”
3.0
3
uln%

Guy Decoux


#3

ts wrote:

You have a problem with your version of yarv

I was asking specifically about the second example:

num = “3e2”
Float(num) # -> 300.0
Integer(num) # -> ArgumentError

Guy Decoux

– Daniel


#4

“D” == Daniel H. removed_email_address@domain.invalid writes:

D> I was asking specifically about the second example:

OK, sorry, I’ve not understood.

Even 1.6.8 give the same result

svg% ./ruby -ve ‘Integer(“3e2”)’
ruby 1.6.8 (2002-12-24) [i686-linux]
-e:1:in `Integer’: invalid value for Integer: “3e2” (ArgumentError)
from -e:1
svg%

1.4.2 give a strange result :slight_smile:

Guy Decoux


#5

I also find using a capatilized method name very odd and
un-rubyish. Looking through the docs, I see there is Array(),
Float(), Integer(), String(), URI(), DelegateClass(), and
possibly more. Why is this style used over Class#new?

– Daniel


#6

Hi,

In message “Re: Inconsistent Behavior Converting String to
Integer/Float”
on Sun, 26 Feb 2006 00:07:36 +0900, Daniel H.
removed_email_address@domain.invalid writes:

|# Strange behavior
|num = “3e2”
|Float(num) # -> 300.0
|Integer(num) # -> ArgumentError

Integer() does not understand scientific notation as an integer
representation.

						matz.