programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Fraction issues on calculating less than % gcd on negative numbers

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Hi guys

I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing. . Thanks in advance.

Enter numerator; then denominator.
-5
10
-5/10
Enter numerator; then denominator.
3
9
1/3
Sum:
-5/30
-0.16666666666666666
Product:
-5/30
-0.16666666666666666
Devide:
-15/30
-0.5
subtract:
-45/90
-0.5
negative:
1/6
0.16666666666666666
Lessthan:
1/6
0.16666666666666666
greaterthan:
1/6
0.16666666666666666

FRACTION CLASS

Marshal
Posts: 79232
377
• Number of slices to send:
Optional 'thank-you' note:
You have fallen into the trap of writing lots of code and then wondering why it doesn't work.
Start by removing most of the //0000000000 comments which make the code harder to read. Then turn your computer off.
Write down an algorithm for GCD. Convert that to pseudocode, then consider turning your computer back on.
Then create a class with a gcd method (probably static) using Euclid's algorithm or something else. Get that to work.

Then you can consider the other methods.

Tate Hsumsor
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Thankd I'll try that and see.

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Tate Hsumsor wrote:I've been writing a fraction class...

Oddly enough, I did exactly the same thing a few years back, except with BigInteger's, so here's a few suggestions for you:

1. Consider implementing Comparable<Fraction>, rather than separate lessThan() and greaterThan() methods.
2. Your lessThan() method doesn't look right (haven't checked the other one).
3. Normalizing (also called 'reducing') can be done "in arrears" - ie, when the Fraction is used, rather than every time you get a result. You may also find it useful to have normalize() return a Fraction - ie, this, after you've reduced it.
4. Consider adding an invert() method.
5. You never check for overflow, despite the fact that pretty much any arithmetic on a Fraction will tend to increase the sizes of the numerator and/or divisor.
6. Fractions have a lot in common with floating-point numbers, so you might want to consider whether you want to support "infinites" (n/0) and/or "NaN" (0/0).
7. You keep the sign in the numerator, which is fine (and probably simplest); but an alternative arrangement is to store the sign separately and make your numerator and divisor positive (sometimes called "sign-magnitude"). One thing this does is allow you to store ±0 as distinct values if you want to; but it's not wildly important.
8. Consider making the class immutable and have your arithmetic methods return new Fractions, rather than updating this one. I think you'll find the API becomes a lot simpler.

and a few "efficiency" things for you:
1. When adding or subtracting, you don't need to normalize if the divisors are equal.
2. When muliplying or dividing, you don't have to normalize if either operand is 1 or -1.
3. When comparing, check the signs first, and then whether one operand is "vulgar" and the other isn't. You only need to compare products if both of the those cases are false.

HIH

Winston

Tate Hsumsor
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Winston thank you so much that was seriously helpful. Much appreciated, I had initially made my class immutable but then ended up having errors on methods, but now I know why, I was not returning the newFraction. Thanks again

Winston Gutkowski
Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Tate Hsumsor wrote:Winston thank you so much that was seriously helpful. Much appreciated...

You're most welcome. Glad to see someone else ploughing the same furrow. I always thought it might be kinda useful.

Winston

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?