Ask Your Question

check if the result is a real number

asked 2012-05-04 12:39:24 +0200

disi gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-05-04 12:59:26 +0200

niles gravatar image

updated 2012-05-04 13:02:33 +0200

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.

edit flag offensive delete link more


Thanks so much :)

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

for self reference, this is what I got:

disi gravatar imagedisi ( 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

Robert Samal gravatar imageRobert Samal ( 2012-05-10 08:16:21 +0200 )edit

answered 2012-05-04 13:13:12 +0200

disi gravatar image

again, this works perfect :)

for a in maxSol:
    if a[x] in RR:
        print RealField(50)(a[x])
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-05-04 12:39:24 +0200

Seen: 3,024 times

Last updated: May 04 '12