Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why does Java not have a RealNumber class?

Reply
Thread Tools

Why does Java not have a RealNumber class?

 
 
Sideswipe
Guest
Posts: n/a
 
      06-20-2008
This is a question that continues to bug me.

There is Number. Double, Float, Int ... all extends Number and then
implement Comparable independently.

I know plenty of Math geeks troll this group. I know why Number is not
Comparable, but number has methods like: "doubleValue()"

What I would love is something like:


public abstract class RealNumber<T> extends Number implements
Comparable<T> {
.....
}

public class Double extends RealNumber<Double> {
// impement comparable <double>
}

public class Integer extends RealNumber<Integer> {
// impement comparable <Integer>
}

That way I could do ... Set<RealNumber> someSet = new
TreeSet<RealNumber>();

I don't care which type they are, as long as they are all the same and
I can't do <Number> because they aren't comparable.

And, I already know I can do:

Set<Number> someSet = new TreeSet<Number>(new Comparator<Number>() {
int compare(Number a, Number b) {return a.doublValue() -
b.doubleValue());}
});

But this has the inherent flaw of precision issues between Integer
and, say Double that this would mask.

I am tempted to actually implement this exact solution for my problem
space through delegation but I am hesitant that there might be some
hidden number theory problem with this approach.

Thoughts?

Christian Bongiorno
http://christian.bongiorno.org
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-20-2008
Sideswipe wrote:
> This is a question that continues to bug me.
>
> There is Number. Double, Float, Int ... all extends Number and then
> implement Comparable independently.
>
> I know plenty of Math geeks troll this group. I know why Number is not
> Comparable, but number has methods like: "doubleValue()"
>
> What I would love is something like:
>
>
> public abstract class RealNumber<T> extends Number implements
> Comparable<T> {
> .....
> }
>
> public class Double extends RealNumber<Double> {
> // impement comparable <double>
> }
>
> public class Integer extends RealNumber<Integer> {
> // impement comparable <Integer>
> }
>
> That way I could do ... Set<RealNumber> someSet = new
> TreeSet<RealNumber>();
>
> I don't care which type they are, as long as they are all the same and
> I can't do <Number> because they aren't comparable.
>
> And, I already know I can do:
>
> Set<Number> someSet = new TreeSet<Number>(new Comparator<Number>() {
> int compare(Number a, Number b) {return a.doublValue() -
> b.doubleValue());}
> });
>
> But this has the inherent flaw of precision issues between Integer
> and, say Double that this would mask.
>
> I am tempted to actually implement this exact solution for my problem
> space through delegation but I am hesitant that there might be some
> hidden number theory problem with this approach.
>
> Thoughts?
>
> Christian Bongiorno
> http://christian.bongiorno.org

Wait, what are you trying to do?

Integer is already Comparable<Integer>. Double implements
Comparable<Double>, what do you hope to achieve?


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Sideswipe
Guest
Posts: n/a
 
      06-20-2008
I would prefer just to understand why there is no "RealNumber" that
all the basic java types extends from. But to answer your question, I
will have to work with numbers and perform basic mathematical steps on
them but I won't know which types until runtime. What I am working on
will not know if it will be Integer Double, etc. So, unless I want to
make methods for all those types (and then types I don't know about --
like subclasses of BigDecimal) I would like to just have (in a perfect
world) a method like

NUM_T extends RealNumber

public void doJob(NUM_T a, NUM_T b) {
a / b;
}

I want to polymorphically encapsulate mathematical operations without
having to overload the same method for everytype. I understand that
isn't possible now, and so, that is what I am asking.

If I had a class called "RealNumber" that I could count on for these
basic ops, I would be set. So, look at the original post.

> Wait, what are you trying to do?
>
> Integer is already Comparable<Integer>. *Double implements
> Comparable<Double>, what do you hope to achieve?
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-20-2008
Sideswipe <(E-Mail Removed)> writes:
>NUM_T extends RealNumber
>public void doJob(NUM_T a, NUM_T b) {
> a / b;
>}


This can be written in C++:

#include <iostream>
#include <ostream>

template<typename NUM_T>
NUM_T doJob( NUM_T const a, NUM_T const b )
{ return a / b; }

