Updates:
Status: Started
Comment #8 on issue 2107 by [email protected]: Real(0.1, 10) - Real(0.1,
10) -> 3.637923296e-13
http://code.google.com/p/sympy/issues/detail?id=2107
Going further:
In [1]: a = Float(0.1, 10)
(0, mpz(3602879701896397), -55, 52) # a._mpf_
In [2]: b = Float("0.1", 10)
(0, mpz(54975581389), -39, 36) # b._mpf_
Next examples print lhs._mpf_, rhs._mpf_, result._mpf_, prec (in bits)
In [3]: S.Zero + a
(0, mpz(3602879701896397), -55, 52) (0, mpz(0), 0, 0) (0, mpz(54975581389),
-39, 36) 37
Out[3]: 0.1000000000
In [4]: S.Zero + b
(0, mpz(54975581389), -39, 36) (0, mpz(0), 0, 0) (0, mpz(54975581389), -39,
36) 37
Out[4]: 0.1000000000
In [5]: _3 + (-a)
(0, mpz(54975581389), -39, 36) (1, mpz(3602879701896397), -55, 52) (0,
mpz(13107), -55, 14) 37
Out[5]: 3.637923296e-13
In [6]: _4 + (-b)
(0, mpz(54975581389), -39, 36) (1, mpz(54975581389), -39, 36) (0, mpz(0),
0, 0) 37
Out[6]: 0
Also 0.1 isn't represented exactly:
In [7]: 0.1
Out[7]: 0.10000000000000001
So, based on this I think that the bug is in Float/mpf constructor, which
doesn't respect precision (dps) (btw. numbers.py mix prec with dps all the
time). When adding S.Zero precision is applied and later lhs != rhs in a' -
a.
Apparently solution is simple:
diff --git a/sympy/core/numbers.py b/sympy/core/numbers.py
index c839043..9ed02db 100644
--- a/sympy/core/numbers.py
+++ b/sympy/core/numbers.py
@@ -313,7 +313,7 @@ def __new__(cls, num, prec=15):
_mpf_ = mpmath.mpf(
S.NegativeOne ** num[0] * num[1] * 2 ** num[2])._mpf_
else:
- _mpf_ = mpmath.mpf(num)._mpf_
+ _mpf_ = mlib.from_float(num, prec, rnd)
if not num:
return C.Zero()
obj = Expr.__new__(cls)
I will run tests and if all pass submit a pull request.
--
You received this message because you are subscribed to the Google Groups
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy-issues?hl=en.