You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Mika Riekkinen <mr...@yahoo.com> on 2002/08/21 21:00:32 UTC
[lang] Math class
Hi!
I was wondering if commons-lang would start a
new set of utilities; Math. This would be comparable
in spirit, to one found in java.lang.Math.
It could contain some math functions missing from
java.lang.Math counterpart. Something like
average(...), sum(...), etc.
__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Daniel Rall <dl...@finemaltcoding.com>.
"Stephen Colebourne" <sc...@btopenworld.com> writes:
> I think this is certainly a possibility. However I'm not sure that your two
> example methods prove the case. Both of these might be more applicable to
> [collection], as you are going to have to pass a collection in. I've not
> made up my mind though.
>
> Have you any more method possibilities, or even better source code ;-)
It's nice to find myself agreeing with Stephen. ;-)
--
Daniel Rall <dl...@finemaltcoding.com>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Stephen Colebourne <sc...@btopenworld.com>.
I think this is certainly a possibility. However I'm not sure that your two
example methods prove the case. Both of these might be more applicable to
[collection], as you are going to have to pass a collection in. I've not
made up my mind though.
Have you any more method possibilities, or even better source code ;-)
Stephen
----- Original Message -----
From: "Mika Riekkinen" <mr...@yahoo.com>
To: <co...@jakarta.apache.org>
Sent: Wednesday, August 21, 2002 8:00 PM
Subject: [lang] Math class
> Hi!
> I was wondering if commons-lang would start a
> new set of utilities; Math. This would be comparable
> in spirit, to one found in java.lang.Math.
>
> It could contain some math functions missing from
> java.lang.Math counterpart. Something like
> average(...), sum(...), etc.
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Mika Riekkinen <mr...@yahoo.com>.
> 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.
In my opinion, when we divide integer by 2, for
the sake of clarity, we should use >> operator.
3/2 == 1, with integer division.
3>>1 == 1, makes it more clear, what is going on.
> The name feels a little weird as I can see a
> developer expecting to have a
> sum(int[]) as well.
You are right. That's the only name I can think
for the method in question.
> I'll have to puzzle through sum2 as an exercise and
> figure out why it's
> the same as sum :) My byte-manipulation experience
Hint: multiplication and division are done in
differend order. Which by the way causes these
different characteristics I talked about.
If we would change the return value to long,
sum2 would be unncessary. (no overflows anymore)
__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Henri Yandell <ba...@generationjava.com>.
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: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Mika Riekkinen <mr...@yahoo.com>.
> The options I can think of immediately are:
>
> int
> float
> long
> double
> byte
> short
>
> Integer
> Float
> Long
> Double
> Byte
> Short
> Number
> BigDecimal
> BigInteger
>
We could minimize these by having classes you
suggested:
Decimal and WholeNumber(RealNumber???)
They would act as adapters for the purposes of
these math utilities.
For the Decimal:
public Decimal(Integer i);
public Decimal(Long l);
> 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 } );
true.
> 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
If we had these DecimalNumber and RealNumber
classes, it would reduce to intValue() || longValue()
which would result the same --> use longValue()
RealNumbers are a bit more tricky, since the precision
is different.
> So, am I right in thinking that there is nothing we
> can do to make
> overloading nicer?? Not without adding reflection
Yes and no. See above.
> 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:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Henri Yandell <ba...@generationjava.com>.
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) * (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: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Mika Riekkinen <mr...@yahoo.com>.
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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Henri Yandell <ba...@generationjava.com>.
http://jcp.org/jsr/detail/13.jsp New Decimalness in JDK 1.5
http://jcp.org/jsr/detail/84.jsp Floating point extensions
http://jcp.org/jsr/detail/108.jsp Units
http://www.vni.com/corner/garage/grande/ and
http://www.vni.com/corner/garage/grande/jsr/
My mistake in thinking the vni Complex number was a JSR, I think it's only
been applied to be a JSR, not accepted.
I'm thinking:
'Simple and usual' stuff goes into NumberUtils at the moment. Possibly
branching out into a MathUtils if enough are added.
Anything complicated is resisted unless good reason occurs. ie) Lang
shouldn't be trying to improve with a new type of BigDecimal class etc.
So to answer Mika's original question, yes it sounds good. What methods
would you like to submit and what structure do you have?
[ie) support int, double, BigDecimal, Number etc]
Hen
On Wed, 21 Aug 2002, Juozas Baliuka wrote:
>
> Yes, Math stuff is a big and I think it is better to have subprojects like
> "matrix", "graph", ... Some "simple and usual" stuff can be added to lang,
> but I do not have ideas about "simple" at this time.
> Is it possible to review some documents from "JSR" ?
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Juozas Baliuka <ba...@centras.lt>.
Yes, Math stuff is a big and I think it is better to have subprojects like
"matrix", "graph", ... Some "simple and usual" stuff can be added to lang,
but I do not have ideas about "simple" at this time.
Is it possible to review some documents from "JSR" ?
----- Original Message -----
From: "Henri Yandell" <ba...@generationjava.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Wednesday, August 21, 2002 9:14 PM
Subject: Re: [lang] Math class
>
> I don't see any theoretical reasons why not. It obeys the Proposal of
> Lang. However, I do have some devil's advocate negativeness:
>
> 1) Math stuff is a big sinkingsand of a project. Where do we draw a line?
> Common algorithms etc?
>
> 2) Who are we competing with. There has been a lot of work in providing
> standardised Math libraries, some of which are up for JSR's.
>
> 3) Which 'number' type do we support. int/float/etc. How overloaded would
> it all be. [this isn't so much a negative as an initial design decision].
>
> ...
>
> 1 is not a major worry. We would probably put the code either in
> NumberUtils or in commons.lang.math. Breaking this off into another
> project is possible if Math got enourmous.
>
> 2 has options. How quickly are the JSR's moving. JDK 1.5 will have some
> decimal enhancements, but I think it's mainly under the Class structure
> and not new methods.
> What licences are competitors using? If the Complex number options were
> privately licened [they're not. vnmath is public I think and a JSR I
> think] then having a Complex number class would be important.
>
> ...
>
> The examples you give could fit in NumberUtils [though Stephen has
> suggested Collections as well]. Minimum and Maximum are already there and
> simple things like average and sum would fit there. Until it got more
> powerful, I wouldn't envisage going more into Maths.
>
> But definitely not an area that is an immediat No for Lang. Just one to
> convince us on :)
>
> Hen
>
> On Wed, 21 Aug 2002, Mika Riekkinen wrote:
>
> > Hi!
> > I was wondering if commons-lang would start a
> > new set of utilities; Math. This would be comparable
> > in spirit, to one found in java.lang.Math.
> >
> > It could contain some math functions missing from
> > java.lang.Math counterpart. Something like
> > average(...), sum(...), etc.
> >
> > __________________________________________________
> > Do You Yahoo!?
> > HotJobs - Search Thousands of New Jobs
> > http://www.hotjobs.com
> >
> > --
> > To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> > For additional commands, e-mail:
<ma...@jakarta.apache.org>
> >
> >
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang] Math class
Posted by Henri Yandell <ba...@generationjava.com>.
I don't see any theoretical reasons why not. It obeys the Proposal of
Lang. However, I do have some devil's advocate negativeness:
1) Math stuff is a big sinkingsand of a project. Where do we draw a line?
Common algorithms etc?
2) Who are we competing with. There has been a lot of work in providing
standardised Math libraries, some of which are up for JSR's.
3) Which 'number' type do we support. int/float/etc. How overloaded would
it all be. [this isn't so much a negative as an initial design decision].
...
1 is not a major worry. We would probably put the code either in
NumberUtils or in commons.lang.math. Breaking this off into another
project is possible if Math got enourmous.
2 has options. How quickly are the JSR's moving. JDK 1.5 will have some
decimal enhancements, but I think it's mainly under the Class structure
and not new methods.
What licences are competitors using? If the Complex number options were
privately licened [they're not. vnmath is public I think and a JSR I
think] then having a Complex number class would be important.
...
The examples you give could fit in NumberUtils [though Stephen has
suggested Collections as well]. Minimum and Maximum are already there and
simple things like average and sum would fit there. Until it got more
powerful, I wouldn't envisage going more into Maths.
But definitely not an area that is an immediat No for Lang. Just one to
convince us on :)
Hen
On Wed, 21 Aug 2002, Mika Riekkinen wrote:
> Hi!
> I was wondering if commons-lang would start a
> new set of utilities; Math. This would be comparable
> in spirit, to one found in java.lang.Math.
>
> It could contain some math functions missing from
> java.lang.Math counterpart. Something like
> average(...), sum(...), etc.
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>