[Numpy-discussion] IEEE 754-2008 decimal floating point support

Michael Gilbert michael.s.gilbert at gmail.com
Wed Sep 8 16:28:26 EDT 2010


On Wed, 8 Sep 2010 22:20:30 +0200, Sandro Tosi wrote:
> On Wed, Sep 8, 2010 at 22:10, Michael Gilbert
> <michael.s.gilbert at gmail.com> wrote:
> > Here is an example:
> >
> >   >>> 0.3/3.0 - 0.1
> >   -1.3877787807814457e-17
> >
> >   >>> mpmath.mpf( '0.3' )/mpmath.mpf( '3.0' ) - mpmath.mpf( '0.1' )
> >   mpf('-1.3877787807814457e-17')
> >
> >   >>> decimal.Decimal( '0.3' )/decimal.Decimal( '3.0' ) - decimal.Decimal ( '0.1' )
> >   Decimal("0.0")
> >
> > Decimal solves the problem; whereas mpmath doesn't.
> 
> you can change mpmath precision up to an arbitrary high value:
> 
> In [4]: mpmath.mp.prec = 100
> 
> In [5]: mpmath.mpf( '0.3' )/mpmath.mpf( '3.0' ) - mpmath.mpf( '0.1' )
> Out[5]: mpf('0.0')

Thanks.  I already knew that.  I tried prec = 500, which did of course
get the error down quite a bit to begin with, but even so, it continued
to grow and grow.  So decimal wins out.

Mike



More information about the NumPy-Discussion mailing list