diff --git a/LICENSE b/LICENSE index cc5e930..d3d2837 100644 --- a/LICENSE +++ b/LICENSE @@ -1,12 +1,13 @@ -Copyright (C) 2013-2014 SheetJS +Copyright (C) 2012-2014 SheetJS -The MIT License (MIT) + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + http://www.apache.org/licenses/LICENSE-2.0 -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - -Except where noted, this license applies to any and all software programs and associated documentation files created by the Original Author and distributed with the Software. + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Makefile b/Makefile index 06e5451..fe73517 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +LIB=frac +TARGET=$(LIB).js .PHONY: frac frac: frac.md voc frac.md @@ -19,3 +21,9 @@ coverage.html: frac .PHONY: coveralls coveralls: mocha --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js + +.PHONY: dist +dist: $(TARGET) + cp $(TARGET) dist/ + cp LICENSE dist/ + uglifyjs $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 frac.js)" diff --git a/README.md b/README.md index 1e78d8f..0a633d5 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ Rational approximation to a floating point number with bounded denominator. Uses the Mediant Method This module also provides an implementation of the continued fraction method as -described by Aberth in "A method for exact computation with rational numbers", +described by Aberth in "A method for exact computation with rational numbers", which appears to be used by spreadsheet programs for displaying fractions -## JS Installation and Usage +## Setup In node: @@ -18,6 +18,12 @@ In the browser: +The script will manipulate `module.exports` if available (e.g. in a CommonJS +`require` context). This is not always desirable. To prevent the behavior, +define `DO_NOT_EXPORT_FRAC` + +## Usage + The exported `frac` function takes three arguments: - `x` the number we wish to approximate @@ -38,6 +44,10 @@ For example: `frac.cont` implements the Aberth algorithm (input and output specifications match the original `frac` function) +## License + +Apache 2.0 + ## Tests Tests generated from Excel have 4 columns. To produce a similar test: @@ -49,7 +59,7 @@ Tests generated from Excel have 4 columns. To produce a similar test: [![Build Status](https://travis-ci.org/SheetJS/frac.svg?branch=master)](https://travis-ci.org/SheetJS/frac) -[![Coverage Status](https://coveralls.io/repos/SheetJS/frac/badge.png)](https://coveralls.io/r/SheetJS/frac) +[![Coverage Status](https://coveralls.io/repos/SheetJS/frac/badge.png?branch=master)](https://coveralls.io/r/SheetJS/frac?branch=master) [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/731e31b3a26382ccd5d213b9e74ea552 "githalytics.com")](http://githalytics.com/SheetJS/frac) diff --git a/dist/LICENSE b/dist/LICENSE new file mode 100644 index 0000000..d3d2837 --- /dev/null +++ b/dist/LICENSE @@ -0,0 +1,13 @@ +Copyright (C) 2012-2014 SheetJS + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/dist/frac.js b/dist/frac.js new file mode 100644 index 0000000..87ffdab --- /dev/null +++ b/dist/frac.js @@ -0,0 +1,42 @@ +/* frac.js (C) 2012-2014 SheetJS -- http://sheetjs.com */ +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) < 0.0000000005) break; + B = 1 / (B - A); + P_2 = P_1; P_1 = P; + Q_2 = Q_1; Q_1 = Q; + } + if(Q > D) { Q = Q_1; P = P_1; } + if(Q > D) { Q = Q_2; P = P_2; } + 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.js b/dist/frac.min.js new file mode 100644 index 0000000..40db049 --- /dev/null +++ b/dist/frac.min.js @@ -0,0 +1,3 @@ +/* frac.js (C) 2012-2014 SheetJS -- http://sheetjs.com */ +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(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){Q=Q_1;P=P_1}if(Q>D){Q=Q_2;P=P_2}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; +//# sourceMappingURL=dist/frac.min.map \ No newline at end of file diff --git a/dist/frac.min.map b/dist/frac.min.map new file mode 100644 index 0000000..5e829ea --- /dev/null +++ b/dist/frac.min.map @@ -0,0 +1 @@ +{"version":3,"file":"dist/frac.min.js","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,SAASC,EAAGC,EAAGC,OACxB,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,C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heetjs.com */ +/* frac.js (C) 2012-2014 SheetJS -- http://sheetjs.com */ var frac = function(x, D, mixed) { var n1 = Math.floor(x), d1 = 1; var n2 = n1+1, d2 = 1; @@ -39,4 +39,4 @@ frac.cont = function cont(x, D, mixed) { var q = Math.floor(sgn * P/Q); return [q, sgn*P - q*Q, Q]; }; -if(typeof module !== 'undefined') module.exports = frac; +if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_FRAC === 'undefined') module.exports = frac; diff --git a/frac.md b/frac.md index 4a9bb21..2a44a4c 100644 --- a/frac.md +++ b/frac.md @@ -16,7 +16,7 @@ where `quotient == 0` for improper fractions. The interpretation is and `quotient <= x` for negative `x`. ```js>frac.js -/* frac.js (C) 2013-2014 SheetJS -- http://sheetjs.com */ +/* frac.js (C) 2012-2014 SheetJS -- http://sheetjs.com */ var frac = function(x, D, mixed) { ``` @@ -160,7 +160,7 @@ The final result is `r = (sgn x)p_k / q_k`: Finally we put some export jazz: ``` -if(typeof module !== 'undefined') module.exports = frac; +if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_FRAC === 'undefined') module.exports = frac; ``` # Tests @@ -234,16 +234,20 @@ xltestfiles.forEach(function(x) { ```json>package.json { "name": "frac", - "version": "0.3.1-a", + "version": "1.0.0", "author": "SheetJS", "description": "Rational approximation with bounded denominator", "keywords": [ "math", "fraction", "rational", "approximation" ], - "main": "./frac.js", - "dependencies": {}, - "devDependencies": {"mocha":"","voc":""}, - "repository": { "type":"git", "url": "git://github.com/SheetJS/frac.git" }, + "main": "frac.js", + "dependencies": { + "voc":"" + }, + "devDependencies": { + "mocha":"" + }, + "repository": { "type":"git", "url":"git://github.com/SheetJS/frac.git" }, "scripts": { - "test": "make test" + "test": "mocha -R spec" }, "config": { "blanket": { @@ -251,6 +255,7 @@ xltestfiles.forEach(function(x) { } }, "bugs": { "url": "https://github.com/SheetJS/frac/issues" }, + "license": "Apache-2.0", "engines": { "node": ">=0.8" } } ``` diff --git a/package.json b/package.json index 0bbe065..34ae1ee 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,19 @@ { "name": "frac", - "version": "0.3.1-a", + "version": "1.0.0", "author": "SheetJS", "description": "Rational approximation with bounded denominator", "keywords": [ "math", "fraction", "rational", "approximation" ], - "main": "./frac.js", - "dependencies": {}, - "devDependencies": {"mocha":"","voc":""}, - "repository": { "type":"git", "url": "git://github.com/SheetJS/frac.git" }, + "main": "frac.js", + "dependencies": { + "voc":"" + }, + "devDependencies": { + "mocha":"" + }, + "repository": { "type":"git", "url":"git://github.com/SheetJS/frac.git" }, "scripts": { - "test": "make test" + "test": "mocha -R spec" }, "config": { "blanket": { @@ -17,5 +21,6 @@ } }, "bugs": { "url": "https://github.com/SheetJS/frac/issues" }, + "license": "Apache-2.0", "engines": { "node": ">=0.8" } }