Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected truncation to integer when using evalf #13365

Open
knuesel opened this issue Sep 28, 2017 · 2 comments
Open

Unexpected truncation to integer when using evalf #13365

knuesel opened this issue Sep 28, 2017 · 2 comments
Labels
concrete core.evalf Wrong Result The output produced by SymPy is mathematically incorrect.

Comments

@knuesel
Copy link

knuesel commented Sep 28, 2017

Calling Sum(1.9**k/factorial(k), (k, 0, oo)).evalf() returns the value for e^1 instead of e^1.9. Calling Sum(x**k/factorial(k), (k, 0, oo)).doit().subs(x,1.9) however gives the expected result.

@jksuom
Copy link
Member

jksuom commented Sep 28, 2017

SymPy evaluates sums of hypergeometric series by using integer arithmetic. All numbers are truncated to integers by MPZ (hypsum). That will also affect factors like 1.9:

>>> from mpmath.libmp.backend import MPZ
>>> MPZ(1.9)
mpz(1)

To make use of non-integer factors the truncation should occur after the operation, not before:

        term = MPZ(term*func1(k - 1))
        term = MPZ(term/func2(k - 1))

@asmeurer asmeurer added concrete core.evalf Wrong Result The output produced by SymPy is mathematically incorrect. labels Sep 28, 2017
Mayer318 added a commit to FIT-CSE2410-A-Bombs/sympy that referenced this issue Nov 14, 2017
TheGluck added a commit to FIT-CSE2410-A-Bombs/sympy that referenced this issue Nov 14, 2017
cchaffin2013 added a commit to FIT-CSE2410-A-Bombs/sympy that referenced this issue Dec 11, 2017
cchaffin2013 added a commit to FIT-CSE2410-A-Bombs/sympy that referenced this issue Dec 11, 2017
@smichr
Copy link
Member

smichr commented Apr 17, 2018

Possible solution:

diff --git a/sympy/core/evalf.py b/sympy/core/evalf.py
index 0ce7def..5815c61 100644
--- a/sympy/core/evalf.py
+++ b/sympy/core/evalf.py
@@ -1127,11 +1127,10 @@ def hypsum(expr, n, start, prec):
         s = term
         k = 1
         while abs(term) > 5:
-            term *= MPZ(func1(k - 1))
-            term //= MPZ(func2(k - 1))
+            term *= func1(k - 1)/func2(k - 1)
             s += term
             k += 1
-        return from_man_exp(s, -prec)
+        return from_man_exp(MPZ(s), -prec)
     else:
         alt = g < 0
         if abs(g) < 1:
diff --git a/sympy/core/tests/test_evalf.py b/sympy/core/tests/test_evalf.py
index 27daed9..46de791 100644
--- a/sympy/core/tests/test_evalf.py
+++ b/sympy/core/tests/test_evalf.py
@@ -525,3 +525,9 @@ def test_issue_13098():
     assert ceiling(log(S('9.'+'9'*20), 10)) == 1
     assert floor(log(20 - S('9.'+'9'*20), 10)) == 1
     assert ceiling(log(20 - S('9.'+'9'*20), 10)) == 2
+
+
+def test_issue_13365():
+    s = Sum(1.9**x/factorial(x), (x, 0, oo))
+    a = S.Exp1**1.9
+    assert (s - a).evalf() < 1e-12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concrete core.evalf Wrong Result The output produced by SymPy is mathematically incorrect.
Projects
None yet
Development

No branches or pull requests

4 participants