commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henri Yandell <bay...@generationjava.com>
Subject Re: [lang] Math class
Date Thu, 22 Aug 2002 07:19:22 GMT

I agree that sum should return a long. Do we need to keep the >> 1 or can
we use a /2? I'm under the impression that there is no longer any
difference in modern Java compiling.

The name feels a little weird as I can see a developer expecting to have a
sum(int[]) as well.

I'll have to puzzle through sum2 as an exercise and figure out why it's
the same as sum :) My byte-manipulation experience is only at the sum
level. Anyone know of a site dedicated to byte-code manipulation
mathematical algorithms?

Hen

On Wed, 21 Aug 2002, Mika Riekkinen wrote:

> My initial post was just start discussion on this
> topic.
> As you all pointed out, this could evolve into
> something 'big'. To start with, here's a sample code
> I had in mind:
>
>     public static int sum(int n) {
>         long j = (n+1) * n;
>
>         return (int) j >> 1;
>     }
>
> Which calculates 1+2+...+n in constant time.
> Alternative, with different characteristics:
>
>     public static int sum2(int n) {
>         int j = n + 1;
>
>         return ((j>>1) * (n|1));
>     }
>
> Of course, these samples could be added to
> suit other types as well. Like Integer.
> The same is with other methods. average(...),
> median(...), ....
>
> NumberUtils would be a good place to start with,
> since it allready has similar methods.
>
> Both of the samples carry a little bug with them;
> They'll overflow at some point. Signature could
> be changed to 'long sum(int)' to prevent overflows.
>
> - mcr70 -
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message