sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.44k stars 480 forks source link

Maxima precision #11643

Open eviatarbach opened 13 years ago

eviatarbach commented 13 years ago

The Sage–Maxima interface currently does not maintain precision:

sage: a = RealField(200)(8.987551787368175506591796875e9)
sage: a
8.9875517873681755065917968750000000000000000000000000000000e9
sage: var('y')
y
sage: b = (a * x).mul(y, hold=True)
sage: b
(8.9875517873681755065917968750000000000000000000000000000000e9*x)*y
sage: c = (b / (x * y)).simplify()
sage: c
8987551787.37
sage: RealField(200)(c)
8.9875517873681697845458984375000000000000000000000000000000e9 

See here for more details: http://groups.google.com/group/sage-devel/browse_thread/thread/3598040d05b8413c

CC: @kcrisman @nbruin

Component: interfaces

Issue created by migration from https://trac.sagemath.org/ticket/11643

mwhansen commented 13 years ago
comment:2

There are a couple of issues for this. The first is converting to Maxima which is currently

sage: a._maxima_init_()
'8.9875517873681755065917968750000000000000000000000000000000000e9'

should be done with something like: calculus.symbolic_expression_from_string

sage: a._maxima_init_()
'8.987551787368175506591796875b9,fpprec:200'

Then, there is an issue going from Maxima to Sage. In sage.calculus.calculus.calculus.symbolic_expression_from_string, sage.rings.real_mpfr.create_RealNumber, but does not pass it any precision. This should try and detect the precision of the bigfloat.

nbruin commented 13 years ago

Attachment: trac_11643-preliminary.patch.gz

nbruin commented 13 years ago
comment:3

I have added a patch that can be applied on top of 4.7.1, that tries to preserve precision when translating back and forth between SR and maxima_lib. (not the string-level conversions; only the routines that are now used for integrate and limit). However, it looks like Maxima itself does not try to preserve precision at all with its bigfloats. Any arithmetic defaults to the set precision. So trying to preserve precision is not going to be very useful. At the very least, fpprec should be set appropriately every time. Evidence in the transcript below:

sage: M=sage.calculus.calculus.maxima
sage: from sage.interfaces.maxima_lib import *
sage: a=12345.6789012345678
sage: a.sign_mantissa_exponent()
(1, 868749920300559282, -46)
sage: a.prec()
60
sage: b=sr_to_max(SR(a))
sage: b
<ECL: ((BIGFLOAT SIMP 60) 868749920300559282 14)>
sage: M(b)
1.2345678901234568b4
sage: c=M(b)+M(b)
sage: c.ecl()
<ECL: ((BIGFLOAT SIMP 56) 54296870018784955 15)>
sage: sr_to_max(SR(2*a))
<ECL: ((BIGFLOAT SIMP 60) 868749920300559282 15)>
eviatarbach commented 11 years ago
comment:4

This is somewhat related to #9263.