From: "Michael W. Ryder" <_mwryder@...> Date: 2008-05-22T04:10:06+09:00 Subject: Re: Why doesn't Float() work the same as Integer()? Yukihiro Matsumoto wrote: > Hi, > > In message "Re: Why doesn't Float() work the same as Integer()?" > on Tue, 20 May 2008 11:30:12 +0900, "Eric I." writes: > > |First, Float() and Integer() work like Ruby itself. In Ruby code you > |can hardcode integer literals using 17 (dec), 021 (oct), or 0x11 > |(hex). But you can only hardcode floating point literals using base > |ten. Admittedly, that's a bit of a punt since you'll probably ask why > |Ruby works that way. > > Because most of us would have hard time to guess how much 0x11.4 is. > Besides that, I don't see any usecase for converting hexadecimal > (integer) representation directly into float values. > > matz. > Thank you for the reply. I wanted to convert strings to Floats first as it simplified some of a method's logic. The method allows Rational.new(3.5) and returns 7/2, it also allows Rational.new(3.5, 2) and returns 7/4. Converting from strings seemed to be a simple addition to the method and rather than having separate logic for Integers and Floats I wanted to just convert all Strings to Floats and then convert the Floats. Since I had already gone this far I decided to add support for any number that Ruby allows, this is where I ran into the "inconsistency" in the methods Float(x) and Integer(x). As Ruby doesn't appear to support Floats in any base other than 10 I had no problem if it didn't convert '0x11.4' just like it wouldn't convert 'abcd'. But since it could convert valid Integers I was confused while it couldn't convert '0x11'.