Is there a javascript function that reduces a fraction

社会主义新天地 提交于 2019-11-27 11:27:55
// Reduce a fraction by finding the Greatest Common Divisor and dividing by it.
function reduce(numerator,denominator){
  var gcd = function gcd(a,b){
    return b ? gcd(b, a%b) : a;
  };
  gcd = gcd(numerator,denominator);
  return [numerator/gcd, denominator/gcd];
}

reduce(2,4);
// [1,2]

reduce(13427,3413358);
// [463,117702]

No, but you can write one yourself fairly easily. Essentially you need to divide the top and bottom parts of the fraction by their 'Greatest Common Denominator'... Which you can calculate from Euclid's algorithm.

Read here for more info: http://www.jimloy.com/number/euclids.htm

edit:

code (because everyone seems to be doing it, this doesn't use recursion though)

var FractionReduce = (function(){
    //Euclid's Algorithm
    var getGCD = function(n, d){
        var numerator = (n<d)?n:d;
        var denominator = (n<d)?d:n;        
        var remainder = numerator;
        var lastRemainder = numerator;

        while (true){
            lastRemainder = remainder;
            remainder = denominator % numerator;
            if (remainder === 0){
                break;
            }
            denominator = numerator;
            numerator = remainder;
        }
        if(lastRemainder){
            return lastRemainder;
        }
    };

    var reduce = function(n, d){
        var gcd = getGCD(n, d);

        return [n/gcd, d/gcd];
    };

    return {
            getGCD:getGCD,
            reduce:reduce
           };

}());

alert(FractionReduce.reduce(3413358, 13427));

To reduce a fraction, divide the numerator and denominator by the Greatest Common Factor. Phrogz and David have already provided the source code..

However if you're searching for javascript libraries for handling fractions, then here are a few to choose from.

  1. Fraction.js
  2. Math.Rational
  3. Ratio.js
  4. Rational.js

Here's an example using Ratio.js.

var a = Ratio(2,4);

a.toString() == "2/4";
a.simplify().toString() == "1/2";    // reduce() returns a clone of the Ratio()
a.toString() == "2/4"; // Ratio functions are non-destructive.
Jabel Márquez

I know there is already an answer, but I want share a JS library that I found when I was looking something to convert decimal numbers into fractions and reducing fractions.

The library calls Fraction.js, which was really helpful for me and saved me a lot time and work. Hope it can be very useful to somebody else!

Here is a recursive function using ECMAScript 6 reduce. It works for most fractions as long as the remainder isn't too small. 0 has been redefined to make it work for arrays like [1.2, 2.4, 12, 24]. I tested in Chrome and IE Edge so it may behave differently in other browsers or upgrades. So it should work with an array of floats.

 Array.prototype.gcd = function () {
   if (this.length === 0)
     return null;
   return this.reduce((prev, curr) => {
     if (curr <= 1.00000000001e-12)
       return prev
     else
       return [curr, prev % curr].gcd();
    });
  }

  var reducedValueGCD = [1.2, 2.4, 12, 24, 240].gcd();

Search for MDN reduce or more info here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!