Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is the command to do a power of a value

18 views
Skip to first unread message

xvicto...@gmail.com

unread,
Jul 30, 2012, 12:28:11 AM7/30/12
to
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?

glen herrmannsfeldt

unread,
Jul 30, 2012, 12:31:35 AM7/30/12
to
Math.pow().

-- glen

xvicto...@gmail.com

unread,
Jul 30, 2012, 12:57:48 AM7/30/12
to
Thanks!

Roedy Green

unread,
Jul 30, 2012, 5:22:55 AM7/30/12
to
On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvicto...@gmail.com
wrote, quoted or indirectly quoted someone who said :
see http://mindprod.com/jgloss/power.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
http://www.youtube.com/watch?v=F-QA2rkpBSY


bugbear

unread,
Jul 30, 2012, 5:44:32 AM7/30/12
to
I have a dim memory from Numerical Analysis
that the "obvious" way to evaluate that is
inaccurate for small "i" and large "n".

BugBear

Andreas Leitgeb

unread,
Jul 30, 2012, 9:22:30 AM7/30/12
to
On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvicto...@gmail.com wrote:
> 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%28double%29

Patricia Shanahan

unread,
Jul 30, 2012, 10:48:47 AM7/30/12
to
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. Results must
be semi-monotonic." (From the pow description). Or do you know of cases
where Math.pow gets an over-large rounding error?

Note that I am not disagreeing with your method for calculating a power
of a number slightly greater than 1, just questioning whether doing it
explicitly gets more precise answers than using Math.pow.

Patricia

bugbear

unread,
Jul 30, 2012, 3:00:55 PM7/30/12
to
To: xvictoryeohx
From: bugbear <bugbear@trim_papermule.co.uk_trim>

xvicto...@gmail.com wrote:
> 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?

I have a dim memory from Numerical Analysis that the "obvious" way to evaluate
that is inaccurate for small "i" and large "n".

BugBear

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

xvictoryeohx

unread,
Jul 30, 2012, 3:00:54 PM7/30/12
to
From: xvicto...@gmail.com

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?

glen herrmannsfeldt

unread,
Jul 30, 2012, 3:00:55 PM7/30/12
to
To: xvictoryeohx
From: glen herrmannsfeldt <g...@ugcs.caltech.edu>

xvicto...@gmail.com wrote:
> 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?

Math.pow().

-- glen

xvictoryeohx

unread,
Jul 30, 2012, 3:00:55 PM7/30/12
to
To: glen herrmannsfeldt
From: xvicto...@gmail.com

On Monday, July 30, 2012 12:31:35 PM UTC+8, glen herrmannsfeldt wrote:
> xvicto...@gmail.com wrote:
>
> > 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?
>
>
>
> Math.pow().
>
>
>
> -- glen

Thanks!

Roedy Green

unread,
Jul 30, 2012, 3:00:55 PM7/30/12
to
To: xvictoryeohx
From: Roedy Green <see_w...@mindprod.com.invalid>

On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvicto...@gmail.com wrote, quoted
or indirectly quoted someone who said :

>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?

see http://mindprod.com/jgloss/power.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the
exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
http://www.youtube.com/watch?v=F-QA2rkpBSY

Andreas Leitgeb

unread,
Jul 30, 2012, 3:00:57 PM7/30/12
to
To: Roedy Green
From: Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>

On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvicto...@gmail.com wrote:
> 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%28double%29

Patricia Shanahan

unread,
Jul 30, 2012, 3:00:57 PM7/30/12
to
To: Andreas Leitgeb
From: Patricia Shanahan <pa...@acm.org>

On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
> On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvicto...@gmail.com wrote:
>> 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%28double%2
9
>

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. Results must be
semi-monotonic." (From the pow description). Or do you know of cases where
Math.pow gets an over-large rounding error?

Note that I am not disagreeing with your method for calculating a power of a
number slightly greater than 1, just questioning whether doing it explicitly
gets more precise answers than using Math.pow.

Patricia

Patricia Shanahan

unread,
Jul 30, 2012, 4:15:16 PM7/30/12
to
I've thought about this some more, and I think I get it now.

If i is reasonably small compared to 100, the 1 + i/100 addition loses
several bits of precision.

Patricia

glen herrmannsfeldt

unread,
Jul 30, 2012, 6:26:15 PM7/30/12
to
Patricia Shanahan <patricia.shanahan@1:261/38.remove-rf4-this> wrote:

(snip, someone wrote)
>> x ^ n = exp ( log(x) * n ) | x = (1 + i/100)
>> = exp ( log( 1 + i/100 ) * n)
>> = exp ( log1p ( i/100 ) * n)

(snip)

> 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.
> Results must be semi-monotonic." (From the pow description).
> Or do you know of cases where Math.pow gets an over-large
> rounding error?

I didn't know about the log1p function. (Does Java have one?)

As x gets small log(1+x) loses precision. Consider:

log(1+1e-60)

The x87 instruction set has an instruction for evaluating log(1+x)
and one for evaluating exp(x-1). Most HLLs don't.

-- glen

Patricia Shanahan

unread,
Jul 30, 2012, 7:01:33 PM7/30/12
to
On 7/30/2012 3:26 PM, glen herrmannsfeldt wrote:
...
> I didn't know about the log1p function. (Does Java have one?)

It's in java.lang.Math, since 1.5.

Patricia

Andreas Leitgeb

unread,
Jul 30, 2012, 7:06:02 PM7/30/12
to
Patricia Shanahan <pa...@acm.org> wrote:
> On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
>> 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%28double%29
>
> 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.

Andreas Leitgeb

unread,
Jul 30, 2012, 7:11:49 PM7/30/12
to
glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> and one for evaluating exp(x-1).

That is exp(x)-1, not exp(x-1), just for the record ;-)

Andreas Leitgeb

unread,
Jul 31, 2012, 2:02:36 PM7/31/12
to
To: Patricia Shanahan
From: Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>

Patricia Shanahan <pa...@acm.org> wrote:
> On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
>> 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%28double%
29
>
> 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.

0 new messages