Socket
Socket
Sign inDemoInstall

fraction.js

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fraction.js - npm Package Compare versions

Comparing version 2.7.0 to 2.8.0

4

bower.json
{
"name": "fraction.js",
"main": "fraction.js",
"version": "2.7.0",
"version": "2.8.0",
"homepage": "http://www.xarg.org/2014/03/precise-calculations-in-javascript/",
"description": "A rational number library",
"keywords": [
"math", "fraction", "rational", "number", "parser"
"math", "fraction", "rational", "number", "parser", "rational numbers"
],

@@ -10,0 +10,0 @@ "authors": [

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

@@ -51,5 +51,5 @@ *

var P = {
"s": 1,
"n": 0,
"d": 0,
"s": 0
"d": 1
};

@@ -60,3 +60,3 @@

if (isNaN(n = parseInt(n, 10))) {
parser_exit();
thorwInvalidParam();
}

@@ -66,3 +66,3 @@ return n * s;

function parser_exit() {
function thorwInvalidParam() {
throw "Invalid Param";

@@ -103,3 +103,3 @@ }

} else {
parser_exit();
thorwInvalidParam();
}

@@ -222,3 +222,3 @@ s = n * d;

default:
parser_exit();
thorwInvalidParam();
}

@@ -248,8 +248,8 @@

while (d % 2 === 0)
d/= 2;
for (; d % 2 === 0;
d/= 2) {}
for (; d % 5 === 0;
d/= 5) {}
while (d % 5 === 0)
d/= 5;
if (d === 1) // Catch non-cyclic numbers

@@ -292,9 +292,12 @@ return 0;

var gcd = function(a, b) {
var t;
while (b) {
t = a;
a = b;
b = t % b;
if (!a) return b;
if (!b) return a;
while (1) {
a%= b;
if (!a) return b;
b%= a;
if (!b) return a;
}
return a;
};

@@ -603,3 +606,3 @@

str+= " ";
n = n % d;
n %= d;
}

@@ -631,8 +634,2 @@

} else {
whole = Math.floor(n / d);
if (whole > 0) {
str+= whole;
n = n % d;
}
str+= "\\frac{";

@@ -639,0 +636,0 @@ str+= n;

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

@@ -8,9 +8,9 @@

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

@@ -10,0 +10,0 @@ "main": "fraction",

@@ -7,33 +7,24 @@ # Fraction.js - ℚ in JavaSript

Tired of inprecise numbers represented by doubles, which have to store ratios and irrational numbers like PI or sqrt(2) the same way? If you need more precision or just want a fraction as a result, have a look at *Fraction.js*!
Tired of inprecise numbers represented by doubles, which have to store rational and irrational numbers like PI or sqrt(2) the same way? Obviously the following problem is preventable:
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).
Convert decimal to fraction
===
The simplest job for fraction.js is to get a fraction out of a decimal:
```javascript
var Fraction = require('fraction.js');
var x = new Fraction(1.88);
var res = x.toFraction(); // String "1 22/25"
1 / 98 * 98 // = 0.9999999999999999
```
Laplace Probability
===
Simple example. What's the probability of throwing a 3, and 1 or 4, and 2 or 4 or 6 with a fair dice?
If you need more precision or just want a fraction as a result, have a look at *Fraction.js*:
P({3}):
```javascript
var p = new Fraction([3].length, 6).toString(); // 0.1(6)
```
var Fraction = require('fraction.js');
P({1, 4}):
```javascript
var p = new Fraction([1, 4].length, 6).toString(); // 0.(3)
Fraction(a).div(98).mul(98) // = 1
```
P({2, 4, 6}):
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).
Convert decimal to fraction
===
The simplest job for fraction.js is to get a fraction out of a decimal:
```javascript
var p = new Fraction([2, 4, 6].length, 6).toString(); // 0.5
var x = new Fraction(1.88);
var res = x.toFraction(); // String "1 22/25"
```

@@ -67,4 +58,25 @@

Another example might be to calculate degrees/minutes/seconds into precise decimal representations:
Laplace Probability
===
Simple example. What's the probability of throwing a 3, and 1 or 4, and 2 or 4 or 6 with a fair dice?
P({3}):
```javascript
var p = new Fraction([3].length, 6).toString(); // 0.1(6)
```
P({1, 4}):
```javascript
var p = new Fraction([1, 4].length, 6).toString(); // 0.(3)
```
P({2, 4, 6}):
```javascript
var p = new Fraction([2, 4, 6].length, 6).toString(); // 0.5
```
Convert degrees/minutes/seconds to precise rational representation:
===
57+45/60+17/3600

@@ -348,3 +360,3 @@ ```javascript

---
Generates an exact LaTeX representation of the actual object.
Generates an exact LaTeX representation of the actual object. You can see a [live demo](http://www.xarg.org/2014/03/precise-calculations-in-javascript/) on my blog.

@@ -351,0 +363,0 @@ String toFraction()

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

@@ -9,2 +9,5 @@ *

// NOTE: This is a nice example, but a stable version of this is served with Polynomial.js:
// https://github.com/infusion/Polynomial.js
var Fraction = require('../fraction.min.js');

@@ -11,0 +14,0 @@

@@ -18,2 +18,5 @@ var assert = require('assert');

}, {
set: .333,
expect: "0.333"
}, {
set: 1.1,

@@ -112,2 +115,5 @@ expect: "1.1"

}, {
set: "355/113",
expect: "3.(1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168)" // Yay, a better PI
}, {
set: "3 1/7",

@@ -164,2 +170,26 @@ expect: '3.(142857)'

}, {
label: "0.4 / 0.1",
set: .4,
fn: "div",
param: ".1",
expect: "4"
}, {
label: "1 / -.1",
set: 1,
fn: "div",
param: "-.1",
expect: "-10"
}, {
label: "1 - (-1)",
set: 1,
fn: "sub",
param: "-1",
expect: "2"
}, {
label: "1 + (-1)",
set: 1,
fn: "add",
param: "-1",
expect: "0"
}, {
label: "-187 % 12",

@@ -201,2 +231,8 @@ set: '-187',

}, {
label: "13/26 mod 1",
set: '13/26',
fn: "mod",
param: "1.000",
expect: "0.5"
}, {
label: "381.(33411) % 1", // Extract fraction part of a number

@@ -921,12 +957,12 @@ set: '381.(33411)',

it("Should pass 123.'3' = 123\\frac{1}{3}", function() {
it("Should pass 123.'3' = \\frac{370}{3}", function() {
var tmp = new Fraction("123.'3'");
assert.equal("123\\frac{1}{3}", tmp.toLatex());
assert.equal("\\frac{370}{3}", tmp.toLatex());
});
it("Should pass 1.'3' = 1\\frac{1}{3}", function() {
it("Should pass 1.'3' = \\frac{4}{3}", function() {
var tmp = new Fraction("1.'3'");
assert.equal("1\\frac{1}{3}", tmp.toLatex());
assert.equal("\\frac{4}{3}", tmp.toLatex());
});

@@ -978,2 +1014,10 @@

});
it('Should work with large calculations', function() {
var x = Fraction(1123875);
var y = Fraction(1238750184);
var z = Fraction(1657134);
var r = Fraction(77344464613500, 92063);
assert.equal(x.mul(y).div(z).toFraction(), r.toFraction());
});
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc