Socket
Socket
Sign inDemoInstall

fraction.js

Package Overview
Dependencies
0
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.4.1 to 2.5.0

4

bower.json
{
"name": "fraction.js",
"main": "fraction.js",
"version": "2.4.1",
"version": "2.5.0",
"homepage": "http://www.xarg.org/2014/03/precise-calculations-in-javascript/",

@@ -11,3 +11,3 @@ "description": "A rational number library",

"authors": [
"Robert Eisele <robert@xarg.org>"
"Robert Eisele <robert@xarg.org> (http://www.xarg.org/)"
],

@@ -14,0 +14,0 @@ "license": [

/**
* @license Fraction.js v2.4.1 01/06/2015
* @license Fraction.js v2.5.0 01/06/2015
* http://www.xarg.org/2014/03/precise-calculations-in-javascript/

@@ -291,3 +291,7 @@ *

a = gcd(P['d'], P['n']); // Abuse a
if (Fraction['REDUCE']) {
a = gcd(P['d'], P['n']); // Abuse a
} else {
a = 1;
}

@@ -304,2 +308,8 @@ this['s'] = P['s'];

/**
* Boolean global variable to be able to disable automatic reduction of the fraction
*
*/
Fraction['REDUCE'] = 1;
/**
* Calculates the absolute value

@@ -630,4 +640,14 @@ *

var p = String(this['n']).split(""); // Numerator chars
var q = this['d']; // Denominator
var g;
var N = this['n'];
var D = this['d'];
if (!Fraction['REDUCE']) {
g = gcd(N, D);
N/= g;
D/= g;
}
var p = String(N).split(""); // Numerator chars
var t = 0; // Tmp var

@@ -638,4 +658,4 @@

var cycLen = cycleLen(this['n'], this['d']); // Cycle length
var cycOff = cycleStart(this['n'], this['d'], cycLen); // Cycle start
var cycLen = cycleLen(N, D); // Cycle length
var cycOff = cycleStart(N, D, cycLen); // Cycle start

@@ -667,6 +687,6 @@ var j = -1;

if (t >= q) {
ret[n]+= zeros + ((t / q) | 0); // Flush zeros, Add current digit
if (t >= D) {
ret[n]+= zeros + ((t / D) | 0); // Flush zeros, Add current digit
zeros = "";
t = t % q;
t = t % D;
} else if (n > 1) { // Add zeros to the zero buffer

@@ -673,0 +693,0 @@ zeros+= "0";

/*
Fraction.js v2.4.1 01/06/2015
Fraction.js v2.5.0 01/06/2015
http://www.xarg.org/2014/03/precise-calculations-in-javascript/

@@ -8,10 +8,10 @@

*/
(function(z){"use strict";function v(a,c){for(var g;c;)g=a,a=c,c=g%c;return a}function w(a,c){if(0===c%2)return w(a,c/2);if(0===c%5)return w(a,c/5);for(var g=1;2E3>g;g++)if(1===x(g,c))return g;return 0}function x(a,c){for(var g=10,b=1;0<a;g=g*g%c,a>>=1)a&1&&(b=b*g%c);return b}function k(a,c){var g=0,b=1,h=1,k=0,n=0,p=0,l=1,u=1,e=0,f=1,t=1,r=1;if(void 0!==a&&null!==a)if(void 0!==c)g=a,b=c,h=g*b;else switch(typeof a){case "object":"d"in a&&"n"in a?(g=a.n,b=a.d,"s"in a&&(g*=a.s)):0 in a?(g=a[0],1 in a&&(b=a[1])):
y();h=g*b;break;case "number":0>a&&(h=a,a=-a);if(0===a%1)g=a;else if(0<a){1<=a&&(u=Math.pow(10,Math.floor(1+Math.log(a)/Math.LN10)),a/=u);for(;1E7>=f&&1E7>=r;)if(g=(e+t)/(f+r),a===g){1E7>=f+r?(g=e+t,b=f+r):r>f?(g=t,b=r):(g=e,b=f);break}else a>g?(e+=t,f+=r):(t+=e,r+=f),1E7<f?(g=t,b=r):(g=e,b=f);g*=u}break;case "string":f=a.match(/\d+|./g);"-"===f[e]?(h=-1,e++):"+"===f[e]&&e++;if(f.length===e+1)n=q(f[e++],h);else if("."===f[e+1]||"."===f[e]){"."!==f[e]&&(k=q(f[e++],h));e++;if(e+1===f.length||"("===
f[e+1]&&")"===f[e+3]||"'"===f[e+1]&&"'"===f[e+3])n=q(f[e],h),l=Math.pow(10,f[e].length),e++;if("("===f[e]&&")"===f[e+2]||"'"===f[e]&&"'"===f[e+2])p=q(f[e+1],h),u=Math.pow(10,f[e+1].length)-1,e+=3}else"/"===f[e+1]||":"===f[e+1]?(n=q(f[e],h),l=q(f[e+2],1),e+=3):"/"===f[e+3]&&" "===f[e+1]&&(k=q(f[e],h),n=q(f[e+2],h),l=q(f[e+4],1),e+=5);if(f.length<=e){h=g=p+u*(k*l+n);b=l*u;break}default:y()}if(!b)throw"DIV/0";d.s=0>h?-1:1;d.n=Math.abs(g);d.d=Math.abs(b)}function q(a,c){isNaN(a=parseInt(a,10))&&y();return a*
c}function y(){throw"Invalid Param";}function b(a,c){if(!(this instanceof b))return new b(a,c);k(a,c);a=v(d.d,d.n);this.s=d.s;this.n=d.n/a;this.d=d.d/a}var d={n:0,d:0,s:0};b.prototype.s=1;b.prototype.n=0;b.prototype.d=1;b.prototype.abs=function(){return new b(this.n,this.d)};b.prototype.neg=function(){return new b(-this.s*this.n,this.d)};b.prototype.add=function(a,c){k(a,c);return new b(this.s*this.n*d.d+d.s*this.d*d.n,this.d*d.d)};b.prototype.sub=function(a,c){k(a,c);return new b(this.s*this.n*d.d-
d.s*this.d*d.n,this.d*d.d)};b.prototype.mul=function(a,c){k(a,c);return new b(this.s*d.s*this.n*d.n,this.d*d.d)};b.prototype.div=function(a,c){k(a,c);return new b(this.s*d.s*this.n*d.d,this.d*d.n)};b.prototype.clone=function(){return new b(this)};b.prototype.mod=function(a,c){if(void 0===a)return new b(this.s*this.n%this.d,1);k(a,c);0===d.n*this.d&&b(0,0);return new b(this.s*d.d*this.n%(d.n*this.d),d.d*this.d)};b.prototype.gcd=function(a,c){k(a,c);return new b(v(d.n,this.n),d.d*this.d/v(d.d,this.d))};
b.prototype.ceil=function(){return new b(Math.ceil(this.s*this.n/this.d),1)};b.prototype.floor=function(){return new b(Math.floor(this.s*this.n/this.d),1)};b.prototype.round=function(){return new b(Math.round(this.s*this.n/this.d),1)};b.prototype.inverse=function(){return new b(this.s*this.d,this.n)};b.prototype.pow=function(a){var c=this.d,b=this.n;0>a?(this.d=Math.pow(b,-a),this.n=Math.pow(c,-a)):(this.d=Math.pow(c,a),this.n=Math.pow(b,a));0===a%2&&(this.s=1);return this};b.prototype.equals=function(a,
c){k(a,c);return this.s*this.n*d.d===d.s*d.n*this.d};b.prototype.compare=function(a,c){k(a,c);var b=this.s*this.n*d.d-d.s*d.n*this.d;return(0<b)-(0>b)};b.prototype.divisible=function(a,c){k(a,c);return!!(d.n*this.d)&&!(this.n*d.d%(d.n*this.d))};b.prototype.valueOf=function(){return this.s*this.n/this.d};b.prototype.toFraction=function(){var a,c="",b=this.n,d=this.d;0>this.s&&(c+="-");1===d?c+=b:(a=Math.floor(b/d),0<a&&(c=c+a+" ",b=b%d),c+=b,c+="/",c+=d);return c};b.prototype.toLatex=function(){var a,
c="",b=this.n,d=this.d;0>this.s&&(c+="-");1===d?c+=b:(a=Math.floor(b/d),0<a&&(c+=a,b=b%d),c=c+"\\frac{"+b,c+="}{",c+=d,c+="}");return c};b.prototype.toString=function(){var a=String(this.n).split(""),b=this.d,d=0,m=[~this.s?"":"-","",""],h="",k=w(this.n,this.d),n;a:{n=this.d;for(var p=0;300>p;p++)if(x(p,n)===x(p+k,n)){n=p;break a}n=0}for(var p=-1,l=1,q=10+k+n+a.length,e=0;e<q;e++,d*=10){e<a.length?d+=Number(a[e]):(l=2,p++);if(0<k)if(p===n)m[l]+=h+"(",h="";else if(p===k+n){m[l]+=h+")";break}d>=b?(m[l]+=
h+(d/b|0),h="",d%=b):1<l?h+="0":m[l]&&(m[l]+="0")}m[0]+=m[1]||"0";return m[2]?m[0]+"."+m[2]:m[0]};"function"===typeof define&&define.amd?define([],function(){return b}):"object"===typeof exports?module.exports=b:z.Fraction=b})(this);
(function(y){"use strict";function u(a,b){for(var g;b;)g=a,a=b,b=g%b;return a}function v(a,b){if(0===b%2)return v(a,b/2);if(0===b%5)return v(a,b/5);for(var g=1;2E3>g;g++)if(1===w(g,b))return g;return 0}function w(a,b){for(var g=10,c=1;0<a;g=g*g%b,a>>=1)a&1&&(c=c*g%b);return c}function k(a,b){var g=0,c=1,h=1,p=0,m=0,k=0,l=1,r=1,e=0,f=1,t=1,q=1;if(void 0!==a&&null!==a)if(void 0!==b)g=a,c=b,h=g*c;else switch(typeof a){case "object":"d"in a&&"n"in a?(g=a.n,c=a.d,"s"in a&&(g*=a.s)):0 in a?(g=a[0],1 in a&&(c=a[1])):
x();h=g*c;break;case "number":0>a&&(h=a,a=-a);if(0===a%1)g=a;else if(0<a){1<=a&&(r=Math.pow(10,Math.floor(1+Math.log(a)/Math.LN10)),a/=r);for(;1E7>=f&&1E7>=q;)if(g=(e+t)/(f+q),a===g){1E7>=f+q?(g=e+t,c=f+q):q>f?(g=t,c=q):(g=e,c=f);break}else a>g?(e+=t,f+=q):(t+=e,q+=f),1E7<f?(g=t,c=q):(g=e,c=f);g*=r}break;case "string":f=a.match(/\d+|./g);"-"===f[e]?(h=-1,e++):"+"===f[e]&&e++;if(f.length===e+1)m=n(f[e++],h);else if("."===f[e+1]||"."===f[e]){"."!==f[e]&&(p=n(f[e++],h));e++;if(e+1===f.length||"("===
f[e+1]&&")"===f[e+3]||"'"===f[e+1]&&"'"===f[e+3])m=n(f[e],h),l=Math.pow(10,f[e].length),e++;if("("===f[e]&&")"===f[e+2]||"'"===f[e]&&"'"===f[e+2])k=n(f[e+1],h),r=Math.pow(10,f[e+1].length)-1,e+=3}else"/"===f[e+1]||":"===f[e+1]?(m=n(f[e],h),l=n(f[e+2],1),e+=3):"/"===f[e+3]&&" "===f[e+1]&&(p=n(f[e],h),m=n(f[e+2],h),l=n(f[e+4],1),e+=5);if(f.length<=e){h=g=k+r*(p*l+m);c=l*r;break}default:x()}if(!c)throw"DIV/0";d.s=0>h?-1:1;d.n=Math.abs(g);d.d=Math.abs(c)}function n(a,b){isNaN(a=parseInt(a,10))&&x();return a*
b}function x(){throw"Invalid Param";}function c(a,b){if(!(this instanceof c))return new c(a,b);k(a,b);c.REDUCE?a=u(d.d,d.n):a=1;this.s=d.s;this.n=d.n/a;this.d=d.d/a}var d={n:0,d:0,s:0};c.prototype.s=1;c.prototype.n=0;c.prototype.d=1;c.REDUCE=1;c.prototype.abs=function(){return new c(this.n,this.d)};c.prototype.neg=function(){return new c(-this.s*this.n,this.d)};c.prototype.add=function(a,b){k(a,b);return new c(this.s*this.n*d.d+d.s*this.d*d.n,this.d*d.d)};c.prototype.sub=function(a,b){k(a,b);return new c(this.s*
this.n*d.d-d.s*this.d*d.n,this.d*d.d)};c.prototype.mul=function(a,b){k(a,b);return new c(this.s*d.s*this.n*d.n,this.d*d.d)};c.prototype.div=function(a,b){k(a,b);return new c(this.s*d.s*this.n*d.d,this.d*d.n)};c.prototype.clone=function(){return new c(this)};c.prototype.mod=function(a,b){if(void 0===a)return new c(this.s*this.n%this.d,1);k(a,b);0===d.n*this.d&&c(0,0);return new c(this.s*d.d*this.n%(d.n*this.d),d.d*this.d)};c.prototype.gcd=function(a,b){k(a,b);return new c(u(d.n,this.n),d.d*this.d/
u(d.d,this.d))};c.prototype.ceil=function(){return new c(Math.ceil(this.s*this.n/this.d),1)};c.prototype.floor=function(){return new c(Math.floor(this.s*this.n/this.d),1)};c.prototype.round=function(){return new c(Math.round(this.s*this.n/this.d),1)};c.prototype.inverse=function(){return new c(this.s*this.d,this.n)};c.prototype.pow=function(a){var b=this.d,c=this.n;0>a?(this.d=Math.pow(c,-a),this.n=Math.pow(b,-a)):(this.d=Math.pow(b,a),this.n=Math.pow(c,a));0===a%2&&(this.s=1);return this};c.prototype.equals=
function(a,b){k(a,b);return this.s*this.n*d.d===d.s*d.n*this.d};c.prototype.compare=function(a,b){k(a,b);var c=this.s*this.n*d.d-d.s*d.n*this.d;return(0<c)-(0>c)};c.prototype.divisible=function(a,b){k(a,b);return!!(d.n*this.d)&&!(this.n*d.d%(d.n*this.d))};c.prototype.valueOf=function(){return this.s*this.n/this.d};c.prototype.toFraction=function(){var a,b="",c=this.n,d=this.d;0>this.s&&(b+="-");1===d?b+=c:(a=Math.floor(c/d),0<a&&(b=b+a+" ",c=c%d),b+=c,b+="/",b+=d);return b};c.prototype.toLatex=function(){var a,
b="",c=this.n,d=this.d;0>this.s&&(b+="-");1===d?b+=c:(a=Math.floor(c/d),0<a&&(b+=a,c=c%d),b=b+"\\frac{"+c,b+="}{",b+=d,b+="}");return b};c.prototype.toString=function(){var a,b=this.n,d=this.d;c.REDUCE||(a=u(b,d),b/=a,d/=a);a=String(b).split("");var k=0,h=[~this.s?"":"-","",""],p="",b=v(b,d),m;a:{for(m=0;300>m;m++)if(w(m,d)===w(m+b,d))break a;m=0}for(var n=-1,l=1,r=10+b+m+a.length,e=0;e<r;e++,k*=10){e<a.length?k+=Number(a[e]):(l=2,n++);if(0<b)if(n===m)h[l]+=p+"(",p="";else if(n===b+m){h[l]+=p+")";
break}k>=d?(h[l]+=p+(k/d|0),p="",k%=d):1<l?p+="0":h[l]&&(h[l]+="0")}h[0]+=h[1]||"0";return h[2]?h[0]+"."+h[2]:h[0]};"function"===typeof define&&define.amd?define([],function(){return c}):"object"===typeof exports?module.exports=c:y.Fraction=c})(this);
{
"name": "fraction.js",
"title": "fraction.js",
"version": "2.4.1",
"version": "2.5.0",
"homepage": "http://www.xarg.org/2014/03/precise-calculations-in-javascript/",

@@ -9,3 +9,3 @@ "bugs": "https://github.com/infusion/Fraction.js/issues",

"keywords": ["math", "fraction", "rational", "number"],
"author": "Robert Eisele <robert@xarg.org>",
"author": "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)",
"main": "fraction",

@@ -33,2 +33,2 @@ "readmeFilename": "README.md",

}
}
}

