# HG changeset patch
# User R. Andrew Ohana <andrew.ohana@gmail.com>
# Date 1338109552 25200
# Node ID a5953d6568d8e7209024eaa110d0185ed5f7cc5e
# Parent 5299aa8232d7acb68d37162f5bef2068c8a68d54
check to see if number field has a default embedding in _mpfr_
diff --git a/sage/rings/number_field/number_field_element.pyx b/sage/rings/number_field/number_field_element.pyx
a
|
b
|
|
1304 | 1304 | sage: RR(a) |
1305 | 1305 | Traceback (most recent call last): |
1306 | 1306 | ... |
1307 | | TypeError: cannot convert a to real number |
1308 | | |
| 1307 | TypeError: Unable to coerce a to a rational |
1309 | 1308 | sage: (a^2)._mpfr_(RR) |
1310 | 1309 | -1.00000000000000 |
| 1310 | |
| 1311 | Verify that :trac:`#13005` has been fixed:: |
| 1312 | |
| 1313 | sage: K.<a> = NumberField(x^2-5) |
| 1314 | sage: RR(K(1)) |
| 1315 | 1.00000000000000 |
| 1316 | sage: RR(a) |
| 1317 | Traceback (most recent call last): |
| 1318 | ... |
| 1319 | TypeError: Unable to coerce a to a rational |
| 1320 | sage: K.<a> = NumberField(x^3+2, embedding=-1.25) |
| 1321 | sage: RR(a) |
| 1322 | -1.25992104989487 |
| 1323 | sage: RealField(prec=100)(a) |
| 1324 | -1.2599210498948731647672106073 |
1311 | 1325 | """ |
1312 | | C = R.complex_field() |
1313 | | tres = C(self) |
1314 | | try: |
1315 | | return R(tres) |
1316 | | except TypeError: |
1317 | | raise TypeError, "cannot convert %s to real number"%(self) |
| 1326 | if self.parent().coerce_embedding() is None: |
| 1327 | return R(self.base_ring()(self)) |
| 1328 | else: |
| 1329 | return R(R.complex_field()(self)) |
1318 | 1330 | |
1319 | 1331 | def __float__(self): |
1320 | 1332 | """ |
… |
… |
|
1326 | 1338 | sage: float(a) |
1327 | 1339 | Traceback (most recent call last): |
1328 | 1340 | ... |
1329 | | TypeError: cannot convert a to real number |
1330 | | |
| 1341 | TypeError: Unable to coerce a to a rational |
1331 | 1342 | sage: (a^2).__float__() |
1332 | 1343 | -1.0 |
| 1344 | sage: k.<a> = NumberField(x^2 + 1,embedding=I) |
| 1345 | sage: float(a) |
| 1346 | Traceback (most recent call last): |
| 1347 | ... |
| 1348 | TypeError: unable to coerce to a real number |
1333 | 1349 | """ |
1334 | | tres = CC(self) |
1335 | | try: |
1336 | | return float(tres) |
1337 | | except TypeError: |
1338 | | raise TypeError, "cannot convert %s to real number"%(self) |
| 1350 | if self.parent().coerce_embedding() is None: |
| 1351 | return float(self.base_ring()(self)) |
| 1352 | else: |
| 1353 | c = complex(self) |
| 1354 | if c.imag == 0: |
| 1355 | return c.real |
| 1356 | raise TypeError('unable to coerce to a real number') |
1339 | 1357 | |
1340 | 1358 | def _complex_double_(self, CDF): |
1341 | 1359 | """ |