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, 18 Jan 2021 18:17:58 +0100Rounding the values of a mappinghttps://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/The problem is only with the `round(, 2)` code
f(x)=round((1/add([10, 20 ,30]))*x,2)
percent_votes_cand=list(map(f,[10, 20 ,30]))
percent_votes_cand
This gives an error since `round()` couldn't be applied to a theoretical argument. The following code works but I want to obtain truncated to 2 digits percentages not elements of QFri, 15 Jan 2021 15:11:13 +0100https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/Comment by slelievre for <p>The problem is only with the <code>round(, 2)</code> code</p>
<pre><code>f(x)=round((1/add([10, 20 ,30]))*x,2)
percent_votes_cand=list(map(f,[10, 20 ,30]))
percent_votes_cand
</code></pre>
<p>This gives an error since <code>round()</code> couldn't be applied to a theoretical argument. The following code works but I want to obtain truncated to 2 digits percentages not elements of Q</p>
https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?comment=55285#post-id-55285Can you say what result you get and what result you would prefer to get?Fri, 15 Jan 2021 15:56:00 +0100https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?comment=55285#post-id-55285Comment by Cyrille for <p>The problem is only with the <code>round(, 2)</code> code</p>
<pre><code>f(x)=round((1/add([10, 20 ,30]))*x,2)
percent_votes_cand=list(map(f,[10, 20 ,30]))
percent_votes_cand
</code></pre>
<p>This gives an error since <code>round()</code> couldn't be applied to a theoretical argument. The following code works but I want to obtain truncated to 2 digits percentages not elements of Q</p>
https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?comment=55343#post-id-55343Slelievre, I have corrected my question.Mon, 18 Jan 2021 17:59:02 +0100https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?comment=55343#post-id-55343Answer by tmonteil for <p>The problem is only with the <code>round(, 2)</code> code</p>
<pre><code>f(x)=round((1/add([10, 20 ,30]))*x,2)
percent_votes_cand=list(map(f,[10, 20 ,30]))
percent_votes_cand
</code></pre>
<p>This gives an error since <code>round()</code> couldn't be applied to a theoretical argument. The following code works but I want to obtain truncated to 2 digits percentages not elements of Q</p>
https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?answer=55344#post-id-55344You should not use a symbolic function for such purpose, symbolic functions should be understood as mathematical expressions, they are usefull if you want to view them as a mathematical object, for example if you want to derivate them.
Instead use a Python function, which is an object that returns a value given an entry.
There are two equivalent ways to define a Python function:
sage: def f(x):
....: return round(1/add([10,20,30]) * x, 2)
Or, the shorter
sage: f = lambda x : round(1/sum([10,20,30]) * x, 2)
In both cases, you can do:
sage: percent_votes_cand=list(map(f,[10, 20 ,30]))
sage: percent_votes_cand
[0.17, 0.33, 0.5]Mon, 18 Jan 2021 18:17:58 +0100https://ask.sagemath.org/question/55284/rounding-the-values-of-a-mapping/?answer=55344#post-id-55344