Comparing version 1.0.4 to 1.0.5
/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ | ||
var frac = function(x, D, mixed) { | ||
var frac = function frac(x, D, mixed) { | ||
var n1 = Math.floor(x), d1 = 1; | ||
@@ -31,3 +31,3 @@ var n2 = n1+1, d2 = 1; | ||
Q = A * Q_1 + Q_2; | ||
if((B - A) < 0.000000005) break; | ||
if((B - A) < 0.00000005) break; | ||
B = 1 / (B - A); | ||
@@ -34,0 +34,0 @@ P_2 = P_1; P_1 = P; |
@@ -1,1 +0,2 @@ | ||
var frac=function(x,D,mixed){var n1=Math.floor(x),d1=1;var n2=n1+1,d2=1;if(x!==n1)while(d1<=D&&d2<=D){var m=(n1+n2)/(d1+d2);if(x===m){if(d1+d2<=D){d1+=d2;n1+=n2;d2=D+1}else if(d1>d2)d2=D+1;else d1=D+1;break}else if(x<m){n2=n1+n2;d2=d1+d2}else{n1=n1+n2;d1=d1+d2}}if(d1>D){d1=d2;n1=n2}if(!mixed)return[0,n1,d1];var q=Math.floor(n1/d1);return[q,n1-q*d1,d1]};frac.cont=function cont(x,D,mixed){var sgn=x<0?-1:1;var B=x*sgn;var P_2=0,P_1=1,P=0;var Q_2=1,Q_1=0,Q=0;var A=Math.floor(B);while(Q_1<D){A=Math.floor(B);P=A*P_1+P_2;Q=A*Q_1+Q_2;if(B-A<5e-9)break;B=1/(B-A);P_2=P_1;P_1=P;Q_2=Q_1;Q_1=Q}if(Q>D){if(Q_1>D){Q=Q_2;P=P_2}else{Q=Q_1;P=P_1}}if(!mixed)return[0,sgn*P,Q];var q=Math.floor(sgn*P/Q);return[q,sgn*P-q*Q,Q]};if(typeof module!=="undefined"&&typeof DO_NOT_EXPORT_FRAC==="undefined")module.exports=frac; | ||
/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ | ||
var frac=function frac(x,D,mixed){var n1=Math.floor(x),d1=1;var n2=n1+1,d2=1;if(x!==n1)while(d1<=D&&d2<=D){var m=(n1+n2)/(d1+d2);if(x===m){if(d1+d2<=D){d1+=d2;n1+=n2;d2=D+1}else if(d1>d2)d2=D+1;else d1=D+1;break}else if(x<m){n2=n1+n2;d2=d1+d2}else{n1=n1+n2;d1=d1+d2}}if(d1>D){d1=d2;n1=n2}if(!mixed)return[0,n1,d1];var q=Math.floor(n1/d1);return[q,n1-q*d1,d1]};frac.cont=function cont(x,D,mixed){var sgn=x<0?-1:1;var B=x*sgn;var P_2=0,P_1=1,P=0;var Q_2=1,Q_1=0,Q=0;var A=Math.floor(B);while(Q_1<D){A=Math.floor(B);P=A*P_1+P_2;Q=A*Q_1+Q_2;if(B-A<5e-8)break;B=1/(B-A);P_2=P_1;P_1=P;Q_2=Q_1;Q_1=Q}if(Q>D){if(Q_1>D){Q=Q_2;P=P_2}else{Q=Q_1;P=P_1}}if(!mixed)return[0,sgn*P,Q];var q=Math.floor(sgn*P/Q);return[q,sgn*P-q*Q,Q]};if(typeof module!=="undefined"&&typeof DO_NOT_EXPORT_FRAC==="undefined")module.exports=frac; |
/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ | ||
var frac = function(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array<number>*/ { | ||
var frac = function frac(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array<number>*/ { | ||
var n1 = Math.floor(x), d1 = 1; | ||
@@ -31,3 +31,3 @@ var n2 = n1+1, d2 = 1; | ||
Q = A * Q_1 + Q_2; | ||
if((B - A) < 0.000000005) break; | ||
if((B - A) < 0.00000005) break; | ||
B = 1 / (B - A); | ||
@@ -34,0 +34,0 @@ P_2 = P_1; P_1 = P; |
/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ | ||
var frac = function(x, D, mixed) { | ||
var frac = function frac(x, D, mixed) { | ||
var n1 = Math.floor(x), d1 = 1; | ||
@@ -31,3 +31,3 @@ var n2 = n1+1, d2 = 1; | ||
Q = A * Q_1 + Q_2; | ||
if((B - A) < 0.000000005) break; | ||
if((B - A) < 0.00000005) break; | ||
B = 1 / (B - A); | ||
@@ -34,0 +34,0 @@ P_2 = P_1; P_1 = P; |
46
frac.md
@@ -26,3 +26,3 @@ # Target | ||
/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ | ||
var frac = function(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array<number>*/ { | ||
var frac = function frac(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array<number>*/ { | ||
``` | ||
@@ -95,3 +95,3 @@ | ||
> Record the sign of x, take b0=|x|, p_{-2}=0, p_{-1}=1, q_{-2}=1, q_{-1}=0 | ||
> Record the sign of x, take `b0=|x|, p_{-2}=0, p_{-1}=1, q_{-2}=1, q_{-1}=0` | ||
@@ -107,4 +107,4 @@ Note that the variables are implicitly indexed at `k` (so `B` refers to `b_k`): | ||
`A` should be the floor of `B`. Originally the bit-or trick was used, but this is not correct | ||
for the range `B>=2**32`. | ||
`A` should be the floor of `B`. Originally the bit-or trick was used, but this | ||
is not correct for the range `B>=2**32`. | ||
@@ -117,4 +117,4 @@ ``` | ||
> ... for k = 0,1,...,K, where K is the first instance of k where | ||
> either q_{k+1} > Q or b_{k+1} is undefined (b_k = a_k). | ||
> ... for `k = 0,1,...,K`, where `K` is the first instance of `k` where | ||
> either `q_{k+1} > Q` or `b_{k+1}` is undefined (`b_k = a_k`). | ||
@@ -125,3 +125,3 @@ ``` | ||
> a_k = [b_k], i.e., the greatest integer <= b_k | ||
> `a_k = [b_k]`, i.e., the greatest integer `<= b_k` | ||
@@ -132,4 +132,4 @@ ``` | ||
> p_k = a_k p_{k-1} + p_{k-2} | ||
> q_k = a_k q_{k-1} + q_{k-2} | ||
> `p_k = a_k p_{k-1} + p_{k-2}` | ||
> `q_k = a_k q_{k-1} + q_{k-2}` | ||
@@ -141,6 +141,6 @@ ``` | ||
> b_{k+1} = (b_{k} - a_{k})^{-1} | ||
> `b_{k+1} = (b_{k} - a_{k})^{-1}` | ||
``` | ||
if((B - A) < 0.000000005) break; | ||
if((B - A) < 0.00000005) break; | ||
``` | ||
@@ -194,17 +194,21 @@ | ||
function xlline(o,j,m,w) { | ||
it(j, function() { | ||
var d, q, qq; | ||
it(j.toString(), function() { | ||
var d, q, qq, f = 0.1; | ||
var q0 = 0, q1 = 0, q2 = 0 | ||
for(var i = j*w; i < m-3 && i < (j+1)*w; ++i) { | ||
d = o[i].split("\t"); | ||
if(d.length < 3) continue; | ||
f = parseFloat(d[0]); | ||
q = frac.cont(Number(d[0]), 9, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? q[1] + "/" + q[2] : " ") : "0 "; | ||
q = frac.cont(f, 9, true); | ||
q0 = q[0]; q1 = q[1]; q2 = q[2]; | ||
qq = (q0!=0||q1!=0) ? (q0!=0 ? q0.toString() : "") + " " + (q1!=0 ? q1.toString() + "/" + q2.toString() : " ") : "0 "; | ||
assert.equal(qq, d[1], d[1] + " 1"); | ||
q = frac.cont(Number(d[0]), 99, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? (q[1] < 10 ? " " : "") + q[1] + "/" + q[2] + (q[2]<10?" ":"") : " ") : "0 "; | ||
q = frac.cont(f, 99, true); | ||
qq = (q[0]!=0||q[1]!=0) ? (q[0]!=0 ? q[0].toString() : "") + " " + (q[1]!=0 ? (q[1] < 10 ? " " : "") + q[1].toString() + "/" + q[2].toString() + (q[2]<10?" ":"") : " ") : "0 "; | ||
assert.equal(qq, d[2], d[2] + " 2"); | ||
q = frac.cont(Number(d[0]), 999, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? (q[1] < 100 ? " " : "") + (q[1] < 10 ? " " : "") + q[1] + "/" + q[2] + (q[2]<10?" ":"") + (q[2]<100?" ":""): " ") : "0 "; | ||
q = frac.cont(f, 999, true); | ||
qq = (q[0]!=0||q[1]!=0) ? (q[0]!=0 ? q[0].toString() : "") + " " + (q[1]!=0 ? (q[1] < 100 ? " " : "") + (q[1] < 10 ? " " : "") + q[1].toString() + "/" + q[2].toString() + (q[2]<10?" ":"") + (q[2]<100?" ":""): " ") : "0 "; | ||
assert.equal(qq, d[3], d[3] + " 3"); | ||
@@ -250,3 +254,3 @@ } | ||
"name": "frac", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "SheetJS", | ||
@@ -264,3 +268,3 @@ "description": "Rational approximation with bounded denominator", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "make test" | ||
}, | ||
@@ -267,0 +271,0 @@ "config": { |
{ | ||
"name": "frac", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "SheetJS", | ||
@@ -16,3 +16,3 @@ "description": "Rational approximation with bounded denominator", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "make test" | ||
}, | ||
@@ -19,0 +19,0 @@ "config": { |
@@ -10,2 +10,3 @@ # frac | ||
## Installation | ||
@@ -17,7 +18,11 @@ | ||
$ npm install frac | ||
```bash | ||
$ npm install frac | ||
``` | ||
In the browser: | ||
<script src="frac.js"></script> | ||
```html | ||
<script src="frac.js"></script> | ||
``` | ||
@@ -32,3 +37,5 @@ The script will manipulate `module.exports` if available (e.g. in a CommonJS | ||
$ pip install frac | ||
```bash | ||
$ pip install frac | ||
``` | ||
@@ -73,3 +80,3 @@ ## Usage | ||
`frac.cont` implements Aberth algorithm | ||
`frac.cont` implements Aberth algorithm. | ||
@@ -80,11 +87,11 @@ For example: | ||
>>> import frac | ||
>>> frac.med(1.3, 9) # [ 0, 9, 7 ] | ||
>>> frac.med(1.3, 9, True) # [ 1, 2, 7 ] | ||
>>> frac.med(-1.3, 9) # [ 0, -9, 7 ] | ||
>>> frac.med(-1.3, 9, True) # [ -2, 5, 7 ] | ||
>>> frac.med(1.3, 9) ## [ 0, 9, 7 ] ## 1.3 ~ 9/7 | ||
>>> frac.med(1.3, 9, True) ## [ 1, 2, 7 ] ## 1.3 ~ 1 + 2/7 | ||
>>> frac.med(-1.3, 9) ## [ 0, -9, 7 ] ## -1.3 ~ -9/7 | ||
>>> frac.med(-1.3, 9, True) ## [ -2, 5, 7 ] ## -1.3 ~ -2 + 5/7 | ||
>>> frac.cont(1.3, 9) # [ 0, 4, 3 ] | ||
>>> frac.cont(1.3, 9, True) # [ 1, 1, 3 ] | ||
>>> frac.cont(-1.3, 9) # [ 0, -4, 3 ] | ||
>>> frac.cont(-1.3, 9, True) # [ -2, 2, 3 ] | ||
>>> frac.cont(1.3, 9) ## [ 0, 4, 3 ] ## 1.3 ~ 4/3 | ||
>>> frac.cont(1.3, 9, True) ## [ 1, 1, 3 ] ## 1.3 ~ 1 + 1/3 | ||
>>> frac.cont(-1.3, 9) ## [ 0, -4, 3 ] ## -1.3 ~ -4/3 | ||
>>> frac.cont(-1.3, 9, True) ## [ -2, 2, 3 ] ## -1.3 ~ -2 + 2/3 | ||
``` | ||
@@ -94,10 +101,14 @@ | ||
The test TSV baselines in the `test_files` directory have four columns: | ||
- Column A contains the raw values | ||
- Column B format "Up to one digit (1/4)" (`denominator = 9`) | ||
- Column C format "Up to two digits (21/25)" (`denominator = 99`) | ||
- Column D format "Up to three digits (312/943)" (`denominator = 999`) | ||
`make test` will run the node-based tests. | ||
Tests generated from Excel have 4 columns. To produce a similar test: | ||
`make pytest` will run the python tests against the system Python version. | ||
- Column A contains the raw values | ||
- Column B format "Up to one digit (1/4)" | ||
- Column C format "Up to two digits (21/25)" | ||
- Column D format "Up to three digits (312/943)" | ||
`make pypytest` will run the python tests against `pypy` if installed | ||
@@ -104,0 +115,0 @@ ## License |
20
test.js
@@ -13,17 +13,21 @@ var fs = require('fs'), assert = require('assert'); | ||
function xlline(o,j,m,w) { | ||
it(j, function() { | ||
var d, q, qq; | ||
it(j.toString(), function() { | ||
var d, q, qq, f = 0.1; | ||
var q0 = 0, q1 = 0, q2 = 0 | ||
for(var i = j*w; i < m-3 && i < (j+1)*w; ++i) { | ||
d = o[i].split("\t"); | ||
if(d.length < 3) continue; | ||
f = parseFloat(d[0]); | ||
q = frac.cont(Number(d[0]), 9, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? q[1] + "/" + q[2] : " ") : "0 "; | ||
q = frac.cont(f, 9, true); | ||
q0 = q[0]; q1 = q[1]; q2 = q[2]; | ||
qq = (q0!=0||q1!=0) ? (q0!=0 ? q0.toString() : "") + " " + (q1!=0 ? q1.toString() + "/" + q2.toString() : " ") : "0 "; | ||
assert.equal(qq, d[1], d[1] + " 1"); | ||
q = frac.cont(Number(d[0]), 99, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? (q[1] < 10 ? " " : "") + q[1] + "/" + q[2] + (q[2]<10?" ":"") : " ") : "0 "; | ||
q = frac.cont(f, 99, true); | ||
qq = (q[0]!=0||q[1]!=0) ? (q[0]!=0 ? q[0].toString() : "") + " " + (q[1]!=0 ? (q[1] < 10 ? " " : "") + q[1].toString() + "/" + q[2].toString() + (q[2]<10?" ":"") : " ") : "0 "; | ||
assert.equal(qq, d[2], d[2] + " 2"); | ||
q = frac.cont(Number(d[0]), 999, true); | ||
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? (q[1] < 100 ? " " : "") + (q[1] < 10 ? " " : "") + q[1] + "/" + q[2] + (q[2]<10?" ":"") + (q[2]<100?" ":""): " ") : "0 "; | ||
q = frac.cont(f, 999, true); | ||
qq = (q[0]!=0||q[1]!=0) ? (q[0]!=0 ? q[0].toString() : "") + " " + (q[1]!=0 ? (q[1] < 100 ? " " : "") + (q[1] < 10 ? " " : "") + q[1].toString() + "/" + q[2].toString() + (q[2]<10?" ":"") + (q[2]<100?" ":""): " ") : "0 "; | ||
assert.equal(qq, d[3], d[3] + " 3"); | ||
@@ -30,0 +34,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32321
189
121