From 3f9eeb089715c80ba7290a37f9f399c79cd8a7a6 Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Wed, 8 May 2019 17:05:12 +0100 Subject: [PATCH] #128 Accept `+` prefix --- decimal.js | 10 ++++++++-- decimal.mjs | 12 +++++++++--- test/modules/Decimal.js | 25 ++++++++++++++++++++----- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/decimal.js b/decimal.js index 46358fc..23cc2d3 100644 --- a/decimal.js +++ b/decimal.js @@ -3,7 +3,7 @@ /* - * decimal.js v10.1.1 + * decimal.js v10.2.0 * An arbitrary-precision Decimal type for JavaScript. * https://github.com/MikeMcl/decimal.js * Copyright (c) 2019 Michael Mclaughlin @@ -4341,10 +4341,12 @@ } // Minus sign? - if (v.charCodeAt(0) === 45) { + 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; } @@ -4461,6 +4463,8 @@ * * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) * + * arguments {number|string|Decimal} + * */ function hypot() { var i, n, @@ -4735,6 +4739,8 @@ * -0 if x is -0, * NaN otherwise * + * x {number|string|Decimal} + * */ function sign(x) { x = new this(x); diff --git a/decimal.mjs b/decimal.mjs index 7948fad..ca5fcd2 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -1,5 +1,5 @@ /* - * decimal.js v10.1.1 + * decimal.js v10.2.0 * An arbitrary-precision Decimal type for JavaScript. * https://github.com/MikeMcl/decimal.js * Copyright (c) 2019 Michael Mclaughlin @@ -3728,7 +3728,7 @@ function tinyPow(b, e) { var n = b; while (--e) n *= b; return n; -} +} // Return the absolute value of `x` reduced to less than or equal to half pi. @@ -4337,10 +4337,12 @@ function clone(obj) { } // Minus sign? - if (v.charCodeAt(0) === 45) { + 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; } @@ -4457,6 +4459,8 @@ function floor(x) { * * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) * + * arguments {number|string|Decimal} + * */ function hypot() { var i, n, @@ -4731,6 +4735,8 @@ function round(x) { * -0 if x is -0, * NaN otherwise * + * x {number|string|Decimal} + * */ function sign(x) { x = new this(x); diff --git a/test/modules/Decimal.js b/test/modules/Decimal.js index 6d38237..24c5897 100644 --- a/test/modules/Decimal.js +++ b/test/modules/Decimal.js @@ -124,6 +124,7 @@ T('Decimal', function () { t('0', '0b0'); t('0', '0B0'); t('-5', '-0b101'); + t('5', '+0b101'); t('1.5', '0b1.1'); t('-1.5', '-0b1.1'); @@ -139,6 +140,7 @@ T('Decimal', function () { // Octal. t('8', '0o10'); t('-8.5', '-0O010.4'); + t('8.5', '+0O010.4'); t('-262144.000000059604644775390625', '-0o1000000.00000001'); t('572315667420.390625', '0o10250053005734.31'); @@ -146,6 +148,7 @@ T('Decimal', function () { t('1', '0x00001'); t('255', '0xff'); t('-15.5', '-0Xf.8'); + t('15.5', '+0Xf.8'); t('-16777216.00000000023283064365386962890625', '-0x1000000.00000001'); t('325927753012307620476767402981591827744994693483231017778102969592507', '0xc16de7aa5bf90c3755ef4dea45e982b351b6e00cd25a82dcfe0646abb'); @@ -156,14 +159,18 @@ T('Decimal', function () { } t('NaN', NaN); + t('NaN', -NaN); t('NaN', 'NaN'); t('NaN', '-NaN'); + t('NaN', '+NaN'); tx(function () {new Decimal(' NaN')}, "' NaN'"); tx(function () {new Decimal('NaN ')}, "'NaN '"); tx(function () {new Decimal(' NaN ')}, "' NaN '"); - tx(function () {new Decimal('+NaN')}, "'+NaN'"); + tx(function () {new Decimal(' -NaN')}, "' -NaN'"); tx(function () {new Decimal(' +NaN')}, "' +NaN'"); + tx(function () {new Decimal('-NaN ')}, "'-NaN '"); + tx(function () {new Decimal('+NaN ')}, "'+NaN '"); tx(function () {new Decimal('.NaN')}, "'.NaN'"); tx(function () {new Decimal('NaN.')}, "'NaN.'"); @@ -171,11 +178,12 @@ T('Decimal', function () { t('-Infinity', -Infinity); t('Infinity', 'Infinity'); t('-Infinity', '-Infinity'); + t('Infinity', '+Infinity'); tx(function () {new Decimal(' Infinity')}, "' Infinity '"); tx(function () {new Decimal('Infinity ')}, "'Infinity '"); tx(function () {new Decimal(' Infinity ')}, "' Infinity '"); - tx(function () {new Decimal('+Infinity')}, "'+Infinity'"); + tx(function () {new Decimal(' -Infinity')}, "' -Infinity'"); tx(function () {new Decimal(' +Infinity')}, "' +Infinity'"); tx(function () {new Decimal('.Infinity')}, "'.Infinity'"); tx(function () {new Decimal('Infinity.')}, "'Infinity.'"); @@ -197,7 +205,7 @@ T('Decimal', function () { tx(function () {new Decimal('0-')}, "'0-'"); tx(function () {new Decimal(' -0')}, "' -0'"); tx(function () {new Decimal('-0 ')}, "'-0 '"); - tx(function () {new Decimal('+0')}, "'+0'"); + tx(function () {new Decimal('+0 ')}, "'+0 '"); tx(function () {new Decimal(' +0')}, "' +0'"); tx(function () {new Decimal(' .0')}, "' .0'"); tx(function () {new Decimal('0. ')}, "'0. '"); @@ -222,12 +230,16 @@ T('Decimal', function () { t('0.1', '.1'); t('0.1', '.1'); t('-0.1', '-.1'); + t('0.1', '+.1'); t('1', '1.'); t('1', '1.0'); t('-1', '-1.'); + t('1', '+1.'); t('-1', '-1.0000'); + t('1', '1.0000'); t('1', '1.00000000'); t('-1', '-1.000000000000000000000000'); + t('1', '+1.000000000000000000000000'); tx(function () {new Decimal(' 1')}, "' 1'"); tx(function () {new Decimal('1 ')}, "'1 '"); @@ -235,8 +247,8 @@ T('Decimal', function () { tx(function () {new Decimal('1-')}, "'1-'"); tx(function () {new Decimal(' -1')}, "' -1'"); tx(function () {new Decimal('-1 ')}, "'-1 '"); - tx(function () {new Decimal('+1')}, "'+1'"); tx(function () {new Decimal(' +1')}, "' +1'"); + tx(function () {new Decimal('+1 ')}, "'+1'"); tx(function () {new Decimal('.1.')}, "'.1.'"); tx(function () {new Decimal('+-1')}, "'+-1'"); tx(function () {new Decimal('-+1')}, "'-+1'"); @@ -262,8 +274,9 @@ T('Decimal', function () { t('123.456789', 123.456789); t('-123.456789', -123.456789); - t('123.456789', '123.456789'); t('-123.456789', '-123.456789'); + t('123.456789', '123.456789'); + t('123.456789', '+123.456789'); tx(function () {new Decimal(void 0)}, "void 0"); tx(function () {new Decimal('undefined')}, "'undefined'"); @@ -282,7 +295,9 @@ T('Decimal', function () { tx(function () {new Decimal('ff')}, "'ff'"); tx(function () {new Decimal('0xg')}, "'oxg'"); tx(function () {new Decimal('0Xfi')}, "'0Xfi'"); + tx(function () {new Decimal('++45')}, "'++45'"); tx(function () {new Decimal('--45')}, "'--45'"); tx(function () {new Decimal('9.99--')}, "'9.99--'"); + tx(function () {new Decimal('9.99++')}, "'9.99++'"); tx(function () {new Decimal('0 0')}, "'0 0'"); });