Sam LaManna
Sam LaManna

Reputation: 425

Help with POW function in C++

m having trouble with the pow function. The equation is A=P*((1+R)/T)^T were "^" is to the power of. How would i use the pow function, i only see examples that are 4 to the 5th power, nothing like what i have to do

Upvotes: 1

Views: 456

Answers (5)

templatetypedef
templatetypedef

Reputation: 373102

The C++ function std::pow can be used as follows: if you write

std::pow(base, exponent)

the function computes baseexponent. In your case, because you want to compute

A = P * ((1 + R) / T) ^ T

You might call the function like this:

A = P * std::pow((1 + R) / T, T);

Note that the second argument needn't be a constant; it can be any value that you'd like, even one you don't know until runtime.

Hope this helps!

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882386

Either:

A = P * pow ((1 + R) / T, T)

or:

A = pow (P * (1 + R) / T, T)

depending on whether the power should include the multiplication by P or not.

I would suspect the former since exponentiation tends to have a higher priority in maths - i.e., 4x2 is 4(x2), not (4x)2.

Upvotes: 0

Mu Qiao
Mu Qiao

Reputation: 7107

Use man pow to see the usage

A = P * pow((1 + R) / T, T)

Upvotes: 0

Jason
Jason

Reputation: 32540

it would simply be A = pow((1+R)/T , T) * P;

Upvotes: 5

Ed Swangren
Ed Swangren

Reputation: 124770

P * pow((1+R)/T, T)

The second argument is the exponent (power) to raise the first argument to.

Upvotes: 1

Related Questions