diff --git a/.npmignore b/.npmignore index f7a29cd..6812b42 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1 @@ -./test_files +test_files/*.tsv diff --git a/frac.js b/frac.js index 87d093a..91df23e 100644 --- a/frac.js +++ b/frac.js @@ -23,9 +23,9 @@ frac.cont = function cont(x, D, mixed) { var B = x * sgn; var P_2 = 0, P_1 = 1, P = 0; var Q_2 = 1, Q_1 = 0, Q = 0; - var A = B|0; + var A = Math.floor(B); while(Q_1 < D) { - A = B|0; + A = Math.floor(B); P = A * P_1 + P_2; Q = A * Q_1 + Q_2; if((B - A) < 0.0000000005) break; diff --git a/frac.md b/frac.md index 55e6927..5c02469 100644 --- a/frac.md +++ b/frac.md @@ -94,7 +94,13 @@ Note that the variables are implicitly indexed at `k` (so `B` refers to `b_k`): var B = x * sgn; var P_2 = 0, P_1 = 1, P = 0; var Q_2 = 1, Q_1 = 0, Q = 0; - var A = B|0; +``` + +`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`. + +``` + var A = Math.floor(B); ``` > Iterate @@ -109,7 +115,7 @@ Note that the variables are implicitly indexed at `k` (so `B` refers to `b_k`): > a_k = [b_k], i.e., the greatest integer <= b_k ``` - A = B|0; + A = Math.floor(B); ``` > p_k = a_k p_{k-1} + p_{k-2} @@ -167,7 +173,8 @@ var xltestfiles=[ ['xl.00001.tsv', 10000], ['xl.0001.tsv', 10000], ['xl.001.tsv', 10000], - ['xl.01.tsv', 10000] + ['xl.01.tsv', 10000], + ['oddities.tsv', 25] ]; function xlline(o,j,m,w) { @@ -220,7 +227,7 @@ test: ```json>package.json { "name": "frac", - "version": "0.3.0", + "version": "0.3.1", "author": "SheetJS", "description": "Rational approximation with bounded denominator", "keywords": [ "math", "fraction", "rational", "approximation" ], @@ -241,7 +248,7 @@ test: And to make sure that test files are not included in npm: ```>.npmignore -./test_files +test_files/*.tsv ``` ```>.gitignore diff --git a/package.json b/package.json index 166fd22..cbf05e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "frac", - "version": "0.3.0", + "version": "0.3.1", "author": "SheetJS", "description": "Rational approximation with bounded denominator", "keywords": [ "math", "fraction", "rational", "approximation" ], diff --git a/test.js b/test.js index 8ce24d1..18ca4ff 100644 --- a/test.js +++ b/test.js @@ -5,7 +5,8 @@ var xltestfiles=[ ['xl.00001.tsv', 10000], ['xl.0001.tsv', 10000], ['xl.001.tsv', 10000], - ['xl.01.tsv', 10000] + ['xl.01.tsv', 10000], + ['oddities.tsv', 25] ]; function xlline(o,j,m,w) { diff --git a/test_files/oddities.tsv b/test_files/oddities.tsv new file mode 100644 index 0000000..0b6d4e2 --- /dev/null +++ b/test_files/oddities.tsv @@ -0,0 +1,126 @@ +12345678900000 12345678900000 12345678900000 12345678900000 +12345678900000.1 12345678900000 12345678900000 1/10 12345678900000 51/512 +12345678900000.2 12345678900000 1/5 12345678900000 1/5 12345678900000 51/256 +12345678900000.3 12345678900000 1/3 12345678900000 3/10 12345678900000 77/256 +12345678900000.4 12345678900000 2/5 12345678900000 2/5 12345678900000 205/512 +12345678900000.5 12345678900000 1/2 12345678900000 1/2 12345678900000 1/2 +12345678900000.6 12345678900000 3/5 12345678900000 3/5 12345678900000 307/512 +12345678900000.7 12345678900000 2/3 12345678900000 7/10 12345678900000 179/256 +12345678900000.8 12345678900000 4/5 12345678900000 4/5 12345678900000 205/256 +12345678900000.9 12345678900001 12345678900000 9/10 12345678900000 461/512 +12345678900001.0 12345678900001 12345678900001 12345678900001 +12345678900001.1 12345678900001 12345678900001 1/10 12345678900001 51/512 +12345678900001.2 12345678900001 1/5 12345678900001 1/5 12345678900001 51/256 +12345678900001.3 12345678900001 1/3 12345678900001 3/10 12345678900001 77/256 +12345678900001.4 12345678900001 2/5 12345678900001 2/5 12345678900001 205/512 +12345678900001.5 12345678900001 1/2 12345678900001 1/2 12345678900001 1/2 +12345678900001.6 12345678900001 3/5 12345678900001 3/5 12345678900001 307/512 +12345678900001.7 12345678900001 2/3 12345678900001 7/10 12345678900001 179/256 +12345678900001.8 12345678900001 4/5 12345678900001 4/5 12345678900001 205/256 +12345678900001.9 12345678900002 12345678900001 9/10 12345678900001 461/512 +12345678900002.0 12345678900002 12345678900002 12345678900002 +12345678900002.1 12345678900002 12345678900002 1/10 12345678900002 51/512 +12345678900002.2 12345678900002 1/5 12345678900002 1/5 12345678900002 51/256 +12345678900002.3 12345678900002 1/3 12345678900002 3/10 12345678900002 77/256 +12345678900002.4 12345678900002 2/5 12345678900002 2/5 12345678900002 205/512 +1234567890000 1234567890000 1234567890000 1234567890000 +1234567890000.1 1234567890000 1/9 1234567890000 1/10 1234567890000 1/10 +1234567890000.2 1234567890000 1/5 1234567890000 1/5 1234567890000 1/5 +1234567890000.3 1234567890000 1/3 1234567890000 3/10 1234567890000 3/10 +1234567890000.4 1234567890000 2/5 1234567890000 2/5 1234567890000 2/5 +1234567890000.5 1234567890000 1/2 1234567890000 1/2 1234567890000 1/2 +1234567890000.6 1234567890000 3/5 1234567890000 3/5 1234567890000 3/5 +1234567890000.7 1234567890000 2/3 1234567890000 7/10 1234567890000 7/10 +1234567890000.8 1234567890000 4/5 1234567890000 4/5 1234567890000 4/5 +1234567890000.9 1234567890000 8/9 1234567890000 9/10 1234567890000 9/10 +1234567890001.0 1234567890001 1234567890001 1234567890001 +1234567890001.1 1234567890001 1/9 1234567890001 1/10 1234567890001 1/10 +1234567890001.2 1234567890001 1/5 1234567890001 1/5 1234567890001 1/5 +1234567890001.3 1234567890001 1/3 1234567890001 3/10 1234567890001 3/10 +1234567890001.4 1234567890001 2/5 1234567890001 2/5 1234567890001 2/5 +1234567890001.5 1234567890001 1/2 1234567890001 1/2 1234567890001 1/2 +1234567890001.6 1234567890001 3/5 1234567890001 3/5 1234567890001 3/5 +1234567890001.7 1234567890001 2/3 1234567890001 7/10 1234567890001 7/10 +1234567890001.8 1234567890001 4/5 1234567890001 4/5 1234567890001 4/5 +1234567890001.9 1234567890001 8/9 1234567890001 9/10 1234567890001 9/10 +1234567890002.0 1234567890002 1234567890002 1234567890002 +1234567890002.1 1234567890002 1/9 1234567890002 1/10 1234567890002 1/10 +1234567890002.2 1234567890002 1/5 1234567890002 1/5 1234567890002 1/5 +1234567890002.3 1234567890002 1/3 1234567890002 3/10 1234567890002 3/10 +1234567890002.4 1234567890002 2/5 1234567890002 2/5 1234567890002 2/5 +123456789000 123456789000 123456789000 123456789000 +123456789000.1 123456789000 1/9 123456789000 1/10 123456789000 1/10 +123456789000.2 123456789000 1/5 123456789000 1/5 123456789000 1/5 +123456789000.3 123456789000 1/3 123456789000 3/10 123456789000 3/10 +123456789000.4 123456789000 2/5 123456789000 2/5 123456789000 2/5 +123456789000.5 123456789000 1/2 123456789000 1/2 123456789000 1/2 +123456789000.6 123456789000 3/5 123456789000 3/5 123456789000 3/5 +123456789000.7 123456789000 2/3 123456789000 7/10 123456789000 7/10 +123456789000.8 123456789000 4/5 123456789000 4/5 123456789000 4/5 +123456789000.9 123456789000 8/9 123456789000 9/10 123456789000 9/10 +123456789001.0 123456789001 123456789001 123456789001 +123456789001.1 123456789001 1/9 123456789001 1/10 123456789001 1/10 +123456789001.2 123456789001 1/5 123456789001 1/5 123456789001 1/5 +123456789001.3 123456789001 1/3 123456789001 3/10 123456789001 3/10 +123456789001.4 123456789001 2/5 123456789001 2/5 123456789001 2/5 +123456789001.5 123456789001 1/2 123456789001 1/2 123456789001 1/2 +123456789001.6 123456789001 3/5 123456789001 3/5 123456789001 3/5 +123456789001.7 123456789001 2/3 123456789001 7/10 123456789001 7/10 +123456789001.8 123456789001 4/5 123456789001 4/5 123456789001 4/5 +123456789001.9 123456789001 8/9 123456789001 9/10 123456789001 9/10 +123456789002.0 123456789002 123456789002 123456789002 +123456789002.1 123456789002 1/9 123456789002 1/10 123456789002 1/10 +123456789002.2 123456789002 1/5 123456789002 1/5 123456789002 1/5 +123456789002.3 123456789002 1/3 123456789002 3/10 123456789002 3/10 +123456789002.4 123456789002 2/5 123456789002 2/5 123456789002 2/5 +12345678900 12345678900 12345678900 12345678900 +12345678900.1 12345678900 1/9 12345678900 1/10 12345678900 1/10 +12345678900.2 12345678900 1/5 12345678900 1/5 12345678900 1/5 +12345678900.3 12345678900 2/7 12345678900 3/10 12345678900 3/10 +12345678900.4 12345678900 2/5 12345678900 2/5 12345678900 2/5 +12345678900.5 12345678900 1/2 12345678900 1/2 12345678900 1/2 +12345678900.6 12345678900 3/5 12345678900 3/5 12345678900 3/5 +12345678900.7 12345678900 5/7 12345678900 7/10 12345678900 7/10 +12345678900.8 12345678900 4/5 12345678900 4/5 12345678900 4/5 +12345678900.9 12345678900 8/9 12345678900 9/10 12345678900 9/10 +12345678901.0 12345678901 12345678901 12345678901 +12345678901.1 12345678901 1/9 12345678901 1/10 12345678901 1/10 +12345678901.2 12345678901 1/5 12345678901 1/5 12345678901 1/5 +12345678901.3 12345678901 2/7 12345678901 3/10 12345678901 3/10 +12345678901.4 12345678901 2/5 12345678901 2/5 12345678901 2/5 +12345678901.5 12345678901 1/2 12345678901 1/2 12345678901 1/2 +12345678901.6 12345678901 3/5 12345678901 3/5 12345678901 3/5 +12345678901.7 12345678901 5/7 12345678901 7/10 12345678901 7/10 +12345678901.8 12345678901 4/5 12345678901 4/5 12345678901 4/5 +12345678901.9 12345678901 8/9 12345678901 9/10 12345678901 9/10 +12345678902.0 12345678902 12345678902 12345678902 +12345678902.1 12345678902 1/9 12345678902 1/10 12345678902 1/10 +12345678902.2 12345678902 1/5 12345678902 1/5 12345678902 1/5 +12345678902.3 12345678902 2/7 12345678902 3/10 12345678902 3/10 +12345678902.4 12345678902 2/5 12345678902 2/5 12345678902 2/5 +1234567890 1234567890 1234567890 1234567890 +1234567890.1 1234567890 1234567890 1/10 1234567890 1/10 +1234567890.2 1234567890 1/5 1234567890 1/5 1234567890 1/5 +1234567890.3 1234567890 2/7 1234567890 3/10 1234567890 3/10 +1234567890.4 1234567890 2/5 1234567890 2/5 1234567890 2/5 +1234567890.5 1234567890 1/2 1234567890 1/2 1234567890 1/2 +1234567890.6 1234567890 3/5 1234567890 3/5 1234567890 3/5 +1234567890.7 1234567890 5/7 1234567890 7/10 1234567890 7/10 +1234567890.8 1234567890 4/5 1234567890 4/5 1234567890 4/5 +1234567890.9 1234567891 1234567890 9/10 1234567890 9/10 +1234567891.0 1234567891 1234567891 1234567891 +1234567891.1 1234567891 1234567891 1/10 1234567891 1/10 +1234567891.2 1234567891 1/5 1234567891 1/5 1234567891 1/5 +1234567891.3 1234567891 2/7 1234567891 3/10 1234567891 3/10 +1234567891.4 1234567891 2/5 1234567891 2/5 1234567891 2/5 +1234567891.5 1234567891 1/2 1234567891 1/2 1234567891 1/2 +1234567891.6 1234567891 3/5 1234567891 3/5 1234567891 3/5 +1234567891.7 1234567891 5/7 1234567891 7/10 1234567891 7/10 +1234567891.8 1234567891 4/5 1234567891 4/5 1234567891 4/5 +1234567891.9 1234567892 1234567891 9/10 1234567891 9/10 +1234567892.0 1234567892 1234567892 1234567892 +1234567892.1 1234567892 1234567892 1/10 1234567892 1/10 +1234567892.2 1234567892 1/5 1234567892 1/5 1234567892 1/5 +1234567892.3 1234567892 2/7 1234567892 3/10 1234567892 3/10 +1234567892.4 1234567892 2/5 1234567892 2/5 1234567892 2/5 +