Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread Mark R. Diggory
J.Pietschmann wrote: Mark R. Diggory wrote: (1) It is very important to also use ((double)x)*x instead of (double)(x*x), as the loss of precision starts to occur at far greater values than overflow occurs if one were doing integer arithmetic IIRC Java shares also the C behaviour in that n*n b

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread Mark R. Diggory
J.Pietschmann wrote: Mark R. Diggory wrote: J.Pietschmann wrote: No. If you cast the base into a double there is not much risk of overflow: double x = n; y=x*x; or y=((double)n)*((double)n); or even y=n*(double)n; (but avoid y=(double)n*n). Double mantissa has IIRC 52 bits, this should be good

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread J.Pietschmann
Mark R. Diggory wrote: (1) It is very important to also use ((double)x)*x instead of (double)(x*x), as the loss of precision starts to occur at far greater values than overflow occurs if one were doing integer arithmetic IIRC Java shares also the C behaviour in that n*n becomes negative instead o

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread J.Pietschmann
Mark R. Diggory wrote: J.Pietschmann wrote: No. If you cast the base into a double there is not much risk of overflow: double x = n; y=x*x; or y=((double)n)*((double)n); or even y=n*(double)n; (but avoid y=(double)n*n). Double mantissa has IIRC 52 bits, this should be good for integers up to 2^26

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread J.Pietschmann
Phil Steitz wrote: I am also a little confused by J's analysis. Do we know definitively that using J2SE, there is precision loss in Math.pow(x,n) vs x*x*...*x (n terms) for small integer n? Actually no. I didn't know they use a specific library routine which is stunningly good. Some tests showed d*

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread Mark R. Diggory
Phil Steitz wrote: --- Brent Worden <[EMAIL PROTECTED]> wrote: I am also a little confused by J's analysis. Do we know definitively that using J2SE, there is precision loss in Math.pow(x,n) vs x*x*...*x (n terms) for small integer n? If the answer is yes, we should establish the guideline that

RE: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-19 Thread Phil Steitz
--- Brent Worden <[EMAIL PROTECTED]> wrote: > > > > I am also a little confused by J's analysis. Do we know > > definitively that using > > J2SE, there is precision loss in Math.pow(x,n) vs x*x*...*x (n > > terms) for small > > integer n? If the answer is yes, we should establish the > > guidelin

Re: [math] Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread Phil Steitz
--- "Mark R. Diggory" <[EMAIL PROTECTED]> wrote: > Thanks for entertaining my sometimes naive questioning, > > J.Pietschmann wrote: > > > Mark R. Diggory wrote: > > > >> (1) Does it seem logical that when working with "n" (or > >> values.length) to use Math.pow(n, x), as positive integers, the

Re: Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread Mark R. Diggory
Thanks for entertaining my sometimes naive questioning, J.Pietschmann wrote: Mark R. Diggory wrote: (1) Does it seem logical that when working with "n" (or values.length) to use Math.pow(n, x), as positive integers, the risk is actually 'integer overflow' when the array representing the number

Re: Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread Mark R. Diggory
Al Chou wrote: --- "Mark R. Diggory" <[EMAIL PROTECTED]> wrote: (1) Does it seem logical that when working with "n" (or values.length) to use Math.pow(n, x), as positive integers, the risk is actually 'integer overflow' when the array representing the number of cases gets very large, for whi

Re: Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread Al Chou
--- "Mark R. Diggory" <[EMAIL PROTECTED]> wrote: > (1) Does it seem logical that when working with "n" (or values.length) > to use Math.pow(n, x), as positive integers, the risk is actually > 'integer overflow' when the array representing the number of cases gets > very large, for which the log

Re: Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread Mark R. Diggory
(1) Does it seem logical that when working with "n" (or values.length) to use Math.pow(n, x), as positive integers, the risk is actually 'integer overflow' when the array representing the number of cases gets very large, for which the log implementation of Math.pow would help retain greater num

Math.pow usage was: Re: cvs commit: ...

2003-06-18 Thread J.Pietschmann
accum += Math.pow(values[i], 2.0); I think Math.pow should not be used for small integer exponents, in particular 2,3 and perhaps 5. You could do this in FORTRAN or other languages with a built-in power operator, because the compiler will optimize it, but languages without that usually rewrite pos(