ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 12 Jul 2020 03:12:28 +0200exact computations with algebraic numbershttps://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/I'm trying to do exact computations with algebraic numbers. In particular, I know to expect integer answers like `1`, `0`, and `3` at the end of my computations, but I'm getting something slightly off.
I noticed that if I run
sage: r = sqrt(2)
sage: a = AA(r)
sage: b = AA(1/r)
sage: c = a*b
Then I get:
sage: c
1.000000000000000?
Is this being handled in the computer as exactly `1`? Otherwise, how can I do exact computations with algebraic numbers in Sage? I obtain the algebraic numbers I'm working with using `algdep()` to find a polynomial and `.roots(QQbar)` to find the roots of that polynomial.Mon, 06 Jul 2020 21:18:17 +0200https://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/Answer by rburing for <p>I'm trying to do exact computations with algebraic numbers. In particular, I know to expect integer answers like <code>1</code>, <code>0</code>, and <code>3</code> at the end of my computations, but I'm getting something slightly off.</p>
<p>I noticed that if I run</p>
<pre><code>sage: r = sqrt(2)
sage: a = AA(r)
sage: b = AA(1/r)
sage: c = a*b
</code></pre>
<p>Then I get:</p>
<pre><code>sage: c
1.000000000000000?
</code></pre>
<p>Is this being handled in the computer as exactly <code>1</code>? Otherwise, how can I do exact computations with algebraic numbers in Sage? I obtain the algebraic numbers I'm working with using <code>algdep()</code> to find a polynomial and <code>.roots(QQbar)</code> to find the roots of that polynomial.</p>
https://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/?answer=52363#post-id-52363Yes, it is exact. To check:
sage: c.minpoly() # minimal polynomial over QQ
x - 1
sage: c.radical_expression()
1Mon, 06 Jul 2020 22:23:25 +0200https://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/?answer=52363#post-id-52363Answer by slelievre for <p>I'm trying to do exact computations with algebraic numbers. In particular, I know to expect integer answers like <code>1</code>, <code>0</code>, and <code>3</code> at the end of my computations, but I'm getting something slightly off.</p>
<p>I noticed that if I run</p>
<pre><code>sage: r = sqrt(2)
sage: a = AA(r)
sage: b = AA(1/r)
sage: c = a*b
</code></pre>
<p>Then I get:</p>
<pre><code>sage: c
1.000000000000000?
</code></pre>
<p>Is this being handled in the computer as exactly <code>1</code>? Otherwise, how can I do exact computations with algebraic numbers in Sage? I obtain the algebraic numbers I'm working with using <code>algdep()</code> to find a polynomial and <code>.roots(QQbar)</code> to find the roots of that polynomial.</p>
https://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/?answer=52434#post-id-52434Computations in algebraic numbers are costly so
Sage computes the product of `a` and `b` "lazily".
Some computations force it to realise the exact value of `c`.
Here are the one I would favour.
sage: c = AA(r) * AA(1/r)
sage: c
1.000000000000000?
sage: c.exactify()
sage: c
1
For the sake of completeness I also include those from @rburing's answer:
sage: c = AA(r) * AA(1/r)
sage: c
1.000000000000000?
sage: c.radical_expression()
1
sage: c = AA(r) * AA(1/r)
sage: c
1.000000000000000?
sage: c.minpoly()
x - 1
sage: c
1
Sun, 12 Jul 2020 03:12:28 +0200https://ask.sagemath.org/question/52362/exact-computations-with-algebraic-numbers/?answer=52434#post-id-52434