This repository was archived by the owner on Jan 30, 2023. It is now read-only.
File tree 1 file changed +14
-9
lines changed
1 file changed +14
-9
lines changed Original file line number Diff line number Diff line change @@ -1184,6 +1184,12 @@ cdef class Expression(CommutativeRingElement):
1184
1184
1.54308063481524
1185
1185
sage: float(cos(I))
1186
1186
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...
1187
1193
"""
1188
1194
cdef GEx res
1189
1195
try :
@@ -1200,7 +1206,13 @@ cdef class Expression(CommutativeRingElement):
1200
1206
raise err
1201
1207
res = self ._gobj.evalf(0 , {' parent' :R_complex})
1202
1208
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)
1204
1216
else :
1205
1217
raise TypeError (" Cannot evaluate symbolic expression to a numeric value." )
1206
1218
@@ -1395,14 +1407,7 @@ cdef class Expression(CommutativeRingElement):
1395
1407
try :
1396
1408
return float (self ._eval_self(float ))
1397
1409
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" )
1406
1411
1407
1412
def __complex__ (self ):
1408
1413
"""
You can’t perform that action at this time.
0 commit comments