java.lang.Rational ...

A

Albretch Mueller

~
Since JDK 1.0 java.lang.Number has been subclassed by:
~
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
why hasn't sun included "Rational" as one of the subclasses? (and it
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't ever
do so)
~
why not java.lang.Rational? It naturally fits within the Number class
hierarchy!
~
thanks
lbrtchx
 
E

Eric Sosman

~
Since JDK 1.0 java.lang.Number has been subclassed by:
~
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
why hasn't sun included "Rational" as one of the subclasses? (and it
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't ever
do so)
~
why not java.lang.Rational? It naturally fits within the Number class
hierarchy!

Also java.lang.Complex, java.lang.Quaternion, ...

FWIW, I've written my own Fraction class using pairs of
BigIntegers. Drop me a line if you'd like the source. (It's
got two known bugs, or one bug twice: The floatValue() and
doubleValue() methods round incorrectly when the value is so
small as to be a denormal number. I've never gotten around
to fixing this, but if you'd like to volunteer ...)
 
L

Lew

~
 Since JDK 1.0 java.lang.Number has been subclassed by:
~
 BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
 why hasn't sun [sic] included "Rational" as one of the subclasses? (and it

Ask them.

People here can speculate, but few if any will know.
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't ever
do so)
~
 why not java.lang.Rational? It naturally fits within the Number class
hierarchy!

So it's Sun's job to write every class you'll ever need? If they did
that, how would you justify your high programmer's salary?
 
M

Mike Schilling

Eric said:
Also java.lang.Complex, java.lang.Quaternion, ...

Transcendental t1 = Math.PI;
Transcendental t2 = t1 - t1; // throws an exception, of course
 
J

John B. Matthews

Albretch Mueller said:
~
Since JDK 1.0 java.lang.Number has been subclassed by:
~
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
why hasn't sun included "Rational" as one of the subclasses? (and it
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't
ever do so)
~
why not java.lang.Rational? It naturally fits within the Number class
hierarchy!

One problem is choosing a representation for the components that
balances range versus efficiency. This one is a decent compromise:

<http://jscience.org/api/org/jscience/mathematics/number/Rational.html>
 
T

Tom Anderson

~
 Since JDK 1.0 java.lang.Number has been subclassed by:
~
 BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
 why hasn't sun [sic] included "Rational" as one of the subclasses? (and it

Ask them.

People here can speculate, but few if any will know.
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't ever
do so)
~
 why not java.lang.Rational? It naturally fits within the Number class
hierarchy!

So it's Sun's job to write every class you'll ever need? If they did
that, how would you justify your high programmer's salary?

Configuration management!

tom
 
A

Albretch Mueller

why not java.lang.Rational? It naturally fits within the Number class
One problem is choosing a representation for the components that
balances range versus efficiency. This one is a decent compromise:

<http://jscience.org/api/org/jscience/mathematics/number/Rational.html>

Hey John

have you used that library yourself?

Have you never needed a method such as boolean
org.jscience.mathematics.number.Rational.reduce()?

I found a little weird that such a library does not have such a
method!

I for one would need such a method

lbrtchx
 
L

Lew

(citation restored - please do not omit citations)

Albretch said:
 have you used that library yourself?

Jscience is a widely used and highly recommended library overall.
 Have you never needed a method such as boolean
org.jscience.mathematics.number.Rational.reduce()?

 I found a little weird that such a library does not have such a
method!

 I for one would need such a method

Man, you are just so hard to please! :)

What would such a method benefit, given that nothing in the cited
'Rational' class reveals the original dividend or divisor?

The beauty of Jscience is that, being free, it can be modified. You
can easily add your essential method.

Of course, then you wouldn't be able to complain that someone else
hadn't done the work for you.
 
A

Albretch Mueller

(citation restored - please do not omit citations)




Jscience is a widely used and highly recommended library overall.




Man, you are just so hard to please! :)
~
Honestly I needed this method or at least I needed to know that
reducing them was done in the background. I think the commons math
Fraction API looks better but I haven't tested one against the other
yet
~
http://commons.apache.org/math/apidocs/org/apache/commons/math/fraction/Fraction.html
~
I will run stress tests on both libs and I will let you know
~
lbrtchx
 
J

John B. Matthews

Albretch Mueller said:
Hey John

have you used that library yourself?

Why, yes I have.
Have you never needed a method such as boolean
org.jscience.mathematics.number.Rational.reduce()?

JScience has a private method, normalize(), that is applied in the
static factories and after each operation. The methods getDividend() &
getDivisor() always return relatively prime values.
I found [it] a little weird that such a library does not have such a
method! I for one would need such a method.

I can't say I've ever seen a need to expose that operation. Can you
elaborate?
 
D

Daniel Pitts

Albretch said:
~
Honestly I needed this method or at least I needed to know that
reducing them was done in the background. I think the commons math
Fraction API looks better but I haven't tested one against the other
yet
~
http://commons.apache.org/math/apidocs/org/apache/commons/math/fraction/Fraction.html
~
I will run stress tests on both libs and I will let you know
~
lbrtchx
The method the OP needs is called "normalize", it is private. It looks
like Rational does keep items reduced, which is a reasonable assumption
to make of a well-implemented Rational class.
 
A

Albretch Mueller