int main()
{ ::std::cout << doJob( 2, 3 )<< '\n';
::std::cout << doJob( 2., 3. )<< '\n'; }

Possibly one can restrict »NUM_T« to numeric types better than
I did above.

There is no C++ implementation for the JVM.

In Java, an approximation would be:

public class Main
{
static int doJob( final int a, final int b ){ return a / b; }
static double doJob( final double a, final double b ){ return a / b; }

public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( doJob( 2, 3 ));
java.lang.System.out.println( doJob( 2., 3. )); }}

>I want to polymorphically encapsulate mathematical operations
>without having to overload the same method for everytype.


You do not want to overload.

Overloading is a kind of polymorphism (static polymorphism).

This uses only a single method declaration:

public class Main
{
@java.lang.SuppressWarnings( "unchecked" )
static< T >T doJob( final T a, final T b )
{ if( a instanceof java.lang.Integer && b instanceof java.lang.Integer )
{ return ( T )
java.lang.Integer.valueOf((( java.lang.Integer )a ).intValue() /
(( java.lang.Integer )b ).intValue() ); }
else if( a instanceof java.lang.Double && b instanceof java.lang.Double )
{ return ( T )
java.lang.Double.valueOf((( java.lang.Double )a ).doubleValue() /
(( java.lang.Double )b ).doubleValue() ); }
else return null; }

public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( doJob( 2, 3 ));
java.lang.System.out.println( doJob( 2., 3. )); }}

 
Reply With Quote
 
mike.mainguy@gmail.com
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 9:45*am, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> Sideswipe <(E-Mail Removed)> writes:
> >NUM_T extends RealNumber
> >public void doJob(NUM_T a, NUM_T b) {
> > * * * *a / b;
> >}

>
> * This can be written in C++:
>
> #include <iostream>
> #include <ostream>
>
> template<typename NUM_T>
> NUM_T doJob( NUM_T const a, NUM_T const b )
> { return a / b; }
>
> int main()
> { ::std::cout << doJob( 2, 3 )<< '\n';
> * ::std::cout << doJob( 2., 3. )<< '\n'; }
>
> * Possibly one can restrict »NUM_T« to numeric types better than
> * I did above.
>
> * There is no C++ implementation for the JVM.
>
> * In Java, an approximation would be:
>
> public class Main
> {
> * static int * *doJob( final int * *a, final int * *b ){ return a / b; }
> * static double doJob( final double a, final double b ){ return a / b; }
>
> * public static void main( final java.lang.String[] args )
> * { java.lang.System.out.println( doJob( *2, 3 *));
> * * java.lang.System.out.println( doJob( 2., 3. )); }}
>
> >I want to polymorphically encapsulate mathematical operations
> >without having to overload the same method for everytype.

>
> * You do not want to overload.
>
> * Overloading is a kind of polymorphism (static polymorphism).
>
> * This uses only a single method declaration:
>
> public class Main
> {
> * @java.lang.SuppressWarnings( "unchecked" )
> * static< T >T doJob( final T a, final T b )
> * { if( a instanceof java.lang.Integer && b instanceof java.lang.Integer )
> * * { return ( T )
> * * * java.lang.Integer.valueOf((( java.lang.Integer )a ).intValue() /
> * * * (( java.lang.Integer )b ).intValue() ); }
> * * else if( a instanceof java.lang.Double && b instanceof java.lang.Double )
> * * { return ( T )
> * * * java.lang.Double.valueOf((( java.lang.Double )a ).doubleValue() /
> * * * (( java.lang.Double )b ).doubleValue() ); }
> * * else return null; }
>
> * public static void main( final java.lang.String[] args )
> * { java.lang.System.out.println( doJob( *2, 3 *));
> * * java.lang.System.out.println( doJob( 2., 3. )); }}


I'm not a mathematician, I just play one on TV, but...

I believe most of the our more modern dynamic languages (ruby, python,
groovy) do this type of coercion, so I doubt there is a mathematical
basis for not doing this. I suspect it was more of an engineering
problem (Double and Int are fundamentally different, how can you
possibly compare them?).

In the same line, would you want to compare a string to a double?
Practically speaking "1.2" == 1.2d should return true for probably
99.9% of the non-trivial use cases one would need to implement. But
from a design/engineering perspective and looking out from the jvm, I
can see where this could be a contentious statement.

 
Reply With Quote
 
