# How to test an identity of rational numbers?

Hi,

Why is there a difference in output between:

 print 5-1 == 0
print 3^(1/3) - 3^(1/5) == 0

False
3^(1/3) - 3^(1/5) == 0


What can I do if $x,y,s,t \in \mathbb Q$, and I want to test $x^y == s^t$?

Roland

edit retag close merge delete

Sort by » oldest newest most voted

The difference you noticed comes from the fact that 5 and 3^(1/3) have different natures

sage: parent(5)
Integer Ring
sage: parent(3^(1/3))
Symbolic Ring


Comparisons between integers (and more generally rationals) is very different from comparisons between symbolic elements. In order to test x^y == s^t you would better assume that bot y and t are integers (that you can always do by taken appropriate powers). Assuming that you only care about positive numbers you can use

def test_equality(x, y, s, t):
m = lcm(y.denominator(), t.denominator())
return x^(y*m) == s^(t*m)

more

There is also a difference between the level of evaluation of the two expressions. The sage (man) sees 5-1, and thinks mayby it is better to write a 4 immediately, then seeing 4 == 0... and in the other case thinks, let it be as it comes...

If a True or a False is needed, then we have to require in the second case an explicit evaluation.

sage:  print 3^(1/3) - 3^(1/5) == 0
3^(1/3) - 3^(1/5) == 0
sage:  print bool( 3^(1/3) - 3^(1/5) == 0 )
False


Note also how the sage interpreter reshapes expressions in the following cases:

sage:  print 4 == 0
False
sage:  print 3^(1/5) - 3^(1/5) == 0
0 == 0
sage:  print bool( 3^(1/5) - 3^(1/5) ) == 0
True

sage:  print type( 3^(1/5) - 3^(1/5) )
<type 'sage.symbolic.expression.Expression'>
sage: print type(0)
<type 'sage.rings.integer.Integer'>

sage: print 98^(1/2) - 8^(1/6) == 5184^(1/4)
7*sqrt(2) - 8^(1/6) == 6*4^(1/4)
sage: print bool( 98^(1/2) - 8^(1/6) == 5184^(1/4) )
True


Note: This answer differs only psychologically from the above one. (Please let it above.)

more