[Python-3000] Extension: mpf for GNU MP floating point

Mark Dickinson dickinsm at gmail.com
Sun Sep 30 17:12:00 CEST 2007


On 9/30/07, Facundo Batista <facundobatista at gmail.com> wrote:
>
> 2007/9/28, Rob Crowther <weilawei at gmail.com>:
>
> > a) MPF() now takes a float or integer argument because mpf_set_str is
> just
>
> Rob, there has been a *lot* of discussion about this for Decimal (see
> the PEP and discussions in python-dev and python-list around the PEP
> date).



But there's a major difference here: Decimal is *decimal* floating point,
MPF and Python floats are *binary* floating point.


So in the case of Decimal, conversion from a decimal string is a
straightforward operation, while conversion from binary involves making
choices about how to round, how many decimal digits to use, etc.


But for MPF it's the other way around:  conversion from a float is immediate
(the GMP precision is always at least 53 bits, so any IEEE double can be
represented as an MPF with no loss of information), while conversion from a
string involves hard work and decisions about how to round (and GMP's
approach to rounding seems pretty haphazard here...).


So since there's really no ambiguity about what MPF(float) should be, and
since it's a computationally trivial operation to initialize an MPF from a
float, you certainly want to allow MPF's to be initialized from floats.
 Admittedly, for initialization from a float *literal* there are still going
to be some surprises for the unwary:  with MPF precision set to 128 bits,
MPF( 1.1) is going to give a binary number that's an accurate representation
of the decimal 1.1 to only 53 bits, not 128 bits.



> The main issue here is what means the user if he calls MPF(2.3):
>
> a) MPF("2.3")
>
> b) MPF("2.2999999999999998")



All 3 of MPF(2.3), MPF("2.3") and MPF(" 2.29...998") should be different
values.  MPF(2.3) is the closest 53-bit binary floating point number to the
decimal 2.3, padded out with zero bits to whatever the current MPF precision
is.  MPF("2.3") should ideally be the closest p-bit binary floating point
number to the decimal 2.3, where p is the current precision.  But in fact,
with the way that GMP works it seems that all that can be said is that MPF("
2.3") is a (p+some_extra_bits) binary floating point number that's close
(but not necessarily closest) to the decimal 2.3.  Similarly for MPF("
2.29...998").


By the way, I'm wondering whether this discussion really belongs on
comp.lang.python instead...


Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20070930/31243546/attachment.htm 


More information about the Python-3000 mailing list