Sideswipe
Guest
Posts: n/a
 
      06-20-2008
Yeah,

I have investigated this before but never got a conclusive answer. So
I am asking a very specific question. You're the only one who has
given me a reasonable answer.

So, yes, Double and Integer are fundamentally different. But, generics
will allow me to force type compatibility (So String compared to
Double will never occur). Since this is runtime, however, I don't have
that luxury. I will be dealing with reflection and other scary
dynamics. What I can do is assume that I would be passed incompatable
types -- I can only enforce this through exception which people don't
like.

So, given that, I want to have 1 method that could handle all those
types, with the assumption that they are type compatible.

Christian
>
> I'm not a mathematician, I just play one on TV, but...
>
> I believe most of the our more modern dynamic languages (ruby, python,
> groovy) do this type of coercion, so I doubt there is a mathematical
> basis for not doing this. *I suspect it was more of an engineering
> problem (Double and Int are fundamentally different, how can you
> possibly compare them?).
>
> In the same line, would you want to compare a string to a double?
> Practically speaking "1.2" == 1.2d should return true for probably
> 99.9% of the non-trivial use cases one would need to implement. *But
> from a design/engineering perspective and looking out from the jvm, I
> can see where this could be a contentious statement.


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>< T >


In some cases, this can be refined to:

< T extends java.lang.Number & java.lang.Comparable< ? >>

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-20-2008
Sideswipe wrote:
> I would prefer just to understand why there is no "RealNumber" that
> all the basic java types extends from. But to answer your question, I
> will have to work with numbers and perform basic mathematical steps on
> them but I won't know which types until runtime. What I am working on
> will not know if it will be Integer Double, etc. So, unless I want to
> make methods for all those types (and then types I don't know about --
> like subclasses of BigDecimal) I would like to just have (in a perfect
> world) a method like
>
> NUM_T extends RealNumber
>
> public void doJob(NUM_T a, NUM_T b) {
> a / b;
> }
>
> I want to polymorphically encapsulate mathematical operations without
> having to overload the same method for everytype. I understand that
> isn't possible now, and so, that is what I am asking.
>
> If I had a class called "RealNumber" that I could count on for these
> basic ops, I would be set. So, look at the original post.
>
>> Wait, what are you trying to do?
>>
>> Integer is already Comparable<Integer>. Double implements
>> Comparable<Double>, what do you hope to achieve?
>>
>> --
>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

>

Something that might get you a step closer:
class MyMathClass<T extends Number&Comparable<T>> {
}

This says that T must extend Number and it must be Comparable to itself.

The trouble is that Java has no operator overloading. It may trick you
into thinking it does, based on the fact that it will auto-box/unbox for
you.

For your use case, now that I'm understanding it a bit better, you need
an interface (Field probably) which represents the ability to perform
addition/subtraction/etc... Then you would have IntegerField implements
Field, DoubleField implements Field, etc...

Granted this is a bit of a pain, but in Java, its the way to do it. In
C++, templates and operator overloading save you some of this tiresome
framework.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-20-2008
Sideswipe <(E-Mail Removed)> writes:

> So, given that, I want to have 1 method that could handle all those
> types, with the assumption that they are type compatible.


Something like:

Number add(Number a, Number b) {
if (a instanceof Float) {
return ((Float)a) + ((Float)b);
}
if (a instanceof Double) {
return ((Double)a) + ((Double)b);
}
// etc
}

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      06-20-2008
On Fri, 20 Jun 2008, Stefan Ram wrote:

> (E-Mail Removed)-berlin.de (Stefan Ram) writes:
>> < T >

>
> In some cases, this can be refined to:
>
> < T extends java.lang.Number & java.lang.Comparable< ? >>


Should that be T extends Number & Comparable<T> - with a 'T' binding the
Comparable, rather than another '?'?

Generics still confuse me, so perhaps not!

tom

--
ONE IN EIGHT GO MAD
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what does "execfile" mean within profiler output and why does it not have a attached line number Rahul Python 4 04-07-2009 03:19 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
WHy does not java have canBeExecuted method in its File class Yadagiri Rao KP Java 4 08-14-2003 04:17 AM



Advertisments