Luke Galea wrote: >Hi All, > >I need to convert a float to a fraction.. So 1.5 to 1 1/2.. >The rational class would represent at least 3/2 well.. but I was surprised to >find that there is no way to easily go from float to rational.. > >Am I missing an easier way? > >Thanks in advance > I wrote a small float => rational method: class Float def to_r if self.nan? return Rational(0,0) # Div by zero error elsif self.infinite? return Rational(self<0 ? -1 : 1,0) # Div by zero error end s,e,f = [self].pack("G").unpack("B*").first.unpack("AA11A52") s = (-1)**s.to_i e = e.to_i(2) if e.nonzero? and e<2047 Rational(s)* Rational(2)**(e-1023)*Rational("1#{f}".to_i(2),0x10000000000000) elsif e.zero? Rational(s)* Rational(2)**(-1024)*Rational("0#{f}".to_i(2),0x10000000000000) end end end -- Jannis Harder