# check if the result is a real number

I get several results and want to check, which one is a real number. From trial and error, I can see that maxSol[2][x] is the only plausable solution.

Is there something like isReal()?

my attempt (don't laugh) ;)

for i in maxSol: print RealField(50)(i)

where I obviously get: TypeError: Unable to convert x (='.....

edit retag close merge delete

Sort by ยป oldest newest most voted

I think "a in RR" is what you want to use. Your first attempt could work too, using a try/except block.

For testing integrality, or rationality, "in ZZ" and "in QQ" work similarly.

And if you know the numbers you're checking are complex, you could also test to see whether their imaginary parts are zero.

more

Thanks so much :)

( 2012-05-04 13:05:48 +0200 )edit

for self reference, this is what I got:

( 2012-05-04 13:12:16 +0200 )edit

It is not so easy: arccos(2) in RR is evaluated as true, while arccos(2).n() is NaN. The best we found with my student is a.n() != NaN and a.n().is_real() -- is this too complicated? See also http://bit.ly/LndAoI

( 2012-05-10 08:16:21 +0200 )edit

again, this works perfect :)

for a in maxSol:
if a[x] in RR:
print RealField(50)(a[x])

more

## Stats

Seen: 3,024 times

Last updated: May 04 '12