Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit b06cfa0

Browse files
committed
Fix _eval_self(float) for "real" complex expressions
1 parent 0065f62 commit b06cfa0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/sage/symbolic/expression.pyx

+14-9
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,12 @@ cdef class Expression(CommutativeRingElement):
11841184
1.54308063481524
11851185
sage: float(cos(I))
11861186
1.5430806348152437
1187+
1188+
TESTS::
1189+
1190+
sage: e = sqrt(2)/sqrt(abs(-(I - 1)*sqrt(2) - I - 1))
1191+
sage: e._eval_self(float)
1192+
0.9036020036...
11871193
"""
11881194
cdef GEx res
11891195
try:
@@ -1200,7 +1206,13 @@ cdef class Expression(CommutativeRingElement):
12001206
raise err
12011207
res = self._gobj.evalf(0, {'parent':R_complex})
12021208
if is_a_numeric(res):
1203-
return R(py_object_from_numeric(res))
1209+
ans = py_object_from_numeric(res)
1210+
# Convert ans to R.
1211+
if R is float and isinstance(ans, complex) and not ans.imag:
1212+
# Python does not automatically convert "real" complex
1213+
# numbers to floats, so we do this manually:
1214+
ans = ans.real
1215+
return R(ans)
12041216
else:
12051217
raise TypeError("Cannot evaluate symbolic expression to a numeric value.")
12061218

@@ -1395,14 +1407,7 @@ cdef class Expression(CommutativeRingElement):
13951407
try:
13961408
return float(self._eval_self(float))
13971409
except TypeError:
1398-
try:
1399-
c = complex(self._eval_self(complex))
1400-
if c.imag == 0:
1401-
return c.real
1402-
else:
1403-
raise
1404-
except TypeError:
1405-
raise TypeError("unable to simplify to float approximation")
1410+
raise TypeError("unable to simplify to float approximation")
14061411

14071412
def __complex__(self):
14081413
"""

0 commit comments

Comments
 (0)