Meaningless Drivel is fun!
The moose likes Java in General and the fly likes java fraction class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java fraction class" Watch "java fraction class" New topic

java fraction class

varun bihani
Ranch Hand

Joined: Aug 11, 2003
Posts: 96
what should be the data types of numerator and denominator of java fraction class? should it be int or double/float?
eg 4/5 or 3.4/4.5 ?

<a href="" target="_blank" rel="nofollow"></a><br /><a href="" target="_blank" rel="nofollow"></a><br /><a href="" target="_blank" rel="nofollow"></a>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Are you talking about a Java class called Fraction?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I suppose it depends on what the purpose of your Fraction class is. Is your goal to create an exact representation of a fraction like 1/3? Or will you be satisfied with convenient alternatives like 0.3333333333? If it's the former, I would use only exact integer types for the numerator and denominator - int, long, or BigInteger. Floating-point arithmentic frequently has small roundoff errors; it's inherent in the format. If you're OK with approximations - then why do you need ths Fraction class at all? For computations you might just as well convert everything to doubles. However, you may have some other reason for this class, which I am unaware of. Care to provide more details on what you're trying to do?

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It would be an interesting exercise to have a number class that could store numerator and denominator. Just so

new MyNumber(1).over( new MyNumber(3) ).times( new MyNumber(3) ).equals( new MyNumber(1) )

or maybe

new MyNumber(1, 3).times( 3 ).equals( 1 );

What would you do about that 3.4 and 4.5 in the original post?

new MyNumber( new MyNumber(34, 10), new MyNumber(45, 10) )

Ok, it doesn't look like that much fun after all. There are languages that maintain numbers like that. Anybody ever use one?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
There are languages that maintain numbers like that. Anybody ever use one?

I'm currently playing with Squeak, a modern Smalltalk dialect. Smalltalk has a Fraction class and uses it fully transparently since ages.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: java fraction class
It's not a secret anymore!