Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16603 > unrolled thread

What is the command to do a power of a value

Started byxvictoryeohx@gmail.com
First post2012-07-29 21:28 -0700
Last post2012-07-30 10:44 +0100
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  What is the command to do a power of a value xvictoryeohx@gmail.com - 2012-07-29 21:28 -0700
    Re: What is the command to do a power of a value glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-07-30 04:31 +0000
      Re: What is the command to do a power of a value xvictoryeohx@gmail.com - 2012-07-29 21:57 -0700
    Re: What is the command to do a power of a value Roedy Green <see_website@mindprod.com.invalid> - 2012-07-30 02:22 -0700
      Re: What is the command to do a power of a value Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-07-30 13:22 +0000
        Re: What is the command to do a power of a value Patricia Shanahan <pats@acm.org> - 2012-07-30 07:48 -0700
          Re: What is the command to do a power of a value Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-07-30 23:06 +0000
    Re: What is the command to do a power of a value bugbear <bugbear@trim_papermule.co.uk_trim> - 2012-07-30 10:44 +0100

#16603 — What is the command to do a power of a value

Fromxvictoryeohx@gmail.com
Date2012-07-29 21:28 -0700
SubjectWhat is the command to do a power of a value
Message-ID<05854398-bcfe-4de2-9eda-7b748239f025@googlegroups.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?

[toc] | [next] | [standalone]


#16604

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2012-07-30 04:31 +0000
Message-ID<jv52n6$td9$4@speranza.aioe.org>
In reply to#16603
xvictoryeohx@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

[toc] | [prev] | [next] | [standalone]


#16605

Fromxvictoryeohx@gmail.com
Date2012-07-29 21:57 -0700
Message-ID<033ca204-dd50-49e0-88c2-9cc1e20d6bfe@googlegroups.com>
In reply to#16604
On Monday, July 30, 2012 12:31:35 PM UTC+8, glen herrmannsfeldt wrote:
> xvictoryeohx@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!

[toc] | [prev] | [next] | [standalone]


#16606

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-30 02:22 -0700
Message-ID<lfkc18hncb6tnv6q66937v5re36onqfl3q@4ax.com>
In reply to#16603
On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvictoryeohx@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

[toc] | [prev] | [next] | [standalone]


#16613

FromAndreas Leitgeb <avl@gamma.logic.tuwien.ac.at>
Date2012-07-30 13:22 +0000
Message-ID<slrnk1d2km.u9l.avl@gamma.logic.tuwien.ac.at>
In reply to#16606
On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvictoryeohx@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

[toc] | [prev] | [next] | [standalone]


#16614

FromPatricia Shanahan <pats@acm.org>
Date2012-07-30 07:48 -0700
Message-ID<xcSdnUmqfrZJA4vNnZ2dnUVZ_uWdnZ2d@earthlink.com>
In reply to#16613
On 7/30/2012 6:22 AM, Andreas Leitgeb wrote:
> On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvictoryeohx@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
>

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

[toc] | [prev] | [next] | [standalone]


#16690

FromAndreas Leitgeb <avl@gamma.logic.tuwien.ac.at>
Date2012-07-30 23:06 +0000
Message-ID<slrnk1e4qq.u9l.avl@gamma.logic.tuwien.ac.at>
In reply to#16614
Patricia Shanahan <pats@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.

[toc] | [prev] | [next] | [standalone]


#16607

Frombugbear <bugbear@trim_papermule.co.uk_trim>
Date2012-07-30 10:44 +0100
Message-ID<1c2dnc6ylpMdyovNnZ2dnUVZ8tKdnZ2d@brightview.co.uk>
In reply to#16603
xvictoryeohx@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web