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.Mon, 25 Jun 2012 12:44:47 +0200Rounding problems in sagetex. Sometimeshttps://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/I was getting wrong answers with Sage. It came down to this:
\documentclass[12pt]{article}%
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
c = randint(14,20)
d = randint(5,10)
\end{sagesilent}
$\frac{\sage{c}}{\sage{d}}-\frac{\sage{c-3}}{\sage{d+1}}=$ \\
%$\sage{c/d-((c-3)/(d+1))}$\\
$\sage{c/d}$\\
$\sage{((c-3)/(d+1))}$\\
$\sage{c/d-((c-3)/(d+1))}$
\end{document}
When I compiled with the commented out line I got the wrong answer, so I commented it out and broke it down into the 3 calculations below it. Output is below: for 20/7 - 17/8, Sage calculates 20/7 as 2 (rounding down) but 17/8 as 17/8 (not rounding down) to get a final answer of 2-17/8=-1/8. Image of output below. Why does it round (down) in one case and not in the others? What is the proper way to get the correct fractional answer?
![image description](/upfiles/13405905658419095.jpeg)Sun, 24 Jun 2012 23:24:50 +0200https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/Answer by DSM for <p>I was getting wrong answers with Sage. It came down to this:</p>
<pre><code>\documentclass[12pt]{article}%
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
c = randint(14,20)
d = randint(5,10)
\end{sagesilent}
$\frac{\sage{c}}{\sage{d}}-\frac{\sage{c-3}}{\sage{d+1}}=$ \\
%$\sage{c/d-((c-3)/(d+1))}$\\
$\sage{c/d}$\\
$\sage{((c-3)/(d+1))}$\\
$\sage{c/d-((c-3)/(d+1))}$
\end{document}
</code></pre>
<p>When I compiled with the commented out line I got the wrong answer, so I commented it out and broke it down into the 3 calculations below it. Output is below: for 20/7 - 17/8, Sage calculates 20/7 as 2 (rounding down) but 17/8 as 17/8 (not rounding down) to get a final answer of 2-17/8=-1/8. Image of output below. Why does it round (down) in one case and not in the others? What is the proper way to get the correct fractional answer?</p>
<p><img alt="image description" src="/upfiles/13405905658419095.jpeg"/></p>
https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?answer=13755#post-id-13755This isn't a sagetex-related issue: it's because randint returns a Python int:
sage: c = randint(14, 20)
sage: d = randint(5, 10)
sage: c
17
sage: d
7
sage: type(c), type(d)
(<type 'int'>, <type 'int'>)
and division of Python ints is truncating:
sage: c / d
2
The reason that the second one behaves is because the 3 and 1 in `c-3` and `d+1` are Sage Integers, and operations involving them and Python ints produce Sage objects (Integers, Rationals, etc), so things work the way they should:
sage: (c-3)/(d+1)
7/4
sage: type(_)
<type 'sage.rings.rational.Rational'>
If you wrap the `randint` call in `Integer`, it should behave, although I can't do a sagetex test at the moment:
sage: c = Integer(randint(14, 20))
sage: c
15
sage: type(c), parent(c)
(<type 'sage.rings.integer.Integer'>, Integer Ring)
Mon, 25 Jun 2012 00:35:38 +0200https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?answer=13755#post-id-13755Comment by dazedANDconfused for <p>This isn't a sagetex-related issue: it's because randint returns a Python int:</p>
<pre><code>sage: c = randint(14, 20)
sage: d = randint(5, 10)
sage: c
17
sage: d
7
sage: type(c), type(d)
(<type 'int'>, <type 'int'>)
</code></pre>
<p>and division of Python ints is truncating:</p>
<pre><code>sage: c / d
2
</code></pre>
<p>The reason that the second one behaves is because the 3 and 1 in <code>c-3</code> and <code>d+1</code> are Sage Integers, and operations involving them and Python ints produce Sage objects (Integers, Rationals, etc), so things work the way they should:</p>
<pre><code>sage: (c-3)/(d+1)
7/4
sage: type(_)
<type 'sage.rings.rational.Rational'>
</code></pre>
<p>If you wrap the <code>randint</code> call in <code>Integer</code>, it should behave, although I can't do a sagetex test at the moment:</p>
<pre><code>sage: c = Integer(randint(14, 20))
sage: c
15
sage: type(c), parent(c)
(<type 'sage.rings.integer.Integer'>, Integer Ring)
</code></pre>
https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?comment=19517#post-id-19517I tested it in sagetex and it works, Thanks! +1.Mon, 25 Jun 2012 00:52:34 +0200https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?comment=19517#post-id-19517Comment by kcrisman for <p>This isn't a sagetex-related issue: it's because randint returns a Python int:</p>
<pre><code>sage: c = randint(14, 20)
sage: d = randint(5, 10)
sage: c
17
sage: d
7
sage: type(c), type(d)
(<type 'int'>, <type 'int'>)
</code></pre>
<p>and division of Python ints is truncating:</p>
<pre><code>sage: c / d
2
</code></pre>
<p>The reason that the second one behaves is because the 3 and 1 in <code>c-3</code> and <code>d+1</code> are Sage Integers, and operations involving them and Python ints produce Sage objects (Integers, Rationals, etc), so things work the way they should:</p>
<pre><code>sage: (c-3)/(d+1)
7/4
sage: type(_)
<type 'sage.rings.rational.Rational'>
</code></pre>
<p>If you wrap the <code>randint</code> call in <code>Integer</code>, it should behave, although I can't do a sagetex test at the moment:</p>
<pre><code>sage: c = Integer(randint(14, 20))
sage: c
15
sage: type(c), parent(c)
(<type 'sage.rings.integer.Integer'>, Integer Ring)
</code></pre>
https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?comment=19512#post-id-19512Hmm. At first I thought this should be fixed. But now I'm not so sure, since it calls the Python random stuff more or less directly. Worth raising on sage-devel?Mon, 25 Jun 2012 12:44:47 +0200https://ask.sagemath.org/question/9107/rounding-problems-in-sagetex-sometimes/?comment=19512#post-id-19512