This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/3/9

Fractions

1
2
Nosson1459 Nosson1459

2017/3/9

#
What's the simplest way (or any way) to simplify a fraction. In my scenario I will be finding the slope of a line, the Y in the slope will be the vertical speed and the x will be the horizontal speed. The number can turn out to be '(300 - 75) / (400 - 200)' (y1 - y2 / x1 - x2) which will make the slope be '225 / 200'. I don't want the speeds to be 225 and 200 I want them to be 9 and 8 ('9 / 8' == '225 / 200'). How can I do this in my program('s code)?
danpost danpost

2017/3/9

#
Once you divide y by x, you will lose the concept of speed in the value. Therefore, keep the x and y values separate and just divide each by 25 to get the speeds in each direction.
Nosson1459 Nosson1459

2017/3/9

#
what I meant was to do this
private int gcf(int y,int x)
    {
        int num=0;
        int gcf=0;
        num=x<y?x:y;
        for(int i=1;i<(num+1);i++)
        {
            if(x%i==0&&y%i==0)gcf=i;
        }
        return gcf;
    }
danpost danpost

2017/3/9

#
Nosson1459 wrote...
what I meant was to do this < Code Omitted >
I am not sure what this code has to do with what you brought up in your original post. However, more simply might be this for the method given:
private int gcf(int x, int y)
{
    for (int i=Math.min(x, y); i>0; i--)
    {
        if (x%i == 0 && y%i == 0)
        {
            return i;
        }
    }
    return -1;
}
or, in condensed form:
private int gcf(int x, int y)
{
    for (int i=Math.min(x, y); i>0; i--) if (x%i == 0 && y%i == 0) return i;
    return -1;
}
Of course, you may want to check the parameter values before finding the gcf with this line (added as first line in gcf method):
if (x<1 || y<1) return -1;
Then you can remove the condition from within the 'for' statement:
for (int i=Math.min(x, y); ; i--)
Nosson1459 Nosson1459

2017/3/9

#
danpost wrote...
I am not sure what this code has to do with what you brought up in your original post.
Nosson1459 wrote...
What's the simplest way (or any way) to simplify a fraction. In my scenario I will be finding the slope of a line, the Y in the slope will be the vertical speed and the x will be the horizontal speed. The number can turn out to be '(300 - 75) / (400 - 200)' (y1 - y2 / x1 - x2) which will make the slope be '225 / 200'. I don't want the speeds to be 225 and 200 I want them to be 9 and 8 ('9 / 8' == '225 / 200'). How can I do this in my program('s code)?
Who said that I was dividing the y by the x and then asking how to get the slope? The question was simply...
Nosson1459 wrote...
What's the simplest way (or any way) to simplify a fraction.
After finding the GCF I can divide the x and y by it.
danpost wrote...
just divide each by 25 to get the speeds in each direction.
If the numbers given above will always be what they are then I would not have made this discussion. The point was that I will have the two coordinates and I won't know what they are beforehand, the numbers given above are an example of what I might get for coordinates and I was showing that it needs simplifying.
danpost danpost

2017/3/9

#
I do not think you know what you are doing; but, using the 'gcf' on the numbers you gave will produce 25, which you divide into the values to come up with 8 and 9 for the speeds. The thing is, if the locations are changing without keeping the same x and y differences, then the speed value will irratically change from one instance to the next.
Nosson1459 Nosson1459

2017/3/9

#
danpost wrote...
I do not think you know what you are doing
I think that its more likely that YOU don't know what I'm doing. my original purpose was to simplify fractions. in order to do that I had to make a method that finds the gcf of the 2 numbers which I have done (and this only happens once in each object).
Super_Hippo Super_Hippo

2017/3/9

#
But didn't danpost simplify your method and that was what you were asking? (I didn't test the methods.)
Nosson1459 Nosson1459

2017/3/9

#
Super_Hippo wrote...
But didn't danpost simplify your method and that was what you were asking? (I didn't test the methods.)
What I was asking was how do I simplify a fraction, if I wanted a method to be simplified I would have posted the code. Danpost's next post was saying that I don't know what I'm doing. I personally think that I can/should know what I'm doing but danpost thinks otherwise (like usual we're arguing). To say it simply:
  • I have two coordinates.
  • I don't know what they are going to be in the program because they are decided by the user's positioning of the mouse.
  • I figure out the slope from the mouse to the bottom-center of the screen. (y1 - y2/x1 - x2)
  • With a frame the size of 800 x 600 pixels the slope can technically turn out to be '100 - 600 / 200 - 400' which is basically -500 / -200 which is also 500 / 200 I don't want the object moving from the bottom-center of the screen at speeds of
    setLocation(getX() - 200, getY() - 500);
    So I wanted to SIMPLIFY it to be 5 / 2.
After all this discussion I have other problems because there are higher numbers which can't be simplified. I got x=174 y=59, these numbers can't be simplified and are too fast.
danpost danpost

2017/3/9

#
Nosson1459 wrote...
After all this discussion I have other problems because there are higher numbers which can't be simplified. I got x=174 y=59, these numbers can't be simplified and are too fast.
That is the issue I brought up with this:
danpost wrote...
if the location(s) are changing without keeping the same x and y differences, then the speed value will change erratically from one instance to the next.
Nosson1459 Nosson1459

2017/3/9

#
I didn't really understand what you meant there before. The speed doesn't change, once it's initialized by the mouse click (not mentioned until now) it keeps the speed and continues moving in that direction but the problem is when the mouse is clicked in a spot that makes a very big slope (high numbers which can't be simplified).
Super_Hippo Super_Hippo

