Before we move much on implementations, I'd like too focus on the minimal
types we can support for 99% of mathematical functions.
The options I can think of immediately are:
int
float
long
double
byte
short
Integer
Float
Long
Double
Byte
Short
Number
BigDecimal
BigInteger
[It would be nice if there was a 'Decimal' and a 'WholeNumber' that
extended Number and that were extended by the other classes].
However the Java language doens't do us many favours here. If I have:
average(long[]), I can't call it with average( new int[] { 1,2,3 } );
Equally, if we have average(Number[]), then we get the correct inheritence
characteristics. However we've no clue as to whether it is 'WholeNumber'/Z
or 'Decimal'/R, so do we use longValue or doubleValue for calculations.
Also, Number average(Number[]) has to figure out what type to use for the
return type, whch will be a pain.
So, am I right in thinking that there is nothing we can do to make
overloading nicer?? Not without adding reflection and thus a significant
performance hit [algorithms normally needing to be performant].
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) * (n1));
> }
>
> 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, email: <mailto:commonsdevunsubscribe@jakarta.apache.org>
> For additional commands, email: <mailto:commonsdevhelp@jakarta.apache.org>
>
>

To unsubscribe, email: <mailto:commonsdevunsubscribe@jakarta.apache.org>
For additional commands, email: <mailto:commonsdevhelp@jakarta.apache.org>
