What is the command to do a power of a value

X

xvictoryeohx

C=L(1+i/100)power of n

i am stuck here
for example square root is Math.sqrt(x)
How do i do Power of a value?
 
A

Andreas Leitgeb

C=L(1+i/100)power of n

x ^ n = exp ( log(x) * n ) | x = (1 + i/100)
= exp ( log( 1 + i/100 ) * n)
= exp ( log1p ( i/100 ) * n)

If you're doing more calculations with same interest-rate but
different periods, then you may want to calculate
double logBase = Math.log1p( i / 100 );
once, and use that for the individual calculations:
C = L * Math.exp( logBase * n )

The gist of this response is, that for the kind of base (1+i/100),
you better separate the pow operation out into log and exp, and
actually use log1p on (i/100) instead of log on (1+i/100) for
efficiency's and precision's sake.

For "Math.log1p" see:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#log1p(double)
 
A

Andreas Leitgeb

Patricia Shanahan said:
I am curious about why you expect this to be more precise than: "The
computed result must be within 1 ulp of the exact result. ..."

Well, one ulp of i/100 is likely smaller than one ulp of 1+i/100
(at least it is for 0 <= i <= 100, the typical range for interest
rates). It's like calculating sin(0.0) versus sin(Math.PI), where
sin() makes the same promise wrt precision up to an ulp.

If the OP had been interested in the interest value alone, i.e. in
I = L*( (1+i/100)^n ) - L
then using log1p() and expm1() probably would beat the precision of
pow() by, um, a few decimal digits, depending of course on the values
of i and n.

Anyway, I think it's good to know that log1p() and expm1() exist,
even if the example at hand doesn't now seem to cry out for them
as loudly as I thought it did on first read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top