Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like thisthis.


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

  • Fraction 1: \$\dfrac{x}{y}\$
  • Fraction 2: \$\dfrac{a}{b}\$

Compare \$x*b\$ to \$a*y\$:

  1. if \$x*b > a*y\$, Fraction 1 is bigger.
  2. if \$x*b < a*y\$, Fraction 2 is bigger.
  3. if \$x*b = a*y\$, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this.


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

  • Fraction 1: \$\dfrac{x}{y}\$
  • Fraction 2: \$\dfrac{a}{b}\$

Compare \$x*b\$ to \$a*y\$:

  1. if \$x*b > a*y\$, Fraction 1 is bigger.
  2. if \$x*b < a*y\$, Fraction 2 is bigger.
  3. if \$x*b = a*y\$, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this.


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

  • Fraction 1: \$\dfrac{x}{y}\$
  • Fraction 2: \$\dfrac{a}{b}\$

Compare \$x*b\$ to \$a*y\$:

  1. if \$x*b > a*y\$, Fraction 1 is bigger.
  2. if \$x*b < a*y\$, Fraction 2 is bigger.
  3. if \$x*b = a*y\$, Fraction 1 is equal to Fraction 2.
added 45 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this:

http://stackoverflow.com/questions/4732645/iphone-objective-c-comparing-doubles-not-workingthis.


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

Fraction 1: \frac{x}{y}$, Fraction 2: a/b

  • Fraction 1: \$\dfrac{x}{y}\$
  • Fraction 2: \$\dfrac{a}{b}\$

Compare xb to ay\$x*b\$ to \$a*y\$:

  1. if xb > ay\$x*b > a*y\$, Fraction 1 is bigger.
  2. if xb < ay\$x*b < a*y\$, Fraction 2 is bigger.
  3. if xb = ay\$x*b = a*y\$, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this:

http://stackoverflow.com/questions/4732645/iphone-objective-c-comparing-doubles-not-working


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

Fraction 1: \frac{x}{y}$, Fraction 2: a/b

Compare xb to ay:

  1. if xb > ay, Fraction 1 is bigger.
  2. if xb < ay, Fraction 2 is bigger.
  3. if xb = ay, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this.


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

  • Fraction 1: \$\dfrac{x}{y}\$
  • Fraction 2: \$\dfrac{a}{b}\$

Compare \$x*b\$ to \$a*y\$:

  1. if \$x*b > a*y\$, Fraction 1 is bigger.
  2. if \$x*b < a*y\$, Fraction 2 is bigger.
  3. if \$x*b = a*y\$, Fraction 1 is equal to Fraction 2.
added 5 characters in body
Source Link
mleyfman
  • 5.3k
  • 1
  • 25
  • 48

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this:

http://stackoverflow.com/questions/4732645/iphone-objective-c-comparing-doubles-not-working


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

Fraction 1: x/\frac{x}{y}$, Fraction 2: a/b

Compare xb to ay:

  1. if xb > ay, Fraction 1 is bigger.
  2. if xb < ay, Fraction 2 is bigger.
  3. if xb = ay, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this:

http://stackoverflow.com/questions/4732645/iphone-objective-c-comparing-doubles-not-working


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

Fraction 1: x/y, Fraction 2: a/b

Compare xb to ay:

  1. if xb > ay, Fraction 1 is bigger.
  2. if xb < ay, Fraction 2 is bigger.
  3. if xb = ay, Fraction 1 is equal to Fraction 2.

One big issue I have with your code is that all of of your comparisons are based off of the fraction's double value. This can lead to many bugs down the line that result in questions like this:

http://stackoverflow.com/questions/4732645/iphone-objective-c-comparing-doubles-not-working


To fix this: you can do one of two things: define or pass an epsilon (such as .00001) to be used for comparison.

Or even better, use the denominator and numerator to compare fractions.

To do so, I would utilize memoization (to make this performant for repeated calls on the same fraction), and to to store the fraction in reduced form. Then you do a simple comparison on the reduced form like so:

Fraction 1: \frac{x}{y}$, Fraction 2: a/b

Compare xb to ay:

  1. if xb > ay, Fraction 1 is bigger.
  2. if xb < ay, Fraction 2 is bigger.
  3. if xb = ay, Fraction 1 is equal to Fraction 2.
Source Link
mleyfman
  • 5.3k
  • 1
  • 25
  • 48
Loading