Michael W. Ryder wrote: > In my continuing work learning Ruby while creating a Rational class I > ran into a problem with converting Strings. I am allowing Strings as > input to the .new method and converting them to a Float, Integer, or > Rational before continuing. At first I was just using Float(x) and > later converting the Float to an Integer. This works fine unless I try > something like Float("0x11") which crashes. Integer("0x11") returns 17 > as expected. Currently I have to try to convert to Float and if that > fails try to convert to Integer which seems to be more work than it > needs to be. > If I use Float("17") it returns 17.0 and Float(0x11) returns 17.0, both > as expected. Why doesn't Float("0x11") work? As Ruby doesn't appear to > handle Floats of bases other than 10 (or at least I haven't figured out > how to yet) I can see it choking on Float("0x11.34") but it shouldn't > choke on Float("0x11"). > Part of what I am doing is trying to follow the statement in Pickaxe > about duck typing and programming for any input, not just a select > subset. As someone who has done floating point validation on microprocessors, I can unequivocally state that the only "alternate base" of interest is to represent the byte string as nibbles or bits. That would be '400000000000000000' => 2.0 in EP, '40000000000000000' => 2.0 in DP, '400000000' => in SP. Which means of course that '0000000040000000'.to_f != '40000000'.to_f. These are formats for IEEE 754. Note that the internal representation of extended precision numbers has more bits. The Athlon had 91. Eariler cpus almost certainly had at least 83. There are very, VERY few people working in an environment where this matters, and most are good enough to write their own code as needed. The hex and binary options for specifying integers exist because we often need to look at integers as bit strings. Oct is a historical anomaly. Different classes have different behaviors because they are different. The fact that Float and Int happen to share the base class Numeric is of no concern to either. -- Posted via http://www.ruby-forum.com/.