1. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Fractions

So, for one of my class projects, we need to create a program that will calculate slope through the y2-y1/x2-x1 formula.
Here's my current code:

Java Code:
```import java.util.Scanner;

public class slope {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
double y1, y2, x1, x2, ya, xa;
y1 = input.nextDouble();
y2 = input.nextDouble();
x1 = input.nextDouble();
x2 = input.nextDouble();
ya = y2 - y1;
xa = x2 - x1;
System.out.println("Your slope is: " + ya + "/" + xa);
}
}```
Now, here's my problem:
I used to just have an m double, and its value was ya/xa. But I realized that when writing equations, people don't use slopes with decimals, they use fractions. So I removed the m double, and simply manually typed in a division bar to represent a fraction.
Java Code:
`System.out.println("Your slope is: " + ya + "/" + xa);`
But then I also realized that there will be times when your x or y values may be decimals or fractions. And I can't very well have a slope that's ((5/2)/(3/2)) or 3.8/4.9.
So, I wanted to add the m double back. But how can I make the m double show as a fractional result. EG: I want 2.5 to show as 5/2.

2. ## Re: Fractions

You might want to rethink your approach. For example, what if x1 is 0 and x2 is pi? How do you want to represent that as a fraction?

3. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

I'm not sure what you mean.
If x1 is 0 and x2 is pi, the bottom part of the fraction would be (3.14159 - 0), so 3.14159.

4. ## Re: Fractions

...then why do you need to represent 2.5 as 5/2?

5. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

Because it's slope. It needs to be a fraction.

6. ## Re: Fractions

Originally Posted by Kartright
Because it's slope. It needs to be a fraction.
So why doesn't 3.14159 have to be a fraction?

You're missing my point. Not every number *can* be represented as a fraction. Your example 2.5 is nice and easy for 5/2, but many (most) other numbers will not be so clean. I think you should take a step back and really think about your actual goal here.

7. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

Every number can be a fraction.
3.79 --> 379/100

456.78 --> 22839/50 (45678/100 when not reduced)

Look, if you can help me great. But I didn't post here to argue...

8. ## Re: Fractions

Originally Posted by Kartright
Every number can be a fraction.
Sorry, but this is absolutely false. Recommended reading: Irrational number - Wikipedia, the free encyclopedia

You haven't mentioned any restrictions on the numbers your users are entering, so there isn't going to be a one-size-fits-all solution for you, since some numbers will be absolutely impossible to reduce to a fraction. I'm not trying to argue, but the best thing I can do to help you is to help you understand the problem in the first place.

9. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

I came on here for help. Not for you to rant at me. It's required that my program output slope in FRACTIONAL form. I don't care if sometimes it doesn't work, or if the fraction isn't pretty. Now, if you aren't going to help me, please leave.

10. ## Re: Fractions

Originally Posted by KevinWorkman
Sorry, but this is absolutely false. Recommended reading: Irrational number - Wikipedia, the free encyclopedia
Not that it matters much here, but I beg to differ: every number in the IEEE/754 set is an element of the set Q (rational numbers).

kind regards,

Jos

11. ## Re: Fractions

Originally Posted by JosAH
Not that it matters much here, but I beg to differ: every number in the IEEE/754 set is an element of the set Q (rational numbers).

kind regards,

Jos
Fair enough. And realistically, a user isn't going to enter an infinite number of decimals- unless operations like square roots are allowed in the input. I'm probably being a bit pedantic, but my point is that the OP needs to narrow his requirements a bit.

12. ## Re: Fractions

Originally Posted by Kartright
I came on here for help. Not for you to rant at me. It's required that my program output slope in FRACTIONAL form. I don't care if sometimes it doesn't work, or if the fraction isn't pretty. Now, if you aren't going to help me, please leave.
I am trying to help you, but getting you to think about the problem more. If you're looking for somebody to just dump code in your lap, you came to the wrong place.

Let's start over: what have you tried? I suggest creating a standalone SSCCE that consists of a single method that takes a double argument and returns a String fraction. Can you get it to return the "ugly" fraction, based on your assertion that every number can be written as one? Can you reduce those ugly fractions? Which step are you stuck on? What are you confused about?

13. Member
Join Date
May 2013
Location
New Hampshire, USA
Posts
5
Rep Power
0

## Re: Fractions

Hi Kartright,

Where this is a school assignment, it wouldn't be beneficial to either of us if we just dropped a working function in your lap, so I agree with Kevin in that aspect. However, I'd be happy to help you work to that goal. As Kevin said, start by trying to write a function that simply takes a double (your slope m), and returns the fraction as a String.

