Giter Club home page Giter Club logo

frac's Introduction

Find rational approximation to given real number

Convert Float or String to Fraction with given denominator maximum.

Math::Fraction.new(0.2)               # => Fraction (1/5)
Math::Fraction.new("-3 1/8")          # => Fraction (-3 1/8)

Math::Fraction.new(0.333)             # => Fraction (1/3)
Math::Fraction.new("0.333")           # => Fraction (1/3)
Math::Fraction.new(0.33, 100)         # => Fraction (33/100)
Math::Fraction.new(1.to_f / 3)        # => Fraction (1/3)

Math::Fraction.new(0.2).to_s          # => String "1/5"
Math::Fraction.new(1.2).to_s          # => String "1 1/5"
Math::Fraction.new(1.2).to_r          # => Rational (6/5)
Math::Fraction.new(1.2).to_a          # => Array [1, 1, 5]
Math::Fraction.new("0.333").to_a      # => Array [0, 1, 3]

Math::Fraction.new(0.2, 100).to_r     # => Rational (1/5)
Math::Fraction.new(0.33, 10).to_r     # => Rational (1/3)
Math::Fraction.new(0.33, 100).to_r    # => Rational (33/100)

Difference from Ruby 1.9 built-in Float#to_r

# Built-in
1.1.to_r # => (2476979795053773/2251799813685248)

# Math::Fraction with big max denominator
Math::Fraction.new(1.1, 1_000_000_000_000_000_000).to_r  # => (11/10)

Installation

gem install frac

Source

Idea and most implementation from www.ics.uci.edu/~eppstein/numth/frap.c

Based on the theory of continued fractions

if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...)))

then best approximation is found by truncating this series (with some adjustments in the last term).

Note the fraction can be recovered as the first column of the matrix

( a1 1 ) ( a2 1 ) ( a3 1 ) ...
( 1  0 ) ( 1  0 ) ( 1  0 )

Instead of keeping the sequence of continued fraction terms, we just keep the last partial product of these matrices.

License

Frac is released under the MIT license.

frac's People

Contributors

dblock avatar

Stargazers

Violet avatar Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.