Ask Your Question
0

Rounding the values of a mapping

asked 2021-01-15 15:11:13 +0100

Cyrille gravatar image

updated 2021-01-18 17:58:41 +0100

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 Q

edit retag flag offensive close merge delete

Comments

Can you say what result you get and what result you would prefer to get?

slelievre gravatar imageslelievre ( 2021-01-15 15:56:00 +0100 )edit

Slelievre, I have corrected my question.

Cyrille gravatar imageCyrille ( 2021-01-18 17:59:02 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-01-18 18:17:58 +0100

tmonteil gravatar image

updated 2021-01-18 18:18:52 +0100

You 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]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-01-15 15:11:13 +0100

Seen: 151 times

Last updated: Jan 18 '21