Here's some additional helpful info for the math that should happen to get the top and bottom of your fraction.

My recommendation would be a loop that just takes your decimal, and multiplies it by each positive integer, starting from 1, until it get's a whole number. For an example number of 4.25, it would basically do this:

4.25 * 1 = 4.25 (not a whole number)
4.25 * 2 = 8.5 (not a whole number)
4.25 * 3 = 12.75 (not a whole number)
4.25 * 4 = 17 (GREAT! A whole number!)

The loop should stop once you find a multiplier that will make your number a whole number, so for that example, the loop would stop at 4, and save the numbers 4 and 17. That's actually your formula for getting the fraction. it would be 17/4. Remember that you can use the modulus operator (%) to see if your result is divisible by 1, and if it is, then it's a whole number.

Give it a shot, and post back here if you have any questions further!

Good luck,
Richard

14. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,004
Rep Power
11

## Re: Fractions

Why not write a fraction class to do your math. Maintain the numerator and the denominator as instance fields. When you divide one class instance by another
e.g. frac1.div(frac2) then you would really be multiplying by the reciprocal of one of the fractions. Adding or subtracting two fractions could be done using an LCM function to find the greatest common multiple as the denominator. The fractions could be reduced using a GCD (greatest common divisor function). Actually for two numbers A and B

A x B = LCM(A,B) x GCD(A,B)

You could even use your toString() method to display in fractional form.

It doesn't have to be elaborate.

Edit: One more thing. You can also maintain a decimal equivalent. Using some basic number theory you can determine the non-repeating and the repeating part and length of the decimal expansion as long as you have the numerator and denominator.

Regards,
Jim
Last edited by jim829; 05-29-2013 at 08:02 PM.

15. ## Re: Fractions

Originally Posted by RichardPressler
My recommendation would be a loop that just takes your decimal, and multiplies it by each positive integer, starting from 1, until it get's a whole number. For an example number of 4.25, it would basically do this:

4.25 * 1 = 4.25 (not a whole number)
4.25 * 2 = 8.5 (not a whole number)
4.25 * 3 = 12.75 (not a whole number)
4.25 * 4 = 17 (GREAT! A whole number!)

The loop should stop once you find a multiplier that will make your number a whole number, so for that example, the loop would stop at 4, and save the numbers 4 and 17. That's actually your formula for getting the fraction. it would be 17/4. Remember that you can use the modulus operator (%) to see if your result is divisible by 1, and if it is, then it's a whole number.
That is too naive (read: too slow); better use the Double.doubleToLongBits( ... ) method so that you have a numerator and a denominator in one step. The denominator is a power of two, so the gcd( ... ) of the two numbers is easy to find. The result fits in two long numbers if the exponent of the orignal number wasn't too large; otherwise BigIntegers will do the job.

kind regards,

Jos

16. Member
Join Date
May 2013
Location
New Hampshire, USA
Posts
5
Rep Power
0

## Re: Fractions

Jos,

While I personally agree, I gave the method the way I did based on what I expect he has knowledge of. Looking at the source containing only very basic language, I assume he's not familiar with BitIntegers. I'd also assume that the purpose of the exercise is to practice a fundamental concept he's just learned, such as basic logic or arithmetic in Java, so I wanted to leave it at that.

17. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

Originally Posted by KevinWorkman
Fair enough. And realistically, a user isn't going to enter an infinite number of decimals- unless operations like square roots are allowed in the input. I'm probably being a bit pedantic, but my point is that the OP needs to narrow his requirements a bit.
Narrow my requirements? I need a fraction. Can't possibly narrow it down anymore.
Also, I don't need you to help me think more about the problem. It's my 7th assignment in the class, it's as basic as can be. My teacher encourages me to think just fine.
So, in the future Kevin, avoid my topics.

Also, I wasn't looking for code to be dumped on my lap. I was looking for an approach, and Richard gave one to me. Thanks Richard, I figured it out.

18. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,004
Rep Power
11

## Re: Fractions

Originally Posted by Kartright
So, in the future Kevin, avoid my topics.
Your loss! With that attitude, a lot more folks may be avoiding your topics.

Regards,
Jim

19. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Fractions

Originally Posted by jim829
Your loss! With that attitude, a lot more folks may be avoiding your topics.

Regards,
Jim
Thanks for the advice Jim. But I simply dislike getting ranted at over a simple request for help.

20. ## Re: Fractions

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•