简体   繁体   中英

Convert from mpf to Sympy Float without losing precision

Is there any way to do this? For example in the code below I lose precision:

>>> from sympy import *
>>> from sympy.mpmath import *
>>> mp.dps = 50
>>> a = mpf('1.0')/mpf('3.0')
>>> a
mpf('0.33333333333333333333333333333333333333333333333333311')
>>> b = Float(a,50)
>>> b
0.33333333333333331482961625624739099293947219848633

Converting to a string first seems to do the trick:

>>> from sympy import *
>>> from sympy.mpmath import *
>>> mp.dps = 50
>>> a = mpf('1.0')/mpf('3.0')
>>> a
mpf('0.33333333333333333333333333333333333333333333333333311')
>>> b = Float(a,50)
>>> b
0.33333333333333331482961625624739099293947219848633
>>> b = Float(str(a),50)
>>> b
0.33333333333333333333333333333333333333333333333333

sympify(a) does the right thing. Float(a) ought to work too, but it seems there is a bug .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM