diff --git a/decimal.js b/decimal.js index c3ac7c5..c7923fc 100644 --- a/decimal.js +++ b/decimal.js @@ -218,8 +218,8 @@ * Return a new Decimal whose value is the value of this Decimal clamped to the range * delineated by `min` and `max`. * - * min {number|string|bigInt|Decimal} - * max {number|string|bigInt|Decimal} + * min {number|string|bigint|Decimal} + * max {number|string|bigint|Decimal} * */ P.clampedTo = P.clamp = function (min, max) { @@ -723,8 +723,7 @@ * */ P.inverseCosine = P.acos = function () { - var halfPi, - x = this, + var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, @@ -744,8 +743,9 @@ Ctor.precision = pr + 6; Ctor.rounding = 1; - - x = (new Ctor(1)).minus(x).div(x.plus(1)).sqrt().atan(); + + // See https://github.com/MikeMcl/decimal.js/pull/217 + x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan(); Ctor.precision = pr; Ctor.rounding = rm; @@ -1124,7 +1124,7 @@ * log[b](Infinity) = Infinity * log[b](NaN) = NaN * - * [base] {number|string|bigInt|Decimal} The base of the logarithm. + * [base] {number|string|bigint|Decimal} The base of the logarithm. * */ P.logarithm = P.log = function (base) { @@ -1218,7 +1218,7 @@ /* * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal. * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * P.max = function () { Array.prototype.push.call(arguments, this); @@ -1230,7 +1230,7 @@ /* * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal. * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * P.min = function () { Array.prototype.push.call(arguments, this); @@ -2053,7 +2053,7 @@ * denominator. If a maximum denominator is not specified, the denominator will be the lowest * value necessary to represent the number exactly. * - * [maxD] {number|string|bigInt|Decimal} Maximum denominator. Integer >= 1 and < Infinity. + * [maxD] {number|string|bigint|Decimal} Maximum denominator. Integer >= 1 and < Infinity. * */ P.toFraction = function (maxD) { @@ -2141,7 +2141,7 @@ * * The return value is not affected by the value of `precision`. * - * y {number|string|bigInt|Decimal} The magnitude to round to a multiple of. + * y {number|string|bigint|Decimal} The magnitude to round to a multiple of. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * * 'toNearest() rounding mode not an integer: {rm}' @@ -2261,7 +2261,7 @@ * * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place). * - * y {number|string|bigInt|Decimal} The power to which to raise this Decimal. + * y {number|string|bigint|Decimal} The power to which to raise this Decimal. * */ P.toPower = P.pow = function (y) { @@ -3991,7 +3991,7 @@ /* * Return a new Decimal whose value is the absolute value of `x`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function abs(x) { @@ -4002,7 +4002,7 @@ /* * Return a new Decimal whose value is the arccosine in radians of `x`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function acos(x) { @@ -4014,7 +4014,7 @@ * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function acosh(x) { @@ -4026,8 +4026,8 @@ * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} - * y {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function add(x, y) { @@ -4039,7 +4039,7 @@ * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function asin(x) { @@ -4051,7 +4051,7 @@ * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function asinh(x) { @@ -4063,7 +4063,7 @@ * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function atan(x) { @@ -4075,7 +4075,7 @@ * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function atanh(x) { @@ -4090,8 +4090,8 @@ * Domain: [-Infinity, Infinity] * Range: [-pi, pi] * - * y {number|string|bigInt|Decimal} The y-coordinate. - * x {number|string|bigInt|Decimal} The x-coordinate. + * y {number|string|bigint|Decimal} The y-coordinate. + * x {number|string|bigint|Decimal} The x-coordinate. * * atan2(±0, -0) = ±pi * atan2(±0, +0) = ±0 @@ -4156,7 +4156,7 @@ * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function cbrt(x) { @@ -4167,7 +4167,7 @@ /* * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function ceil(x) { @@ -4178,9 +4178,9 @@ /* * Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`. * - * x {number|string|bigInt|Decimal} - * min {number|string|bigInt|Decimal} - * max {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * min {number|string|bigint|Decimal} + * max {number|string|bigint|Decimal} * */ function clamp(x, min, max) { @@ -4254,7 +4254,7 @@ * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function cos(x) { @@ -4266,7 +4266,7 @@ * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function cosh(x) { @@ -4286,7 +4286,7 @@ * The Decimal constructor and exported function. * Return a new Decimal instance. * - * v {number|string|bigInt|Decimal} A numeric value. + * v {number|string|bigint|Decimal} A numeric value. * */ function Decimal(v) { @@ -4477,8 +4477,8 @@ * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} - * y {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function div(x, y) { @@ -4490,7 +4490,7 @@ * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} The power to which to raise the base of the natural log. + * x {number|string|bigint|Decimal} The power to which to raise the base of the natural log. * */ function exp(x) { @@ -4501,7 +4501,7 @@ /* * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function floor(x) { @@ -4515,7 +4515,7 @@ * * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * */ function hypot() { @@ -4557,7 +4557,7 @@ * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function ln(x) { @@ -4571,8 +4571,8 @@ * * log[y](x) * - * x {number|string|bigInt|Decimal} The argument of the logarithm. - * y {number|string|bigInt|Decimal} The base of the logarithm. + * x {number|string|bigint|Decimal} The argument of the logarithm. + * y {number|string|bigint|Decimal} The base of the logarithm. * */ function log(x, y) { @@ -4584,7 +4584,7 @@ * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function log2(x) { @@ -4596,7 +4596,7 @@ * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function log10(x) { @@ -4607,7 +4607,7 @@ /* * Return a new Decimal whose value is the maximum of the arguments. * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * */ function max() { @@ -4618,7 +4618,7 @@ /* * Return a new Decimal whose value is the minimum of the arguments. * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * */ function min() { @@ -4630,8 +4630,8 @@ * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} - * y {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function mod(x, y) { @@ -4643,8 +4643,8 @@ * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} - * y {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function mul(x, y) { @@ -4656,8 +4656,8 @@ * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} The base. - * y {number|string|bigInt|Decimal} The exponent. + * x {number|string|bigint|Decimal} The base. + * y {number|string|bigint|Decimal} The exponent. * */ function pow(x, y) { @@ -4775,7 +4775,7 @@ * * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL). * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function round(x) { @@ -4791,7 +4791,7 @@ * -0 if x is -0, * NaN otherwise * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function sign(x) { @@ -4804,7 +4804,7 @@ * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function sin(x) { @@ -4816,7 +4816,7 @@ * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function sinh(x) { @@ -4828,7 +4828,7 @@ * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function sqrt(x) { @@ -4840,8 +4840,8 @@ * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} - * y {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function sub(x, y) { @@ -4855,7 +4855,7 @@ * * Only the result is rounded, not the intermediate calculations. * - * arguments {number|string|bigInt|Decimal} + * arguments {number|string|bigint|Decimal} * */ function sum() { @@ -4875,7 +4875,7 @@ * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function tan(x) { @@ -4887,7 +4887,7 @@ * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|bigInt|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function tanh(x) { @@ -4898,7 +4898,7 @@ /* * Return a new Decimal whose value is `x` truncated to an integer. * - * x {number|string|bigInt|Decimal} + * x {number|string|bigint|Decimal} * */ function trunc(x) { diff --git a/decimal.mjs b/decimal.mjs index 293ee8e..5cd1fa9 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -214,8 +214,8 @@ P.ceil = function () { * Return a new Decimal whose value is the value of this Decimal clamped to the range * delineated by `min` and `max`. * - * min {number|string|Decimal} - * max {number|string|Decimal} + * min {number|string|bigint|Decimal} + * max {number|string|bigint|Decimal} * */ P.clampedTo = P.clamp = function (min, max) { @@ -719,8 +719,7 @@ P.hyperbolicTangent = P.tanh = function () { * */ P.inverseCosine = P.acos = function () { - var halfPi, - x = this, + var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, @@ -741,7 +740,8 @@ P.inverseCosine = P.acos = function () { Ctor.precision = pr + 6; Ctor.rounding = 1; - x = (new Ctor(1)).minus(x).div(x.plus(1)).sqrt().atan(); + // See https://github.com/MikeMcl/decimal.js/pull/217 + x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan(); Ctor.precision = pr; Ctor.rounding = rm; @@ -1120,7 +1120,7 @@ P.lessThanOrEqualTo = P.lte = function (y) { * log[b](Infinity) = Infinity * log[b](NaN) = NaN * - * [base] {number|string|Decimal} The base of the logarithm. + * [base] {number|string|bigint|Decimal} The base of the logarithm. * */ P.logarithm = P.log = function (base) { @@ -1214,11 +1214,11 @@ P.logarithm = P.log = function (base) { /* * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal. * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * P.max = function () { Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'lt'); + return maxOrMin(this.constructor, arguments, -1); }; */ @@ -1226,11 +1226,11 @@ P.max = function () { /* * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal. * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * P.min = function () { Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'gt'); + return maxOrMin(this.constructor, arguments, 1); }; */ @@ -2049,7 +2049,7 @@ P.toFixed = function (dp, rm) { * denominator. If a maximum denominator is not specified, the denominator will be the lowest * value necessary to represent the number exactly. * - * [maxD] {number|string|Decimal} Maximum denominator. Integer >= 1 and < Infinity. + * [maxD] {number|string|bigint|Decimal} Maximum denominator. Integer >= 1 and < Infinity. * */ P.toFraction = function (maxD) { @@ -2137,7 +2137,7 @@ P.toHexadecimal = P.toHex = function (sd, rm) { * * The return value is not affected by the value of `precision`. * - * y {number|string|Decimal} The magnitude to round to a multiple of. + * y {number|string|bigint|Decimal} The magnitude to round to a multiple of. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * * 'toNearest() rounding mode not an integer: {rm}' @@ -2257,7 +2257,7 @@ P.toOctal = function (sd, rm) { * * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place). * - * y {number|string|Decimal} The power to which to raise this Decimal. + * y {number|string|bigint|Decimal} The power to which to raise this Decimal. * */ P.toPower = P.pow = function (y) { @@ -3241,19 +3241,25 @@ function isOdd(n) { /* - * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'. + * Handle `max` (`n` is -1) and `min` (`n` is 1). */ -function maxOrMin(Ctor, args, ltgt) { - var y, +function maxOrMin(Ctor, args, n) { + var k, y, x = new Ctor(args[0]), i = 0; for (; ++i < args.length;) { y = new Ctor(args[i]); + + // NaN? if (!y.s) { x = y; break; - } else if (x[ltgt](y)) { + } + + k = x.cmp(y); + + if (k === n || k === 0 && x.s === n) { x = y; } } @@ -3512,6 +3518,7 @@ function nonFiniteToString(x) { function parseDecimal(x, str) { var e, i, len; + // TODO BigInt str: no need to check for decimal point, exponential form or leading zeros. // Decimal point? if ((e = str.indexOf('.')) > -1) str = str.replace('.', ''); @@ -3979,7 +3986,7 @@ function truncate(arr, len) { /* * Return a new Decimal whose value is the absolute value of `x`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function abs(x) { @@ -3990,7 +3997,7 @@ function abs(x) { /* * Return a new Decimal whose value is the arccosine in radians of `x`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function acos(x) { @@ -4002,7 +4009,7 @@ function acos(x) { * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function acosh(x) { @@ -4014,8 +4021,8 @@ function acosh(x) { * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} - * y {number|string|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function add(x, y) { @@ -4027,7 +4034,7 @@ function add(x, y) { * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function asin(x) { @@ -4039,7 +4046,7 @@ function asin(x) { * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function asinh(x) { @@ -4051,7 +4058,7 @@ function asinh(x) { * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function atan(x) { @@ -4063,7 +4070,7 @@ function atan(x) { * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function atanh(x) { @@ -4078,8 +4085,8 @@ function atanh(x) { * Domain: [-Infinity, Infinity] * Range: [-pi, pi] * - * y {number|string|Decimal} The y-coordinate. - * x {number|string|Decimal} The x-coordinate. + * y {number|string|bigint|Decimal} The y-coordinate. + * x {number|string|bigint|Decimal} The x-coordinate. * * atan2(±0, -0) = ±pi * atan2(±0, +0) = ±0 @@ -4144,7 +4151,7 @@ function atan2(y, x) { * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function cbrt(x) { @@ -4155,7 +4162,7 @@ function cbrt(x) { /* * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function ceil(x) { @@ -4166,9 +4173,9 @@ function ceil(x) { /* * Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`. * - * x {number|string|Decimal} - * min {number|string|Decimal} - * max {number|string|Decimal} + * x {number|string|bigint|Decimal} + * min {number|string|bigint|Decimal} + * max {number|string|bigint|Decimal} * */ function clamp(x, min, max) { @@ -4242,7 +4249,7 @@ function config(obj) { * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function cos(x) { @@ -4254,7 +4261,7 @@ function cos(x) { * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function cosh(x) { @@ -4274,7 +4281,7 @@ function clone(obj) { * The Decimal constructor and exported function. * Return a new Decimal instance. * - * v {number|string|Decimal} A numeric value. + * v {number|string|bigint|Decimal} A numeric value. * */ function Decimal(v) { @@ -4288,7 +4295,6 @@ function clone(obj) { // which points to Object. x.constructor = Decimal; - // Duplicate. if (isDecimalInstance(v)) { x.s = v.s; @@ -4353,9 +4359,10 @@ function clone(obj) { } return; + } - // Infinity, NaN. - } else if (v * 0 !== 0) { + // Infinity or NaN? + if (v * 0 !== 0) { if (!v) x.s = NaN; x.e = NaN; x.d = null; @@ -4363,22 +4370,32 @@ function clone(obj) { } return parseDecimal(x, v.toString()); - - } else if (t !== 'string') { - throw Error(invalidArgument + v); } - // Minus sign? - if ((i = v.charCodeAt(0)) === 45) { - v = v.slice(1); - x.s = -1; - } else { - // Plus sign? - if (i === 43) v = v.slice(1); - x.s = 1; + if (t === 'string') { + if ((i = v.charCodeAt(0)) === 45) { // minus sign + v = v.slice(1); + x.s = -1; + } else { + if (i === 43) v = v.slice(1); // plus sign + x.s = 1; + } + + return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v); } - return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v); + if (t === 'bigint') { + if (v < 0) { + v = -v; + x.s = -1; + } else { + x.s = 1; + } + + return parseDecimal(x, v.toString()); + } + + throw Error(invalidArgument + v); } Decimal.prototype = P; @@ -4455,8 +4472,8 @@ function clone(obj) { * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} - * y {number|string|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function div(x, y) { @@ -4468,7 +4485,7 @@ function div(x, y) { * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} The power to which to raise the base of the natural log. + * x {number|string|bigint|Decimal} The power to which to raise the base of the natural log. * */ function exp(x) { @@ -4479,7 +4496,7 @@ function exp(x) { /* * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function floor(x) { @@ -4493,7 +4510,7 @@ function floor(x) { * * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * */ function hypot() { @@ -4535,7 +4552,7 @@ function isDecimalInstance(obj) { * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function ln(x) { @@ -4549,8 +4566,8 @@ function ln(x) { * * log[y](x) * - * x {number|string|Decimal} The argument of the logarithm. - * y {number|string|Decimal} The base of the logarithm. + * x {number|string|bigint|Decimal} The argument of the logarithm. + * y {number|string|bigint|Decimal} The base of the logarithm. * */ function log(x, y) { @@ -4562,7 +4579,7 @@ function log(x, y) { * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function log2(x) { @@ -4574,7 +4591,7 @@ function log2(x) { * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function log10(x) { @@ -4585,22 +4602,22 @@ function log10(x) { /* * Return a new Decimal whose value is the maximum of the arguments. * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * */ function max() { - return maxOrMin(this, arguments, 'lt'); + return maxOrMin(this, arguments, -1); } /* * Return a new Decimal whose value is the minimum of the arguments. * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * */ function min() { - return maxOrMin(this, arguments, 'gt'); + return maxOrMin(this, arguments, 1); } @@ -4608,8 +4625,8 @@ function min() { * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|Decimal} - * y {number|string|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function mod(x, y) { @@ -4621,8 +4638,8 @@ function mod(x, y) { * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} - * y {number|string|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function mul(x, y) { @@ -4634,8 +4651,8 @@ function mul(x, y) { * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} The base. - * y {number|string|Decimal} The exponent. + * x {number|string|bigint|Decimal} The base. + * y {number|string|bigint|Decimal} The exponent. * */ function pow(x, y) { @@ -4753,7 +4770,7 @@ function random(sd) { * * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL). * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function round(x) { @@ -4769,7 +4786,7 @@ function round(x) { * -0 if x is -0, * NaN otherwise * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function sign(x) { @@ -4782,7 +4799,7 @@ function sign(x) { * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function sin(x) { @@ -4794,7 +4811,7 @@ function sin(x) { * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function sinh(x) { @@ -4806,7 +4823,7 @@ function sinh(x) { * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function sqrt(x) { @@ -4818,8 +4835,8 @@ function sqrt(x) { * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * - * x {number|string|Decimal} - * y {number|string|Decimal} + * x {number|string|bigint|Decimal} + * y {number|string|bigint|Decimal} * */ function sub(x, y) { @@ -4833,7 +4850,7 @@ function sub(x, y) { * * Only the result is rounded, not the intermediate calculations. * - * arguments {number|string|Decimal} + * arguments {number|string|bigint|Decimal} * */ function sum() { @@ -4853,7 +4870,7 @@ function sum() { * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function tan(x) { @@ -4865,7 +4882,7 @@ function tan(x) { * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * - * x {number|string|Decimal} A value in radians. + * x {number|string|bigint|Decimal} A value in radians. * */ function tanh(x) { @@ -4876,7 +4893,7 @@ function tanh(x) { /* * Return a new Decimal whose value is `x` truncated to an integer. * - * x {number|string|Decimal} + * x {number|string|bigint|Decimal} * */ function trunc(x) { diff --git a/test/hypothesis/README.txt b/test/hypothesis/README.txt new file mode 100644 index 0000000..4b73124 --- /dev/null +++ b/test/hypothesis/README.txt @@ -0,0 +1,2 @@ +See decimal PR #217 +https://github.com/MikeMcl/decimal.js/pull/217 \ No newline at end of file diff --git a/test/modules/sin.js b/test/modules/sin.js index 05036fe..971be72 100644 --- a/test/modules/sin.js +++ b/test/modules/sin.js @@ -119,5 +119,9 @@ T('sin', function () { t('22011131111011111.111111611113111111111151111111', 143, 2, '0.82582504036277799386306063085803210583251158969990606609364360685569588545519071481543672724620118406694191888115120286393881609546697317692404'); t('996270725099515169352424536636186062915113219400094989.8763797268889422850038402633796294758036260533902551191769915343780424028900449342752548782035', 46, 2, '0.6613706114081017074779805460666900787572253475'); t('0.780360750628373', 37, 5, '0.7035358359376557390803090830882458906'); - t('5900', 14, 4, '0.088879123681079'); + + // Mathematical: 0.088879123681079499990 (20 sig digs) + // Actual: 0.08887912368108 + // Expected: + //t('5900', 14, 4, '0.088879123681079'); });