ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 15 Jul 2017 10:42:01 -0500How to test an identity of rational numbers?http://ask.sagemath.org/question/38274/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$?
Thanks for your support!
RolandSat, 15 Jul 2017 06:17:59 -0500http://ask.sagemath.org/question/38274/how-to-test-an-identity-of-rational-numbers/Answer by dan_fulea for <p>Hi, </p>
<p>Why is there a difference in output between:</p>
<pre><code> print 5-1 == 0
print 3^(1/3) - 3^(1/5) == 0
False
3^(1/3) - 3^(1/5) == 0
</code></pre>
<p>What can I do if $x,y,s,t \in \mathbb Q$, and I want to test $x^y == s^t$?</p>
<p>Thanks for your support!</p>
<p>Roland</p>
http://ask.sagemath.org/question/38274/how-to-test-an-identity-of-rational-numbers/?answer=38279#post-id-38279There 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.)
Sat, 15 Jul 2017 10:42:01 -0500http://ask.sagemath.org/question/38274/how-to-test-an-identity-of-rational-numbers/?answer=38279#post-id-38279Answer by vdelecroix for <p>Hi, </p>
<p>Why is there a difference in output between:</p>
<pre><code> print 5-1 == 0
print 3^(1/3) - 3^(1/5) == 0
False
3^(1/3) - 3^(1/5) == 0
</code></pre>
<p>What can I do if $x,y,s,t \in \mathbb Q$, and I want to test $x^y == s^t$?</p>
<p>Thanks for your support!</p>
<p>Roland</p>
http://ask.sagemath.org/question/38274/how-to-test-an-identity-of-rational-numbers/?answer=38275#post-id-38275The 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)Sat, 15 Jul 2017 06:28:05 -0500http://ask.sagemath.org/question/38274/how-to-test-an-identity-of-rational-numbers/?answer=38275#post-id-38275