A rational proposal
mwm at mired.org
Wed Dec 22 00:09:23 CET 2004
Nick Coghlan <ncoghlan at iinet.net.au> writes:
> Mike Meyer wrote:
>> I'm willing to do the work to get
>> decimals working properly with it.
> Facundo's post reminded me of some of the discussion about the
> interaction between floats and Decimal that went on when he was
> developing the module that eventually made it into the standard
> Perhaps Rational should have the same "arm's length" interaction with
> floats that Decimal does - require the user to set the precision they
> want by turning the float into a string that is then fed to the
> Rational constructor. My argument is that the following behaviour
> might be a little disconcerting:
> Py> x = 1.1
> Py> Rational(x)
> Rational("11000000000000001 / 10000000000000000")
Yeah. That's why the spec specified integers for the argumetns.
> as opposed to:
> Py> x = 1.1
> Py> Rational("%.2f" % x)
> Rational("11 / 10")
> (A direct Decimal->Rational conversion should be OK, however, since it
> should match standard expections regarding the behaviour of the
> fractional portion)
Yeah. I've already added that to my copy of the PEP. That makes
rationals like (1/"1E1000") much easier to represent properly.
> The other point is that, since converting a Rational to float() or
> Decimal() may lose information, this is something that Python
> shouldn't really do automatically. As Facundo suggested, a string
> representation is a suitable intermediate format that makes explicit
> the degree of precision used in the conversion.
Well, you want to be able to add floats to rationals. The results
shouldn't be rational, for much the same reason as you don't want to
convert floats to rationals directly. I figure the only choice that
leaves is that the result be a float. That and float(rational) should
be the only times that a rational gets turned into a float.
Are you suggestiong that float(rational) should be a string, with the
number of degrees of precesion set by something like the Context type
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list