# ratio simplifier

given a ratio (ex. 374:266 or 724.36292:740.37872) how do I get the smallest ratio? 187:133 for the first ex. Now for the second ex, that is a tricker answer. Do I convert to integer and then calculate the smallest ratio? I think I am leaning that way.

Also, 187:133 seems a bit large. Next question is how to identify a smaller ratio that is "close"? say within 2 or 3 decimal places? this example ratio is about 1.4060:1 or I guess I could say 14.06:10 then about 7:5? Not sure if I'm even close to the right track on this.

Be nice if there were already a gem that did this calculation. But I can write a method once I find a correct algorithm. I've tried google searching, but keep getting either web pages that do a Ratio Simplifier or things about gem stone ratios.

NOTE: this is not for Rails. It is for a process that I'm writing. Will be ran on my mac locally only.

\$ ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]

\$ uname -a
Darwin far-wa.local 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64

def ratio_simplifier( x, y )

return nil if x.nil? or y.nil?
x = x.round
y = y.round

# do math magic here
# ...

return "#{x}:#{y}"

end # ratio_simplifier

puts ratio_simplifier 374, 266

Thanks for any insight

Eric
ruby-talk@far.tips

Quick answer, the Rational class does that for integers. That's a first
step, the rest depends on how you define approximatiins.

Sometimes the hardest part is finding the right terms to search for.
The numerator and denominator of an irreducible fraction are relatively
coprime.
Down the wikipedia rabbithole:

Xavier's reply saved my evening.
After relearning that Floats and Rationals don't mix well. Fixed decimal to
the rescue...

\$ pry
[1] pry(main)> require 'bigdecimal'
=> true
[2] pry(main)> require 'bigdecimal/util'
=> true
[3] pry(main)> ratio = Rational(374,266)
=> (187/133)
[4] pry(main)> Rational ratio.to_d(1)
=> (1/1)
[5] pry(main)> Rational ratio.to_d(2)
=> (7/5)
[6] pry(main)> Rational ratio.to_d(3)
=> (141/100)
[7] pry(main)> Rational ratio.to_d(4)
=> (703/500)

-gf-

···

On Mon, Feb 1, 2021 at 6:45 PM Xavier Noria <fxn@hashref.com> wrote:

Quick answer, the Rational class does that for integers. That's a first
step, the rest depends on how you define approximatiins.
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

look up continued fraction convergents.

martin

···

On Mon, Feb 1, 2021 at 4:33 PM Gerard Fowley <gerard.fowley@iqeo.net> wrote:

Sometimes the hardest part is finding the right terms to search for.
The numerator and denominator of an irreducible fraction are relatively
coprime.
Down the wikipedia rabbithole:
Coprime integers - Wikipedia
Irreducible fraction - Wikipedia
Euclidean algorithm - Wikipedia
Xavier's reply saved my evening.
After relearning that Floats and Rationals don't mix well. Fixed decimal
to the rescue...

\$ pry
[1] pry(main)> require 'bigdecimal'
=> true
[2] pry(main)> require 'bigdecimal/util'
=> true
[3] pry(main)> ratio = Rational(374,266)
=> (187/133)
[4] pry(main)> Rational ratio.to_d(1)
=> (1/1)
[5] pry(main)> Rational ratio.to_d(2)
=> (7/5)
[6] pry(main)> Rational ratio.to_d(3)
=> (141/100)
[7] pry(main)> Rational ratio.to_d(4)
=> (703/500)

-gf-

On Mon, Feb 1, 2021 at 6:45 PM Xavier Noria <fxn@hashref.com> wrote:

Quick answer, the Rational class does that for integers. That's a first
step, the rest depends on how you define approximatiins.
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>