From: Axel Etzold Date: 2008-05-27T07:58:38+09:00 Subject: Re: Why doesn't Float() work the same as Integer()? -------- Original-Nachricht -------- > Datum: Tue, 27 May 2008 07:24:28 +0900 > Von: "Michael W. Ryder" <_mwryder@worldnet.att.net> > An: ruby-talk@ruby-lang.org > Betreff: Re: Why doesn\'t Float() work the same as Integer()? > Pit Capitain wrote: > > 2008/5/21 Michael W. Ryder <_mwryder@worldnet.att.net>: > >> The method allows Rational.new(3.5) and returns 7/2 > > > > I would be very careful with converting Floats to Rationals. You know > > that Rational.new(0.1) wouldn't return 1/10? If not you should read > > about IEEE 754, which is the basis for Ruby's floating point numbers. > > > > I realize that my implementation will not work with all allowable Floats > such as Infinity, NAN, etc. but I am not trying to cover everything, > just some of the most common. I envision being able to use my version > of the rational module to implement a command line calculator that could > take "3.4 + 12 3/4" and return 16 3/20 or 16.15. > I can not see how you can say that 0.1 != 1/10. I tried looking up the > standard but the paper I looked at, about implementing the standard in a > language, made no mention of converting floating point numbers to > rational numbers. Dear Michael, maybe what Pit meant was what you find in a paper like this: http://docs.sun.com/source/806-3568/ncg_goldberg.html about numerical (mis-)representations of Floats on machines. If you want to do calculations with both Floats and fractions, you can first generate a best approximation (with respect to the size of the denominator, that is) of the Float by a fraction, via a continued fraction expansion of the Float, http://en.wikipedia.org/wiki/Continued_fraction http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations and then go on from there. But in the end, that wont be cleaner than floating point arithmetic - consider adding or multiplying the numbers defined by the following continued fraction expansions: cfpi=[3,1,4,1,5,9,...] ('single digits from pi') cfe=[2,7,1,8,2,8,1,8,2,8...] ('single digits from e') Best regards, Axel -- GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen! Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx