Ask Your Question
1

Why is math.exp or math.sin used?

asked 2017-05-19 06:03:34 +0100

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 08:52:11 +0100

tmonteil gravatar image

updated 2017-05-19 08:52:40 +0100

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 13:05:42 +0100 )edit

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

mforets gravatar imagemforets ( 2017-05-20 20:49:11 +0100 )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-19 06:03:34 +0100

Seen: 277 times

Last updated: May 19 '17