From 6016146ef79a8b7b5b1b7bda3b66d3e6f3dabf21 Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Sat, 10 Mar 2018 19:43:59 +0000 Subject: [PATCH] #88 Update doc and .mjs after toNearest change --- decimal.js | 10 ++------ decimal.mjs | 20 +++++++--------- doc/API.html | 67 +++++++++++++++++++++++----------------------------- 3 files changed, 39 insertions(+), 58 deletions(-) diff --git a/decimal.js b/decimal.js index b0697fb..63c7977 100644 --- a/decimal.js +++ b/decimal.js @@ -2114,14 +2114,8 @@ /* - * Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value - * of this Decimal. - * - * If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`, - * or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple. - * - * In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0 - * (ROUND_UP), and so on. + * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding + * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal. * * The return value will always have the same sign as this Decimal, unless either this Decimal * or `y` is NaN, in which case the return value will be also be NaN. diff --git a/decimal.mjs b/decimal.mjs index 78f9927..98ac347 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -2110,16 +2110,9 @@ P.toHexadecimal = P.toHex = function (sd, rm) { }; - /* - * Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value - * of this Decimal. - * - * If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`, - * or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple. - * - * In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0 - * (ROUND_UP), and so on. + * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding + * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal. * * The return value will always have the same sign as this Decimal, unless either this Decimal * or `y` is NaN, in which case the return value will be also be NaN. @@ -2148,7 +2141,11 @@ P.toNearest = function (y, rm) { rm = Ctor.rounding; } else { y = new Ctor(y); - if (rm !== void 0) checkInt32(rm, 0, 8); + if (rm === void 0) { + rm = Ctor.rounding; + } else { + checkInt32(rm, 0, 8); + } // If x is not finite, return x if y is not NaN, else NaN. if (!x.d) return y.s ? x : y; @@ -2163,7 +2160,6 @@ P.toNearest = function (y, rm) { // If y is not zero, calculate the nearest multiple of y to x. if (y.d[0]) { external = false; - if (rm < 4) rm = [4, 5, 7, 8][rm]; x = divide(x, y, 0, rm, 1).times(y); external = true; finalise(x); @@ -4329,7 +4325,7 @@ function clone(obj) { Decimal.config = Decimal.set = config; Decimal.clone = clone; - Decimal.isDecimal = isDecimalInstance; + Decimal.isDecimal = isDecimalInstance; Decimal.abs = abs; Decimal.acos = acos; diff --git a/doc/API.html b/doc/API.html index ab44236..ae5f05d 100644 --- a/doc/API.html +++ b/doc/API.html @@ -2045,8 +2045,8 @@ x.toBinary(1) // '0b1p+8'

Throws on an invalid dp or rm value.

 x = new Decimal(12.34567)
-x.toDecimalPlaces(0)                // '12'
-x.toDecimalPlaces(1, 0)             // '12.3'
+x.toDecimalPlaces(0)                      // '12'
+x.toDecimalPlaces(1, Decimal.ROUND_UP)    // '12.3'
 
 y = new Decimal(9876.54321)
 y.toDP(3)                           // '9876.543'
@@ -2083,15 +2083,15 @@ y.toDP(1, Decimal.ROUND_DOWN)       // '9876.5'
 x = 45.6
 y = new Decimal(x)
-x.toExponential()              // '4.56e+1'
-y.toExponential()              // '4.56e+1'
-x.toExponential(0)             // '5e+1'
-y.toExponential(0)             // '5e+1'
-x.toExponential(1)             // '4.6e+1'
-y.toExponential(1)             // '4.6e+1'
-y.toExponential(1, 1)          // '4.5e+1'  (ROUND_DOWN)
-x.toExponential(3)             // '4.560e+1'
-y.toExponential(3)             // '4.560e+1'
+x.toExponential() // '4.56e+1' +y.toExponential() // '4.56e+1' +x.toExponential(0) // '5e+1' +y.toExponential(0) // '5e+1' +x.toExponential(1) // '4.6e+1' +y.toExponential(1) // '4.6e+1' +y.toExponential(1, Decimal.ROUND_DOWN) // '4.5e+1' +x.toExponential(3) // '4.560e+1' +y.toExponential(3) // '4.560e+1' @@ -2131,14 +2131,14 @@ y.toExponential(3) // '4.560e+1'
 x = 3.456
 y = new Decimal(x)
-x.toFixed()              // '3'
-y.toFixed()              // '3.456'
-y.toFixed(0)             // '3'
-x.toFixed(2)             // '3.46'
-y.toFixed(2)             // '3.46'
-y.toFixed(2, 1)          // '3.45'  (ROUND_DOWN)
-x.toFixed(5)             // '3.45600'
-y.toFixed(5)             // '3.45600'
+x.toFixed() // '3' +y.toFixed() // '3.456' +y.toFixed(0) // '3' +x.toFixed(2) // '3.46' +y.toFixed(2) // '3.46' +y.toFixed(2, Decimal.ROUND_DOWN) // '3.45' +x.toFixed(5) // '3.45600' +y.toFixed(5) // '3.45600' @@ -2210,25 +2210,16 @@ x.toHex(1) // '0x1p+8'
- toNearest.toNearest(n [, rm]) ⇒ Decimal + toNearest.toNearest(x [, rm]) ⇒ Decimal

x: number|string|Decimal
rm: number: integer, 0 to 8 inclusive

- Returns a new Decimal whose value is the nearest multiple of x to the value of - this Decimal. -

-

- If the value of this Decimal is equidistant from two multiples of x, the rounding - mode rm, or rounding if rm is - omitted, determines the direction of the nearest. -

-

- In this context, rounding mode ROUND_HALF_UP is - interpreted the same as rounding mode ROUND_UP, and so - on, i.e. the rounding is either up, down, to ceil, to floor or to even. + Returns a new Decimal whose value is the nearest multiple of x in the direction + of rounding mode rm, or rounding if + rm is omitted, to the value of this Decimal.

The return value will always have the same sign as this Decimal, unless either this Decimal @@ -2241,11 +2232,11 @@ x.toHex(1) // '0x1p+8'

 x = new Decimal(1.39)
-x.toNearest(0.25)              // '1.5'
+x.toNearest(0.25)                        // '1.5'
 
-y = new Decimal(0.75)          // equidistant from 0.5 and 1
-y.toNearest(0.5, 0)            // '1'    (ROUND_UP)
-y.toNearest(0.5, 1)            // '0.5'  (ROUND_DOWN)
+y = new Decimal(9.499) +y.toNearest(0.5, Decimal.ROUND_UP) // '9.5' +y.toNearest(0.5, Decimal.ROUND_DOWN) // '9' @@ -2393,8 +2384,8 @@ x.toPrecision() // '45.6' y.toPrecision() // '45.6' x.toPrecision(1) // '5e+1' y.toPrecision(1) // '5e+1' -y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP) -y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN) +y.toPrecision(2, Decimal.ROUND_UP) // '4.6e+1' +y.toPrecision(2, Decimal.DOWN) // '4.5e+1' x.toPrecision(5) // '45.600' y.toPrecision(5) // '45.600'