2017/3/9

#
So basically you click somewhere with the mouse and the x and y difference between that position and the bottom left corner decides how fast (?) and in which direction something should move? I am not sure why the fractions are needed in this case, so I guess I am wrong with the first sentence.
davmac davmac

2017/3/9

#
Nosson1459 wrote...
I think that its more likely that YOU don't know what I'm doing
To be fair, I think you confused the issue by talking about the differences between two co-ordinates (which is now seemingly irrelevant) and "speeds":
I don't want the speeds to be 225 and 200 I want them to be 9 and 8
If they are "speeds" then as danpost says reducing them by the GCF will result in somewhat arbitrarily scaled speeds according to the angle. It sounds like what you're really after is the gradient, expressed as a normalised fraction. For what it's worth, there is a more efficient way of finding the GCF than the method you posted: it was developed by Euclid:
    public static int gcd(int p, int q) {
        while (q != 0) {
            int temp = q;
            q = p % q;
            p = temp;
        }
        return p;
    }
Nosson1459 Nosson1459

2017/3/10

#
davmac wrote...
Nosson1459 wrote...
I think that its more likely that YOU don't know what I'm doing
To be fair, I think you confused the issue by talking about the differences between two co-ordinates (which is now seemingly irrelevant) and "speeds":
I agree there was confusion but I was just saying that I know what I'm talking about. How many times do I have to explain what I'm doing? (rhetorical question) On a graph when you want to figure out the slope of a line you use the following formula with two coordinates on the line - 'y2-y1/x2-x1'. (So far not too complicated I hope.) In my scenario I have two coordinates (A line can be drawn between any two points/coordinates) 1) The cursor/mouse 2) An actor which when this code is being run will be in the bottom-center of the screen (Hippo by mistake said "left"). I figure out the slope by doing: (class level scope variables)
private int deltaX;
private int deltaY;
private boolean shouldMove = false;
private MouseInfo mouse;
Then:
if (shouldMove == false && Greenfoot.mouseClicked(null))
{
    mouse = Greenfoot.getMouseInfo();
    /* The line under this originally used/uses my posted gcf method, but now I can use any of the three */
    int gcf = gcf(mouse.getY() - getY(), mouse.getX() - getX());
    deltaX = (mouse.getX() - getX()) / gcf;
    deltaY = (mouse.getY() - getY()) / gcf;
    shouldMove=true;
}
if (!isAtEdge()) setLocation(getX() + deltaX, getY() + deltaY);
Now I have the y/x slope I use the y and x separately from the slope for the setLocation. I don't see what I did or said wrong.
I don't want the speeds to be 225 and 200 I want them to be 9 and 8
If they are "speeds" then as danpost says reducing them by the GCF will result in somewhat arbitrarily scaled speeds according to the angle.
What is this supposed to mean "as danpost says reducing them by the GCF will result in somewhat arbitrarily scaled speeds" should I quote my posts AGAIN? I asked how can I programmatically simplify a fraction? I have the numerator and the denominator of a fraction and I wanted to know how to put the fraction into simplest form. I gave an example of numbers that I might get in the scenario and I explained why I have to do what I'm trying to do here. The answer I received was...
danpost wrote...
Once you divide y by x, you will lose the concept of speed in the value. Therefore, keep the x and y values separate and just divide each by 25 to get the speeds in each direction.
There are two problems with this reply. 1) The first half and 2) The second half. I wasn't dividing y by x, my slope is/was y <over> x I don't need to keep them separate values because they always were and always will be. Telling me to divide by 25 will work with those numbers but NOT ALL so I explained that I need to find the GCF to put the fraction into simplest form. Then danpost gave me a condensed(, fixed up, smaller) version for the method, but what do you think the first non-condensed method was simplifying "more simply might be this for the method given:"?
davmac wrote...
It sounds like what you're really after is the gradient, expressed as a normalized fraction. For what it's worth, there is a more efficient way of finding the GCF than the method you posted: it was developed by Euclid:
    public static int gcd(int p, int q) {
        while (q != 0) {
            int temp = q;
            q = p % q;
            p = temp;
        }
        return p;
    }
I don't know what you mean by gradient but all I see is that you gave me a method to find the Greatest Common Factor/Denominator just like danpost did and just like I did. It's just that each one gets smaller and smaller.
Nosson1459 Nosson1459

2017/3/10

#
I forgot to mention that if I use this code (danpost's) then I'll have to remove the last return statement, line 5 (out of 6).
private int gcf(int x, int y)
{
    if (x < 1 || y < 1) return -1;
    for (int i = Math.min(x, y); ; i--) if (x % i == 0 && y % i == 0) return i;
}
There are more replies on the next page.
1
2