I found [it] a little weird that such a library does not have such a
method! I for one would need such a method.
I can't say I've ever seen a need to expose that operation. Can you
elaborate?


The method the OP needs is called "normalize", it is private. It looks
like Rational does keep items reduced, which is a reasonable assumption
to make of a well-implemented Rational class.

Well, from my original post we wandered around the topic to its main
point; namely, how do these libraries do their background cooking with
prime numbers

I think those libraries -must- keep the fractions reduced in fact
they should keep instead of the numbers themselves their
factorizations in order to run lcd and gcf algorithms without dividing
and modulo operations

Some time ago I had to run simulations analyzing data in solid states
physics and due to symmetry conditions it was very important to know
if some figures were actually 0 or some small number. What we did was:

1) Stash in memory all logarithms of prime numbers and then read them
in as needed

2) all multiplications and divisions we vectorized as linear
operations on those (already calculated) logarithms

3) then exponentiated the result ...

This way we also new the error range of our calculations and as a
double check we would run the same program with a higher precision and
compare the results to easily spot our symmetries

lbrtchx
 
J

John B. Matthews

Albretch Mueller said:
I found [it] a little weird that such a library does not have such a
method! I for one would need such a method.
I can't say I've ever seen a need to expose that operation. Can you
elaborate?


The method the OP needs is called "normalize", it is private. It
looks like Rational does keep items reduced, which is a reasonable
assumption to make of a well-implemented Rational class.

Well, from my original post we wandered around the topic to its main
point; namely, how do these libraries do their background cooking
with prime numbers

I think those libraries -must- keep the fractions reduced in fact
they should keep instead of the numbers themselves their
factorizations in order to run lcd and gcf algorithms without
dividing and modulo operations

The implementations I know typically rely on GCD via Euclid's
algorithm. I was intrigued to learn that the approach "never requires
more steps than five times the number of digits (base 10) of the
smaller integer."

<http://en.wikipedia.org/wiki/Euclidean_algorithm>

[...]
 
E

Eric Sosman

[...]
The Number class represents an instance which can be converted to a
byte, short, int, long, float or double, while trying to keep the same
numerical value when appropriate (i.e. if shortValue() returns 371 then
intValue() shall return 371, and doubleValue() shall return 371.0).

You've slipped a cog somewhere, I think.

public class Rong {
public static void main(String[] unused) {
Number n = new Double(371.42);
System.out.println("shortValue: " + n.shortValue());
System.out.println("intValue: " + n.intValue());
System.out.println("doubleValue: " + n.doubleValue());
}
}

Output:

shortValue: 371
intValue: 371
doubleValue: 371.42
 
A

Arved Sandstrom

Thomas said:
Actually not really. There is not a real "hierarchy" of arithmetic
types; for instance, the Double class does not extend Integer, even
though every 32-bit integer value can be represented exactly as a
Double. For a full "tower of arithmetic type", you do not want Java but
Scheme.

The Number class represents an instance which can be converted to a
byte, short, int, long, float or double, while trying to keep the same
numerical value when appropriate (i.e. if shortValue() returns 371 then
intValue() shall return 371, and doubleValue() shall return 371.0). Sun
included in java.lang the base classes corresponding to the primitive
types, and in java.math some additional classes which are useful to many
application domains (BigInteger and BigDecimal). There are many other
classes which could have been added to java.math as well; in my line of
work, a class for generic modular integers would have been great. Right
now, I do not see what kind of application would benefit from rational
numbers, although I can see uses for a class corresponding to algebraic
numbers.

I think you meant to say, you can't see what kind of application would
benefit from having a _class_ that represents rational numbers. If you
happened to be working with Pade approximants you could certainly live
without such a class, but you sure couldn't live without rational
numbers. :)

AHS
 
A

Arne Vajhøj

~
Since JDK 1.0 java.lang.Number has been subclassed by:
~
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
why hasn't sun included "Rational" as one of the subclasses? (and it
seems based on past history and the kind of stuff they deem important
(http://openjdk.java.net/projects/jdk7/features/) that they won't ever
do so)
~
why not java.lang.Rational? It naturally fits within the Number class
hierarchy!
~

Rational is not a traditional basic type in programming like the
above.

Sure they could have added Rational, Complex etc. in java.math package.

I even think they should have done so.

But apparently those in charge considered those a niche and something
to be found in an external math package.

And no matter how much they chose to include then there will always
be some that think some functionality is missing.

Arne
 
A

Arne Vajhøj

~
Honestly I needed this method or at least I needed to know that
reducing them was done in the background. I think the commons math
Fraction API looks better but I haven't tested one against the other
yet
~
http://commons.apache.org/math/apidocs/org/apache/commons/math/fraction/Fraction.html
~
I will run stress tests on both libs and I will let you know
~

That is the good thing about multiple open source libraries covering
the same topic.

You can try them all and pick the one that fits your needs best.

And that may not the the same pick for everyone.

Arne
 
A

Arne Vajhøj

~
Since JDK 1.0 java.lang.Number has been subclassed by:
~
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
~
why hasn't sun [sic] included "Rational" as one of the subclasses? (and it

Ask them.

People here can speculate, but few if any will know.

Speculating is not unusual around here.
So it's Sun's job to write every class you'll ever need? If they did
that, how would you justify your high programmer's salary?

Basic mathematical entities is a rather small subset of all classes.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top