Ask Your Question
1

Why is math.exp or math.sin used?

asked 2017-05-18 23:03:34 -0500

cybervigilante gravatar image

I saw a small function that used math.

def f(x,y): return math.exp(x/5)*math.sin(y)

I removed math. from the terms and it worked just the same. What is the purpose of math. ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-19 01:52:11 -0500

tmonteil gravatar image

updated 2017-05-19 01:52:40 -0500

When you write:

sage: math.exp(2)
7.38905609893065

you rely on the exp function provided by Python math numerical library.

When you write:

sage: exp(2)
e^2

you rely on the Sage exp function, which by default returns a symbolic expressions. Tou can get its numerical value (with arbitrary precision) as:

sage: exp(2).n()
7.38905609893065
sage: exp(2).n(digits=100)
7.389056098930650227230427460575007813180315570551847324087127822522573796079057763384312485079121795
edit flag offensive delete link more

Comments

I assume the Python library is faster for basic numerical work? But emphasis on the arbitrary precision!!!

kcrisman gravatar imagekcrisman ( 2017-05-19 06:05:42 -0500 )edit

yes, and for performance aspects there are technical remarks and neat comparisons in this thread.

mforets gravatar imagemforets ( 2017-05-20 13:49:11 -0500 )edit

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: 2017-05-18 23:03:34 -0500

Seen: 37 times

Last updated: May 19