version bump 0.2.1: adding tests

This commit is contained in:
SheetJS 2013-12-25 23:14:09 -05:00
parent 70b4c2177e
commit 804bc32c2c
6 changed files with 200067 additions and 4 deletions

1
.npmignore Normal file

@ -0,0 +1 @@
./test_files

@ -1,3 +1,4 @@
/* frac.js (C) 2013 SheetJS -- http://sheetjs.com */
var frac = function(x, D, mixed) {
var n1 = Math.floor(x), d1 = 1;
var n2 = n1+1, d2 = 1;
@ -27,7 +28,7 @@ frac.cont = function cont(x, D, mixed) {
A = B|0;
P = A * P_1 + P_2;
Q = A * Q_1 + Q_2;
if((B - A) < 0.00000001) break;
if((B - A) < 0.0000000001) break;
B = 1 / (B - A);
P_2 = P_1; P_1 = P;
Q_2 = Q_1; Q_1 = Q;

35
frac.md

@ -16,6 +16,7 @@ where `quotient == 0` for improper fractions. The interpretation is
and `quotient <= x` for negative `x`.
```js>frac.js
/* frac.js (C) 2013 SheetJS -- http://sheetjs.com */
var frac = function(x, D, mixed) {
```
@ -122,7 +123,7 @@ Note that the variables are implicitly indexed at `k` (so `B` refers to `b_k`):
> b_{k+1} = (b_{k} - a_{k})^{-1}
```
if((B - A) < 0.00000001) break;
if((B - A) < 0.0000000001) break;
```
At the end of each iteration, advance `k` by one step:
@ -159,8 +160,38 @@ if(typeof module !== 'undefined') module.exports = frac;
# Tests
```js>test.js
var fs = require('fs'), assert = require('assert');
var frac;
describe('source', function() { it('should load', function() { frac = require('./'); }); });
function line(o,j,m) {
it(j, function(done) {
var d, q, qq;
for(var i = j*100; i < m-3 && i < (j+1)*100; ++i) {
d = o[i].split("\t");
q = frac.cont(Number(d[0]), 9, true);
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? q[1] + "/" + q[2] : " ") : "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 ";
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 ";
assert.equal(qq, d[3], d[3] + " 3");
}
done();
});
}
function parsetest(o) {
for(var j = 0, m = o.length-3; j < m/100; ++j) line(o,j,m);
}
describe('xl.00001.tsv', function() {
var o = fs.readFileSync('./test_files/xl.00001.tsv', 'utf-8').split("\n");
parsetest(o);
});
```
# Miscellany
@ -179,7 +210,7 @@ test:
```json>package.json
{
"name": "frac",
"version": "0.2.0",
"version": "0.2.1",
"author": "SheetJS",
"description": "Rational approximation with bounded denominator",
"keywords": [ "math", "fraction", "rational", "approximation" ],

@ -1,6 +1,6 @@
{
"name": "frac",
"version": "0.2.0",
"version": "0.2.1",
"author": "SheetJS",
"description": "Rational approximation with bounded denominator",
"keywords": [ "math", "fraction", "rational", "approximation" ],

30
test.js

@ -1,2 +1,32 @@
var fs = require('fs'), assert = require('assert');
var frac;
describe('source', function() { it('should load', function() { frac = require('./'); }); });
function line(o,j,m) {
it(j, function(done) {
var d, q, qq;
for(var i = j*100; i < m-3 && i < (j+1)*100; ++i) {
d = o[i].split("\t");
q = frac.cont(Number(d[0]), 9, true);
qq = (q[0]||q[1]) ? (q[0] || "") + " " + (q[1] ? q[1] + "/" + q[2] : " ") : "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 ";
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 ";
assert.equal(qq, d[3], d[3] + " 3");
}
done();
});
}
function parsetest(o) {
for(var j = 0, m = o.length-3; j < m/100; ++j) line(o,j,m);
}
describe('xl.00001.tsv', function() {
var o = fs.readFileSync('./test_files/xl.00001.tsv', 'utf-8').split("\n");
parsetest(o);
});

200000
test_files/xl.00001.tsv Normal file

File diff suppressed because it is too large Load Diff