diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d2b47d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +node_modules \ No newline at end of file diff --git a/index.js b/index.js index 3daa8d7..76c205f 100644 --- a/index.js +++ b/index.js @@ -310,6 +310,26 @@ Fraction.prototype.normalize = (function() * round the number to nine decimal places * to avoid js floating point funnies */ + + function numberToFixed (x) { + let e + if (Math.abs(x) < 1.0) { + e = parseInt(x.toString().split("e-")[1]) + if (e) { + x *= Math.pow(10, e - 1) + x = "0." + (new Array(e)).join("0") + x.toString().substring(2); + } + } else { + e = parseInt(x.toString().split("+")[1]) + if (e > 20) { + e -= 20 + x /= Math.pow(10, e) + x += (new Array(e + 1)).join("0"); + } + } + return x; + } + if (isFloat(this.denominator)) { var rounded = roundToPlaces(this.denominator, 9); var scaleup = Math.pow(10, rounded.toString().split('.')[1].length); @@ -318,7 +338,7 @@ Fraction.prototype.normalize = (function() this.numerator *= scaleup; } if (isFloat(this.numerator)) { - var rounded = roundToPlaces(this.numerator, 9); + var rounded = numberToFixed(roundToPlaces(this.numerator, 9)); var scaleup = Math.pow(10, rounded.toString().split('.')[1].length); this.numerator = Math.round(this.numerator * scaleup); // this !!! should be a whole number //this.numerator *= scaleup; diff --git a/package.json b/package.json index de718e2..c7907e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fractional", - "version": "1.0.0", + "version": "1.0.1", "description": "fraction arithmetic", "main": "index.js", "scripts": {