0

I have a class named Rectangle.The class has two properties width and height. I need to check whether the Rectangle class has golden ratio(height/width = 1.6), if it does I need to return true, else return false.

Here is my class:

class Rectangle {
   private int x, y;
   private int height, width;

   public boolean isGoldenRatio()
   {
     return (height / width == 1 && height % width == 6); 
   }
}

I created the function isGoldenRatio() to check the ratio of the sides. But I guess it's wrong, because I don't get the desired result.

UPDATE: I cant define additions fields in my class and I cant use math library.

Any idea how to fix the function?

radoh
  • 4,034
  • 5
  • 29
  • 38
Michael
  • 11,410
  • 43
  • 120
  • 228
  • You're going to need some sort of tolerance on that, as you can't represent 1.6 exactly in floats. Is 1.60001 close enough? 1.600000000001? While we're at it, do you want 1.6, or 1.618033988...? – Iluvatar Dec 08 '16 at 23:14
  • yes 1.61 is fine enough... – Michael Dec 08 '16 at 23:17
  • Just check that `width * 8 == height * 5`, `height` and `width` are ints so you can avoid all that doubles division, thresholds etc. – Jaroslaw Pawlak Dec 08 '16 at 23:31

4 Answers4

3

To avoid the problems with float, you could use math and simply do

return (height * 10) == (width * 16);

To explain it, you got equation:

H / W = 1.6       / multiply by 10
10 * H / W = 16   / multiply by W
10 * H = 16 * W
radoh
  • 4,034
  • 5
  • 29
  • 38
0

if you want that number exactly you could change it to cast them to a double and compare like this

public boolean isGoldenRatio()
{
 return ((double)height / width == 1.6);
}
Austin
  • 706
  • 4
  • 22
0

All you need to do it divide, and just cast the numerator to a float first. You also need to decide how close it has to be to count as golden.

class Rectangle {
    private int x, y;
    private int height, width;
    private final static float FLOAT_THRESH = .01;

    public boolean isGoldenRatio()
    {
        return Math.abs(1.6 - (float)height / width) < FLOAT_THRESH;
    }
}
AlphaQ
  • 646
  • 8
  • 18
Iluvatar
  • 1,487
  • 9
  • 12
  • This does bring up the question of which direction you want the ratio applied. The question seems to imply one that's higher than wider. – Iluvatar Dec 08 '16 at 23:33
  • I cant define additions fileds in my class and I cant use math library. – Michael Dec 08 '16 at 23:34
  • Maybe you should say what exactly you need this for then, and clarify whether you're looking for a flat 1.6 or the actual golden ratio. As stated then and given the ints, go with radoh then. – Iluvatar Dec 08 '16 at 23:36
0

Use math to avoid working with floats. The ratio of the Fibonacci numbers approaches the golden ratio within the square of the denominator. e.g.

1/1, 2/1, 3/2, 5/3, 8/5, 13/8, ...

Are within 1/1, 1/1, 1/4, 1/9, 1/25, 1/64, ... respectively of the golden ratio.

So you can do the math entirely with integers or rational numbers.

If you want to be within two decimal points approximate by 21/13. So check if

-1 <= 21*height - 13*width <= 1

or vice-versa for height and width interchanged.

djechlin
  • 54,898
  • 29
  • 144
  • 264