@@ -9,3 +9,3 @@ # Fraction.js - ℚ in JavaSript

Internally, numbers are represented as *numerator / denominator*, which adds just a little overhead. However, the library is written with performance in mind and outperforms any other implementation, as you can see [here](http://jsperf.com/convert-a-rational-number-to-a-babylonian-fractions/28).
Internally, numbers are represented as *numerator / denominator*, which adds just a little overhead. However, the library is written with performance in mind and outperforms any other implementation, as you can see [here](http://jsperf.com/convert-a-rational-number-to-a-babylonian-fractions/28). This basic data-type makes it the perfect basis for [Polynomial.js](https://github.com/infusion/Polynomial.js).

@@ -357,2 +357,30 @@ Convert decimal to fraction

Options
===
The library should work without configuring anything. However, there is one global option:
```
Fraction.REDUCE = <true|false>
```
It tells Fraction.js whether to reduce the fraction or not.
```
// Normal behavior
var f = Fraction(3, 6);
console.log(f); // 1/2
// Disable fraction reduction
Fraction.REDUCE = false;
var g = Fraction(3, 6);
console.log(g); // 3/6
// Back to normal behavior
Fraction.REDUCE = true;
var h = Fraction(g);
console.log(h); // 1/2
```
Exceptions

@@ -377,3 +405,3 @@ ===

Using Fraction.js at the browser
Using Fraction.js with the browser
===

@@ -380,0 +408,0 @@ <script src="fraction.js"></script>

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc