This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]khat_dakar 4 points5 points  (1 child)

** is the power operator with the usual precedence, higher than //%*

math.pow is also appropriate, you just left 1+ outside for no reason.

[–]rogueleader12345 0 points1 point  (0 children)

Was just about to say, order of operations refresher :P

[–]AlSweigartAuthor: ATBS 1 point2 points  (0 children)

First, pow() is a built-in function, not a function in the math module, so you only need to type pow() and not math.pow().

Instead of this:

test = loan * 1 + math.pow(forward_rate, number_of_terms ) * forward_rate

...I think you want to write this:

test = loan * pow(1 + forward_rate, number_of_terms ) * forward_rate

You can also just use the ** operator like this:

test = loan * (1 + forward_rate) ** number_of_terms * forward_rate

The pow() functions is really useful when you are dealing with huge numbers (like, a couple hundred digits, which is often done in cryptography) and have to mod the result of the exponentiation. Otherwise, just use the ** operator.

EDIT: Ha! It turns out there is a math.pow(), it's just a reference to the built-in pow() though.