diff --git a/Makefile b/Makefile
index 82d2628..9816c32 100644
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@ $(TARGET) $(AUXTARGETS): %.js : %.flow.js
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^[ \t]*\/\*[:#][^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*[:#][^*]*\*\//gm,""))' > $@
$(FLOWTARGET): $(DEPS)
- voc $^
+ voc $^ || npx voc $^
.PHONY: clean
clean: ## Remove targets and build artifacts
@@ -40,7 +40,7 @@ ctest: ## Build browser test (into ctest/ subdirectory)
.PHONY: ctestserv
ctestserv: ## Start a test server on port 8000
- @cd ctest && python -mSimpleHTTPServer
+ @cd ctest && python -mSimpleHTTPServer || python3 -mhttp.server || npx -y http-server -p 8000 .
.PHONY: fullint
fullint: lint old-lint tslint flow mdlint ## Run all checks
diff --git a/README.md b/README.md
index b1fae7f..9083981 100644
--- a/README.md
+++ b/README.md
@@ -13,16 +13,16 @@ replicate fraction formats.
### JS
-With [`npm`](https://www.npmjs.org/package/frac):
+For NodeJS:
```bash
-$ npm install frac
+$ npm i --save https://cdn.sheetjs.com/frac-1.1.3/frac-1.1.3.tgz
```
In the browser:
```html
-
+
```
The script will manipulate `module.exports` if available . This is not always
@@ -104,7 +104,7 @@ The test TSV baselines in the `test_files` directory have four columns:
`make ctest` will use `browserify` to build a standalone script that can be run
in the web browser. The transform `brfs` must be installed locally. Browser
-test script built against `browserfy@16.5.1` and `brfs@2.0.2`.
+test script built against `browserify@16.5.1` and `brfs@2.0.2`.
`make pytest` will run the python tests against the system Python version.
@@ -114,17 +114,3 @@ test script built against `browserfy@16.5.1` and `brfs@2.0.2`.
Please consult the attached LICENSE file for details. All rights not explicitly
granted by the Apache 2.0 License are reserved by the Original Author.
-
-## Badges
-
-[](https://saucelabs.com/u/frac)
-
-[](https://travis-ci.org/SheetJS/frac)
-
-[](https://coveralls.io/r/SheetJS/frac?branch=master)
-
-[](https://npmjs.org/package/frac)
-
-[](https://david-dm.org/sheetjs/frac)
-
-[](https://github.com/SheetJS/frac)
diff --git a/ctest/frac.js b/ctest/frac.js
index efd1a0d..6f14206 100644
--- a/ctest/frac.js
+++ b/ctest/frac.js
@@ -2,18 +2,16 @@
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) {
+ if(x !== n1) while(d1 + 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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
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(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
if(!mixed) return [0, n1, d1];
var q = Math.floor(n1/d1);
return [q, n1 - q*d1, d1];
diff --git a/ctest/test.js b/ctest/test.js
index d938f37..30f8f26 100644
--- a/ctest/test.js
+++ b/ctest/test.js
@@ -3,18 +3,16 @@
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) {
+ if(x !== n1) while(d1 + 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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
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(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
if(!mixed) return [0, n1, d1];
var q = Math.floor(n1/d1);
return [q, n1 - q*d1, d1];
@@ -43,6 +41,7 @@ frac.cont = function cont(x, D, mixed) {
if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_FRAC === 'undefined') module.exports = frac;
},{}],2:[function(require,module,exports){
+/* eslint-env mocha, node */
var fs = require('fs'), assert = require('assert');
var frac;
describe('source', function() { it('should load', function() { frac = require('./'); }); });
@@ -57,7 +56,7 @@ var xltestfiles=[
function xlline(o,j,m,w) {
it(j.toString(), function() {
var d, q, qq, f = 0.1;
- var q0 = 0, q1 = 0, q2 = 0
+ 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;
@@ -99,6 +98,8 @@ describe('mediant', function() {
cmp(frac(1.0,9,true), [1,0,1]);
cmp(frac(1.7,9,true), [1,5,7]);
cmp(frac(1.7,9,false),[0,12,7]);
+ cmp(frac(0.69,9,false),[0,2,3]);
+ cmp(frac(0.6969,9,false),[0,5,7]);
});
});
xltestfiles.forEach(function(x) {
@@ -111,7 +112,7 @@ xltestfiles.forEach(function(x) {
},{"./":1,"assert":4,"fs":3}],3:[function(require,module,exports){
},{}],4:[function(require,module,exports){
-(function (global){
+(function (global){(function (){
'use strict';
var objectAssign = require('object-assign');
@@ -619,7 +620,7 @@ var objectKeys = Object.keys || function (obj) {
return keys;
};
-}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
+}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"object-assign":8,"util/":7}],5:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
@@ -653,7 +654,7 @@ module.exports = function isBuffer(arg) {
&& typeof arg.readUInt8 === 'function';
}
},{}],7:[function(require,module,exports){
-(function (process,global){
+(function (process,global){(function (){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
@@ -1241,7 +1242,7 @@ function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
-}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
+}).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./support/isBuffer":6,"_process":9,"inherits":5}],8:[function(require,module,exports){
/*
object-assign
diff --git a/dist/LICENSE b/dist/LICENSE
index d99d93d..4bdda80 100644
--- a/dist/LICENSE
+++ b/dist/LICENSE
@@ -1,4 +1,192 @@
-Copyright (C) 2012-present SheetJS
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright (C) 2012-present SheetJS LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
diff --git a/dist/frac.js b/dist/frac.js
index efd1a0d..6f14206 100644
--- a/dist/frac.js
+++ b/dist/frac.js
@@ -2,18 +2,16 @@
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) {
+ if(x !== n1) while(d1 + 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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
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(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
if(!mixed) return [0, n1, d1];
var q = Math.floor(n1/d1);
return [q, n1 - q*d1, d1];
diff --git a/dist/frac.min.js b/dist/frac.min.js
index 9a5cb02..e689a05 100644
--- a/dist/frac.min.js
+++ b/dist/frac.min.js
@@ -1,2 +1,2 @@
/* 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(xD){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_1D){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;
+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+d2<=D){var m=(n1+n2)/(d1+d2);if(x===m){if(d1+d2<=D){d2=d1+=d2;n2=n1+=n2}break}else if(xD){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;
diff --git a/dist/frac.min.map b/dist/frac.min.map
index b86417b..7b9b2be 100644
--- a/dist/frac.min.map
+++ b/dist/frac.min.map
@@ -1 +1 @@
-{"version":3,"sources":["frac.js"],"names":["frac","x","D","mixed","n1","Math","floor","d1","n2","d2","m","q","cont","sgn","B","P_2","P_1","P","Q_2","Q_1","Q","A","module","DO_NOT_EXPORT_FRAC","exports"],"mappings":";AACA,GAAIA,MAAO,QAASA,MAAKC,EAAGC,EAAGC,OAC7B,GAAIC,IAAKC,KAAKC,MAAML,GAAIM,GAAK,CAC7B,IAAIC,IAAKJ,GAAG,EAAGK,GAAK,CACpB,IAAGR,IAAMG,GAAI,MAAMG,IAAML,GAAKO,IAAMP,EAAG,CACrC,GAAIQ,IAAKN,GAAKI,KAAOD,GAAKE,GAC1B,IAAGR,IAAMS,EAAG,CACV,GAAGH,GAAKE,IAAMP,EAAG,CAAEK,IAAIE,EAAIL,KAAII,EAAIC,IAAGP,EAAE,MACnC,IAAGK,GAAKE,GAAIA,GAAGP,EAAE,MACjBK,IAAGL,EAAE,CACV,WAEG,IAAGD,EAAIS,EAAG,CAAEF,GAAKJ,GAAGI,EAAIC,IAAKF,GAAGE,OAChC,CAAEL,GAAKA,GAAGI,EAAID,IAAKA,GAAGE,IAE7B,GAAGF,GAAKL,EAAG,CAAEK,GAAKE,EAAIL,IAAKI,GAC3B,IAAIL,MAAO,OAAQ,EAAGC,GAAIG,GAC1B,IAAII,GAAIN,KAAKC,MAAMF,GAAGG,GACtB,QAAQI,EAAGP,GAAKO,EAAEJ,GAAIA,IAExBP,MAAKY,KAAO,QAASA,MAAKX,EAAGC,EAAGC,OAC9B,GAAIU,KAAMZ,EAAI,GAAK,EAAI,CACvB,IAAIa,GAAIb,EAAIY,GACZ,IAAIE,KAAM,EAAGC,IAAM,EAAGC,EAAI,CAC1B,IAAIC,KAAM,EAAGC,IAAM,EAAGC,EAAI,CAC1B,IAAIC,GAAIhB,KAAKC,MAAMQ,EACnB,OAAMK,IAAMjB,EAAG,CACbmB,EAAIhB,KAAKC,MAAMQ,EACfG,GAAII,EAAIL,IAAMD,GACdK,GAAIC,EAAIF,IAAMD,GACd,IAAIJ,EAAIO,EAAK,KAAY,KACzBP,GAAI,GAAKA,EAAIO,EACbN,KAAMC,GAAKA,KAAMC,CACjBC,KAAMC,GAAKA,KAAMC,EAEnB,GAAGA,EAAIlB,EAAG,CAAE,GAAGiB,IAAMjB,EAAG,CAAEkB,EAAIF,GAAKD,GAAIF,QAAY,CAAEK,EAAID,GAAKF,GAAID,KAClE,IAAIb,MAAO,OAAQ,EAAGU,IAAMI,EAAGG,EAC/B,IAAIT,GAAIN,KAAKC,MAAMO,IAAMI,EAAEG,EAC3B,QAAQT,EAAGE,IAAII,EAAIN,EAAES,EAAGA,GAG1B,UAAUE,UAAW,mBAAsBC,sBAAuB,YAAaD,OAAOE,QAAUxB","file":"dist/frac.min.js"}
\ No newline at end of file
+{"version":3,"sources":["frac.js"],"names":["frac","x","D","mixed","n1","Math","floor","d1","n2","d2","m","q","cont","sgn","B","P_2","P_1","P","Q_2","Q_1","Q","A","module","DO_NOT_EXPORT_FRAC","exports"],"mappings":";AACA,GAAIA,MAAO,QAASA,MAAKC,EAAGC,EAAGC,OAC7B,GAAIC,IAAKC,KAAKC,MAAML,GAAIM,GAAK,CAC7B,IAAIC,IAAKJ,GAAG,EAAGK,GAAK,CACpB,IAAGR,IAAMG,GAAI,MAAMG,GAAKE,IAAMP,EAAG,CAC/B,GAAIQ,IAAKN,GAAKI,KAAOD,GAAKE,GAC1B,IAAGR,IAAMS,EAAG,CACV,GAAGH,GAAKE,IAAMP,EAAG,CAAEO,GAAMF,IAAIE,EAAKD,IAAMJ,IAAII,GAC5C,UAEG,IAAGP,EAAIS,EAAG,CAAEF,GAAKJ,GAAGI,EAAIC,IAAKF,GAAGE,OAChC,CAAEL,GAAKA,GAAGI,EAAID,IAAKA,GAAGE,IAE7B,GAAGD,GAAKC,GAAKR,GAAKA,EAAIG,GAAKG,GAAK,CAAEA,GAAKE,EAAIL,IAAKI,GAChD,IAAIL,MAAO,OAAQ,EAAGC,GAAIG,GAC1B,IAAII,GAAIN,KAAKC,MAAMF,GAAGG,GACtB,QAAQI,EAAGP,GAAKO,EAAEJ,GAAIA,IAExBP,MAAKY,KAAO,QAASA,MAAKX,EAAGC,EAAGC,OAC9B,GAAIU,KAAMZ,EAAI,GAAK,EAAI,CACvB,IAAIa,GAAIb,EAAIY,GACZ,IAAIE,KAAM,EAAGC,IAAM,EAAGC,EAAI,CAC1B,IAAIC,KAAM,EAAGC,IAAM,EAAGC,EAAI,CAC1B,IAAIC,GAAIhB,KAAKC,MAAMQ,EACnB,OAAMK,IAAMjB,EAAG,CACbmB,EAAIhB,KAAKC,MAAMQ,EACfG,GAAII,EAAIL,IAAMD,GACdK,GAAIC,EAAIF,IAAMD,GACd,IAAIJ,EAAIO,EAAK,KAAY,KACzBP,GAAI,GAAKA,EAAIO,EACbN,KAAMC,GAAKA,KAAMC,CACjBC,KAAMC,GAAKA,KAAMC,EAEnB,GAAGA,EAAIlB,EAAG,CAAE,GAAGiB,IAAMjB,EAAG,CAAEkB,EAAIF,GAAKD,GAAIF,QAAY,CAAEK,EAAID,GAAKF,GAAID,KAClE,IAAIb,MAAO,OAAQ,EAAGU,IAAMI,EAAGG,EAC/B,IAAIT,GAAIN,KAAKC,MAAMO,IAAMI,EAAEG,EAC3B,QAAQT,EAAGE,IAAII,EAAIN,EAAES,EAAGA,GAG1B,UAAUE,UAAW,mBAAsBC,sBAAuB,YAAaD,OAAOE,QAAUxB","file":"dist/frac.min.js"}
\ No newline at end of file
diff --git a/frac.flow.js b/frac.flow.js
index c46be40..c7b3562 100644
--- a/frac.flow.js
+++ b/frac.flow.js
@@ -2,18 +2,16 @@
var frac = function frac(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array*/ {
var n1 = Math.floor(x), d1 = 1;
var n2 = n1+1, d2 = 1;
- if(x !== n1) while(d1 <= D && d2 <= D) {
+ if(x !== n1) while(d1 + 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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
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(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
if(!mixed) return [0, n1, d1];
var q = Math.floor(n1/d1);
return [q, n1 - q*d1, d1];
diff --git a/frac.js b/frac.js
index efd1a0d..6f14206 100644
--- a/frac.js
+++ b/frac.js
@@ -2,18 +2,16 @@
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) {
+ if(x !== n1) while(d1 + 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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
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(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
if(!mixed) return [0, n1, d1];
var q = Math.floor(n1/d1);
return [q, n1 - q*d1, d1];
diff --git a/frac.md b/frac.md
index 95ac853..ad1c785 100644
--- a/frac.md
+++ b/frac.md
@@ -36,11 +36,11 @@ the numerators and denominators separately:
var n2 = n1+1, d2 = 1;
```
-If `x` is not integral, we bisect using mediants until a denominator exceeds
-our target:
+If `x` is not integral, we bisect using mediants until a the next denominator
+exceeds our target:
```
- if(x !== n1) while(d1 <= D && d2 <= D) {
+ if(x !== n1) while(d1 + d2 <= D) {
```
The mediant is the sum of the numerators divided by the sum of denominators:
@@ -55,9 +55,7 @@ denominator)
```
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;
+ if(d1 + d2 <= D) { d2 = (d1+=d2); n2 = (n1+=n2); }
break;
}
```
@@ -70,11 +68,17 @@ Otherwise shrink the range:
}
```
-At this point, `d1 > D` or `d2 > D` (but not both -- keep track of how `d1` and
-`d2` change). So we merely return the desired values:
+At this point, the optimal fraction is either `n1 / d1` or `n2 / d2`. The deltas
+should be compared to determine the closer fraction. For this function, we will
+store the results in `n1` and `d1`:
+
+```
+ if(n2 / d2 - x <= x - n1 / d1 ) { d1 = d2; n1 = n2; }
+```
+
+The rest of the function determines which fraction to return:
```
- 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];
@@ -173,6 +177,7 @@ if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_FRAC === 'undefined') m
# Tests
```js>test.js
+/* eslint-env mocha, node */
var fs = require('fs'), assert = require('assert');
var frac;
describe('source', function() { it('should load', function() { frac = require('./'); }); });
@@ -187,7 +192,7 @@ var xltestfiles=[
function xlline(o,j,m,w) {
it(j.toString(), function() {
var d, q, qq, f = 0.1;
- var q0 = 0, q1 = 0, q2 = 0
+ 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;
@@ -229,6 +234,8 @@ describe('mediant', function() {
cmp(frac(1.0,9,true), [1,0,1]);
cmp(frac(1.7,9,true), [1,5,7]);
cmp(frac(1.7,9,false),[0,12,7]);
+ cmp(frac(0.69,9,false),[0,2,3]);
+ cmp(frac(0.6969,9,false),[0,5,7]);
});
});
xltestfiles.forEach(function(x) {
@@ -244,7 +251,7 @@ xltestfiles.forEach(function(x) {
```json>package.json
{
"name": "frac",
- "version": "1.1.2",
+ "version": "1.1.3",
"author": "SheetJS",
"description": "Rational approximation with bounded denominator",
"keywords": [ "math", "fraction", "rational", "approximation" ],
@@ -262,7 +269,7 @@ xltestfiles.forEach(function(x) {
"dtslint": "^0.1.2",
"typescript": "2.2.0"
},
- "repository": { "type": "git", "url": "git://github.com/SheetJS/frac.git" },
+ "repository": { "type": "git", "url": "https://git.sheetjs.com/SheetJS/frac.git" },
"scripts": {
"test": "make test",
"build": "make",
@@ -275,7 +282,7 @@ xltestfiles.forEach(function(x) {
}
},
"homepage": "http://sheetjs.com/opensource",
- "bugs": { "url": "https://github.com/SheetJS/frac/issues" },
+ "bugs": { "url": "https://git.sheetjs.com/SheetJS/frac/issues" },
"license": "Apache-2.0",
"engines": { "node": ">=0.8" }
}
diff --git a/package.json b/package.json
index c604e86..465a5ae 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "frac",
- "version": "1.1.2",
+ "version": "1.1.3",
"author": "SheetJS",
"description": "Rational approximation with bounded denominator",
"keywords": [ "math", "fraction", "rational", "approximation" ],
@@ -18,7 +18,7 @@
"dtslint": "^0.1.2",
"typescript": "2.2.0"
},
- "repository": { "type": "git", "url": "git://github.com/SheetJS/frac.git" },
+ "repository": { "type": "git", "url": "https://git.sheetjs.com/SheetJS/frac.git" },
"scripts": {
"test": "make test",
"build": "make",
@@ -31,7 +31,7 @@
}
},
"homepage": "http://sheetjs.com/opensource",
- "bugs": { "url": "https://github.com/SheetJS/frac/issues" },
+ "bugs": { "url": "https://git.sheetjs.com/SheetJS/frac/issues" },
"license": "Apache-2.0",
"engines": { "node": ">=0.8" }
}
diff --git a/test.js b/test.js
index 30276f3..d500c1d 100644
--- a/test.js
+++ b/test.js
@@ -1,3 +1,4 @@
+/* eslint-env mocha, node */
var fs = require('fs'), assert = require('assert');
var frac;
describe('source', function() { it('should load', function() { frac = require('./'); }); });
@@ -12,7 +13,7 @@ var xltestfiles=[
function xlline(o,j,m,w) {
it(j.toString(), function() {
var d, q, qq, f = 0.1;
- var q0 = 0, q1 = 0, q2 = 0
+ 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;
@@ -54,6 +55,8 @@ describe('mediant', function() {
cmp(frac(1.0,9,true), [1,0,1]);
cmp(frac(1.7,9,true), [1,5,7]);
cmp(frac(1.7,9,false),[0,12,7]);
+ cmp(frac(0.69,9,false),[0,2,3]);
+ cmp(frac(0.6969,9,false),[0,5,7]);
});
});
xltestfiles.forEach(function(x) {