mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
Add immutability tests
This commit is contained in:
parent
a62bb62b6b
commit
c2c4480881
@ -15,7 +15,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
});
|
});
|
||||||
|
|
||||||
var t = function (coefficient, exponent, sign, n) {
|
var t = function (coefficient, exponent, sign, n) {
|
||||||
T.assertDecimal(coefficient, exponent, sign, new Decimal(n));
|
T.assertEqualProps(coefficient, exponent, sign, new Decimal(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
t([0], 0, 1, 0);
|
t([0], 0, 1, 0);
|
||||||
|
@ -4,9 +4,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
T('absoluteValue');
|
T('absoluteValue');
|
||||||
|
|
||||||
function t(expected, value){
|
function t(expected, value){
|
||||||
var x = new Decimal(value);
|
T.assertEqual(expected, new Decimal(value).abs().valueOf());
|
||||||
T.assertEqual(expected, x.abs().valueOf());
|
|
||||||
T.assert(x.eq(value) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.acos(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).acos().valueOf());
|
T.assertEqual(expected, new Decimal(n).acos().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.acosh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).acosh().valueOf());
|
T.assertEqual(expected, new Decimal(n).acosh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.asin(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).asin().valueOf());
|
T.assertEqual(expected, new Decimal(n).asin().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.asinh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).asinh().valueOf());
|
T.assertEqual(expected, new Decimal(n).asinh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.atan(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).atan().valueOf());
|
T.assertEqual(expected, new Decimal(n).atan().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
t('-Infinity', 'Infinity', 20, 4, '-0.78539816339744830962');
|
t('-Infinity', 'Infinity', 20, 4, '-0.78539816339744830962');
|
||||||
t('-Infinity', '-Infinity', 20, 4, '-2.3561944901923449288');
|
t('-Infinity', '-Infinity', 20, 4, '-2.3561944901923449288');
|
||||||
|
|
||||||
// Fails (both 1 ulp)
|
// Fails (1 ulp)
|
||||||
//t('5900', '7999999999999999999999999999999999999999199999999999999999999.99999', 4, 1, '0.0000000000000000000000000000000000000000000000000000000007375');
|
//t('5900', '7999999999999999999999999999999999999999199999999999999999999.99999', 4, 1, '0.0000000000000000000000000000000000000000000000000000000007375');
|
||||||
//t('5999999.9999999999999999999999999999999999999999999998', '599999999999999999999999989999999999999999999999999999999999999999999999999999999999999999999999999999999999999999959999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.9999999999999999999999999999999999999999999', 2, 2, '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011');
|
//t('5999999.9999999999999999999999999999999999999999999998', '599999999999999999999999989999999999999999999999999999999999999999999999999999999999999999999999999999999999999999959999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.9999999999999999999999999999999999999999999', 2, 2, '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011');
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.atanh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).atanh().valueOf());
|
T.assertEqual(expected, new Decimal(n).atanh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.cbrt(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).cbrt().valueOf());
|
T.assertEqual(expected, new Decimal(n).cbrt().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
T('ceil');
|
T('ceil');
|
||||||
|
|
||||||
function t(expected, n) {
|
function t(expected, n) {
|
||||||
T.assertEqual(expected, Decimal.ceil(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).ceil().valueOf());
|
T.assertEqual(expected, new Decimal(n).ceil().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
|
|
||||||
function t(a, b, expected) {
|
function t(a, b, expected) {
|
||||||
T.assertEqual(expected, new Decimal(a).comparedTo(b));
|
T.assertEqual(expected, new Decimal(a).comparedTo(b));
|
||||||
//T.assertEqual(expected, new Decimal(a).cmp(b));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -23,9 +23,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
minE {number} [-EXP_LIMIT, 0]
|
minE {number} [-EXP_LIMIT, 0]
|
||||||
modulo {number} [0, 9]
|
modulo {number} [0, 9]
|
||||||
crypto {boolean|number} {true, false, 1, 0}
|
crypto {boolean|number} {true, false, 1, 0}
|
||||||
isExpBin {boolean|number} {true, false, 1, 0}
|
|
||||||
isExpHex {boolean|number} {true, false, 1, 0}
|
|
||||||
isExpOct {boolean|number} {true, false, 1, 0}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
t(Decimal.config({}) === Decimal);
|
t(Decimal.config({}) === Decimal);
|
||||||
|
@ -6,10 +6,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
var x = new Decimal(n);
|
T.assertEqual(expected, new Decimal(n).cos().valueOf());
|
||||||
T.assertEqual(expected, x.cos().valueOf());
|
|
||||||
T.assertEqual(expected, Decimal.cos(n).valueOf());
|
|
||||||
T.assert(x.eq(n) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
@ -42,7 +39,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
t('0.3834777777', 3, 4, '0.927');
|
t('0.3834777777', 3, 4, '0.927');
|
||||||
t('16355.3334', 9, 4, '0.979658167');
|
t('16355.3334', 9, 4, '0.979658167');
|
||||||
t('0.997737586545708065463833973312637902054356957830033', 5, 5, '0.5422');
|
t('0.997737586545708065463833973312637902054356957830033', 5, 5, '0.5422');
|
||||||
t('-0.997737586545708065463833973312637902054356957830033', 5, 5, '0.5422');
|
|
||||||
t('7803700', 4, 2, '-0.9032');
|
t('7803700', 4, 2, '-0.9032');
|
||||||
t('841822094288189413002174891207354317', 5, 2, '-0.86017');
|
t('841822094288189413002174891207354317', 5, 2, '-0.86017');
|
||||||
t('67942', 8, 3, '-0.33958641');
|
t('67942', 8, 3, '-0.33958641');
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.cosh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).cosh().valueOf());
|
T.assertEqual(expected, new Decimal(n).cosh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +124,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
Decimal.precision = sd;
|
Decimal.precision = sd;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, new Decimal(dividend).div(divisor).valueOf());
|
T.assertEqual(expected, new Decimal(dividend).div(divisor).valueOf());
|
||||||
//T.assertEqual(expected, new Decimal(dividend).div(new Decimal(divisor)).valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t('999.5', 1, '1000', 3, 0);
|
t('999.5', 1, '1000', 3, 0);
|
||||||
|
@ -6,10 +6,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, expected, pr, rm) {
|
function t(n, expected, pr, rm) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
var x = new Decimal(n);
|
T.assertEqual(expected, new Decimal(n).exp().valueOf());
|
||||||
T.assertEqual(expected, x.exp().valueOf());
|
|
||||||
T.assertEqual(expected, Decimal.exp(n).valueOf());
|
|
||||||
T.assert(x.eq(n) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -4,7 +4,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
T('floor');
|
T('floor');
|
||||||
|
|
||||||
function t(expected, n) {
|
function t(expected, n) {
|
||||||
T.assertEqual(expected, Decimal.floor(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).floor().valueOf());
|
T.assertEqual(expected, new Decimal(n).floor().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
T('fromJSON');
|
T('fromJSON');
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
//crypto: true,
|
|
||||||
precision: Math.random() * 40 + 1 | 0,
|
precision: Math.random() * 40 + 1 | 0,
|
||||||
rounding: Math.random() * 9 | 0,
|
rounding: Math.random() * 9 | 0,
|
||||||
toExpNeg: -7,
|
toExpNeg: -7,
|
||||||
@ -25,21 +24,19 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
// Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
|
// Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
|
||||||
r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
|
r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
|
||||||
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
|
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
|
||||||
//( n = Math.floor( Math.random() * 40 ) + '' ).slice( Math.random() * n.length | 0 ));
|
|
||||||
|
|
||||||
//console.log(r.toString());
|
//console.log(r.toString());
|
||||||
|
|
||||||
toJ = r.toJSON();
|
toJ = r.toJSON();
|
||||||
//console.log(' toJ: ' + toJ);
|
//console.log(' toJSON: ' + toJ);
|
||||||
|
|
||||||
fromJ = Decimal.fromJSON(toJ);
|
fromJ = Decimal.fromJSON(toJ);
|
||||||
//console.log(' fromJ: ' + fromJ);
|
//console.log(' fromJSON: ' + fromJ);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (!r.eq(fromJ)) {
|
if (!r.eq(fromJ)) {
|
||||||
console.error(' r: ' + r);
|
console.error(' r: ' + r);
|
||||||
console.error(' toJ: ' + toJ);
|
console.error(' toJSON: ' + toJ);
|
||||||
console.error(' fromJ: ' + fromJ);
|
console.error(' fromJSON: ' + fromJ);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
T.assert(r.eq(fromJ));
|
T.assert(r.eq(fromJ));
|
||||||
|
561
test/modules/immutability.js
Normal file
561
test/modules/immutability.js
Normal file
@ -0,0 +1,561 @@
|
|||||||
|
// Tests immutability of operand[s] for all applicable methods.
|
||||||
|
// Also tests each Decimal.prototype method against its equivalent Decimal method where applicable.
|
||||||
|
if (typeof T === 'undefined') require('../setup');
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
T('immutability');
|
||||||
|
|
||||||
|
Decimal.config({
|
||||||
|
precision: 20,
|
||||||
|
rounding: 4,
|
||||||
|
toExpNeg: -7,
|
||||||
|
toExpPos: 21,
|
||||||
|
minE: -9e15,
|
||||||
|
maxE: 9e15
|
||||||
|
});
|
||||||
|
|
||||||
|
// Integer [0, 9e15), with each possible number of digits, [1, 16], equally likely.
|
||||||
|
function randInt() {
|
||||||
|
return Math.floor(Math.random() * 9e15 / Math.pow(10, Math.random() * 16 | 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
var a, aa, b, bb, i, k, n, t, v, x, y, z;
|
||||||
|
|
||||||
|
t = T.assertEqualDecimal;
|
||||||
|
|
||||||
|
v = [
|
||||||
|
0,
|
||||||
|
NaN,
|
||||||
|
Infinity,
|
||||||
|
-Infinity,
|
||||||
|
0.5,
|
||||||
|
-0.5,
|
||||||
|
1,
|
||||||
|
-1,
|
||||||
|
(x = Decimal.random()),
|
||||||
|
x.neg(),
|
||||||
|
(x = randInt()),
|
||||||
|
-x,
|
||||||
|
(x = Decimal.random().plus(randInt())),
|
||||||
|
x.neg()
|
||||||
|
];
|
||||||
|
|
||||||
|
for (i = 0; i < v.length; i++) {
|
||||||
|
a = new Decimal(v[i]);
|
||||||
|
aa = new Decimal(v[i]);
|
||||||
|
k = (Math.random() * 10 | 0) / 10;
|
||||||
|
b = k == 0.5 ? new Decimal(a) : a[k < 0.5 ? 'plus' : 'minus'](Decimal.random().plus(randInt()));
|
||||||
|
bb = new Decimal(b);
|
||||||
|
n = Math.random() * 20 + 1 | 0;
|
||||||
|
|
||||||
|
x = a.absoluteValue();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.abs();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.abs(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.ceil();
|
||||||
|
t(a, aa);
|
||||||
|
y = Decimal.ceil(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = a.comparedTo(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.cmp(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.cosine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.cos();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.cos(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.cubeRoot();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.cbrt();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.cbrt(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.decimalPlaces();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.dp();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.dividedBy(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.div(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.div(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.dividedToIntegerBy(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.divToInt(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = a.equals(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.eq(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.floor();
|
||||||
|
t(a, aa);
|
||||||
|
y = Decimal.floor(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = a.greaterThan(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.gt(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.greaterThanOrEqualTo(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.gte(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
// Omit these hyperbolic methods if a is large, as they are too time-consuming.
|
||||||
|
if (a.abs().lt(1000)) {
|
||||||
|
x = a.hyperbolicCosine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.cosh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.cosh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.hyperbolicSine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.sinh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.sinh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.hyperbolicTangent();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.tanh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.tanh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
// a [-1, 1]
|
||||||
|
x = a.inverseCosine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.acos();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.acos(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
// a [1, Infinity]
|
||||||
|
x = a.inverseHyperbolicCosine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.acosh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.acosh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.inverseHyperbolicSine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.asinh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.asinh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
// a [-1, 1]
|
||||||
|
x = a.inverseHyperbolicTangent();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.atanh();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.atanh(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
// a [-1, 1]
|
||||||
|
x = a.inverseSine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.asin();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.asin(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.inverseTangent();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.atan();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.atan(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
a.isFinite();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.isInteger();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.isInt();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
a.isNaN();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.isNegative();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.isNeg();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.isPositive();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.isPos();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
a.isZero();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.lessThan(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.lt(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.lessThanOrEqualTo(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.lte(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.logarithm();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.log();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.log(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.minus(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.sub(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.sub(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.modulo(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.mod(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.mod(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.naturalExponential();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.exp();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.exp(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.naturalLogarithm();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.ln();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.ln(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.negated();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.neg();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = a.plus(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.add(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.add(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.precision();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.sd();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
x = a.round();
|
||||||
|
t(a, aa);
|
||||||
|
y = Decimal.round(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = a.sine();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.sin();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.sin(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.squareRoot();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.sqrt();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.sqrt(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.tangent();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.tan();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.tan(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
x = a.times(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.mul(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.mul(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
a.toBinary();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.toDecimalPlaces(n);
|
||||||
|
t(a, aa);
|
||||||
|
y = a.toDP(n);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
a.toExponential(n);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
a.toFixed(n);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
a.toFraction();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.toHexadecimal();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.toHex();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
T.assertEqual(x, y);
|
||||||
|
|
||||||
|
a.toJSON();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
a.toNearest(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
a.toNumber();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
a.toOctal();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.toPower(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
y = a.pow(b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
z = Decimal.pow(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
a.toPrecision(n);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.toSignificantDigits(n);
|
||||||
|
t(a, aa);
|
||||||
|
y = a.toSD(n);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
a.toString();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
x = a.truncated();
|
||||||
|
t(a, aa);
|
||||||
|
y = a.trunc();
|
||||||
|
t(a, aa);
|
||||||
|
z = Decimal.trunc(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
t(y, z);
|
||||||
|
|
||||||
|
a.valueOf();
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
Decimal.atan2(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
|
||||||
|
Decimal.hypot(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
x = Decimal.log(a, 10);
|
||||||
|
t(a, aa);
|
||||||
|
y = Decimal.log10(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
x = Decimal.log(a, 2);
|
||||||
|
t(a, aa);
|
||||||
|
y = Decimal.log2(a);
|
||||||
|
t(a, aa);
|
||||||
|
|
||||||
|
t(x, y);
|
||||||
|
|
||||||
|
Decimal.max(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
Decimal.min(a, b);
|
||||||
|
t(a, aa);
|
||||||
|
t(b, bb);
|
||||||
|
|
||||||
|
Decimal.sign(a);
|
||||||
|
t(a, aa);
|
||||||
|
}
|
||||||
|
|
||||||
|
T.stop();
|
||||||
|
})();
|
||||||
|
|
||||||
|
/*
|
||||||
|
// All methods tested above except:
|
||||||
|
Decimal.clone();
|
||||||
|
Decimal.config();
|
||||||
|
Decimal.fromJSON();
|
||||||
|
Decimal.noConflict();
|
||||||
|
Decimal.random();
|
||||||
|
*/
|
@ -3,9 +3,8 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
(function () {
|
(function () {
|
||||||
T('integer pow');
|
T('integer pow');
|
||||||
|
|
||||||
// Highest finite exponent is 100
|
// Highest finite exponent here is 100
|
||||||
function t(expected, n, exp) {
|
function t(expected, n, exp) {
|
||||||
T.assertEqual(expected, Decimal.pow(n, exp).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).pow(exp).valueOf());
|
T.assertEqual(expected, new Decimal(n).pow(exp).valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
Decimal.precision = sd;
|
Decimal.precision = sd;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, new Decimal(n).ln().valueOf());
|
T.assertEqual(expected, new Decimal(n).ln().valueOf());
|
||||||
//T.assertEqual(expected, Decimal.ln(n).valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -7,7 +7,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
Decimal.precision = sd;
|
Decimal.precision = sd;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, new Decimal(n).log(base).valueOf());
|
T.assertEqual(expected, new Decimal(n).log(base).valueOf());
|
||||||
//T.assertEqual(expected, Decimal.log(n, base).valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
t = function (base, exp, expected, sd, rm) {
|
t = function (base, exp, expected, sd, rm) {
|
||||||
Decimal.precision = sd;
|
Decimal.precision = sd;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.pow(base, exp).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(base).pow(exp).valueOf());
|
T.assertEqual(expected, new Decimal(base).pow(exp).valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
// Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
|
// Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
|
||||||
r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
|
r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
|
||||||
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
|
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
|
||||||
//T.write(' r: ' + r);
|
//console.log(' r: ' + r);
|
||||||
|
|
||||||
// Random rounding mode.
|
// Random rounding mode.
|
||||||
Decimal.rounding = Math.random() * 9 | 0;
|
Decimal.rounding = Math.random() * 9 | 0;
|
||||||
@ -28,11 +28,11 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
Decimal.precision = Math.random() * 40 + 1 | 0;
|
Decimal.precision = Math.random() * 40 + 1 | 0;
|
||||||
|
|
||||||
p = r.pow(0.5);
|
p = r.pow(0.5);
|
||||||
//T.write(' r.pow(0.5): ' + p);
|
//console.log(' r.pow(0.5): ' + p);
|
||||||
|
|
||||||
// sqrt is much faster than pow(0.5)
|
// sqrt is much faster than pow(0.5)
|
||||||
s = r.sqrt();
|
s = r.sqrt();
|
||||||
//T.write(' r.sqrt(): ' + s);
|
//console.log(' r.sqrt(): ' + s);
|
||||||
|
|
||||||
T.assertEqual(p.valueOf(), s.valueOf());
|
T.assertEqual(p.valueOf(), s.valueOf());
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(expected, n, rm) {
|
function t(expected, n, rm) {
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, new Decimal(n).round().valueOf());
|
T.assertEqual(expected, new Decimal(n).round().valueOf());
|
||||||
T.assertEqual(expected, Decimal.round(n).valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -6,10 +6,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
var x = new Decimal(n);
|
T.assertEqual(expected, new Decimal(n).sin().valueOf());
|
||||||
T.assertEqual(expected, x.sin().valueOf());
|
|
||||||
T.assertEqual(expected, Decimal.sin(n).valueOf());
|
|
||||||
T.assert(x.eq(n) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
@ -30,9 +27,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
t('93332358216', 48, 5, '0.000816283943127306029649893248669685836695194145277');
|
t('93332358216', 48, 5, '0.000816283943127306029649893248669685836695194145277');
|
||||||
t('766737078569022420173292205068953186848344673451240027655054631094318050', 18, 6, '0.00111135055272494455');
|
t('766737078569022420173292205068953186848344673451240027655054631094318050', 18, 6, '0.00111135055272494455');
|
||||||
|
|
||||||
t('-93332358216', 48, 5, '-0.000816283943127306029649893248669685836695194145277');
|
|
||||||
t('-766737078569022420173292205068953186848344673451240027655054631094318050', 18, 6, '-0.00111135055272494455');
|
|
||||||
|
|
||||||
t('0.359136383962093189473162700700590970518456985109181875257764726415814563424613193774806585890478208084275468196568294033', 38, 1, '0.35146584650000000000000000000000000006');
|
t('0.359136383962093189473162700700590970518456985109181875257764726415814563424613193774806585890478208084275468196568294033', 38, 1, '0.35146584650000000000000000000000000006');
|
||||||
t('3202222222227222222222222222222224222222222222222222222222.222222222222222222222222222', 7, 0, '0.5499568');
|
t('3202222222227222222222222222222224222222222222222222222222.222222222222222222222222222', 7, 0, '0.5499568');
|
||||||
t('8', 5, 6, '0.98936');
|
t('8', 5, 6, '0.98936');
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.sinh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).sinh().valueOf());
|
T.assertEqual(expected, new Decimal(n).sinh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
Decimal.precision = sd;
|
Decimal.precision = sd;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, new Decimal(n).sqrt().valueOf());
|
T.assertEqual(expected, new Decimal(n).sqrt().valueOf());
|
||||||
//T.assertEqual(expected, Decimal.sqrt(n).valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -6,10 +6,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
var x = new Decimal(n);
|
T.assertEqual(expected, new Decimal(n).tan().valueOf());
|
||||||
T.assertEqual(expected, x.tan().valueOf());
|
|
||||||
T.assertEqual(expected, Decimal.tan(n).valueOf());
|
|
||||||
T.assert(x.eq(n) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
@ -28,10 +25,8 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
t('-Infinity', 40, 4, 'NaN');
|
t('-Infinity', 40, 4, 'NaN');
|
||||||
|
|
||||||
t('3', 5, 5, '-0.14255');
|
t('3', 5, 5, '-0.14255');
|
||||||
t('-3', 5, 5, '0.14255');
|
|
||||||
t('0.8', 7, 6, '1.029639');
|
t('0.8', 7, 6, '1.029639');
|
||||||
t('539998661123466984649191689138494900416386817996524', 7, 6, '32.04695');
|
t('539998661123466984649191689138494900416386817996524', 7, 6, '32.04695');
|
||||||
t('-539998661123466984649191689138494900416386817996524', 7, 6, '-32.04695');
|
|
||||||
t('0.5172293543584894071801901902', 5, 1, '0.56888');
|
t('0.5172293543584894071801901902', 5, 1, '0.56888');
|
||||||
t('5', 10, 5, '-3.380515006');
|
t('5', 10, 5, '-3.380515006');
|
||||||
t('0.5283979979897015', 1, 4, '0.6');
|
t('0.5283979979897015', 1, 4, '0.6');
|
||||||
|
@ -6,7 +6,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
function t(n, pr, rm, expected) {
|
function t(n, pr, rm, expected) {
|
||||||
Decimal.precision = pr;
|
Decimal.precision = pr;
|
||||||
Decimal.rounding = rm;
|
Decimal.rounding = rm;
|
||||||
T.assertEqual(expected, Decimal.tanh(n).valueOf());
|
|
||||||
T.assertEqual(expected, new Decimal(n).tanh().valueOf());
|
T.assertEqual(expected, new Decimal(n).tanh().valueOf());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,7 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
T('times');
|
T('times');
|
||||||
|
|
||||||
var t = function (multiplicand, multiplier, expected) {
|
var t = function (multiplicand, multiplier, expected) {
|
||||||
var x = new Decimal(multiplicand);
|
T.assertEqual(expected, new Decimal(multiplicand).times(multiplier).valueOf());
|
||||||
T.assertEqual(expected, x.times(multiplier).valueOf());
|
|
||||||
T.assert(x.eq(multiplicand) || x.isNaN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -20,13 +20,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
maxE: 9e15
|
maxE: 9e15
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
|
||||||
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed
|
|
||||||
JS: 2.26.toFixed(5) returns 2.26000
|
|
||||||
JS: -2.26.toFixed(5) returns -2.26
|
|
||||||
Decimal: new Decimal(-2.26).toFixed(5) returns -2.26000
|
|
||||||
*/
|
|
||||||
|
|
||||||
t('100.0', 99.9512986, 1);
|
t('100.0', 99.9512986, 1);
|
||||||
t('10.0', 9.95036, 1);
|
t('10.0', 9.95036, 1);
|
||||||
t('1.0', 0.99, 1);
|
t('1.0', 0.99, 1);
|
||||||
|
@ -5,7 +5,6 @@ if (typeof T === 'undefined') require('../setup');
|
|||||||
|
|
||||||
function t(expected, n) {
|
function t(expected, n) {
|
||||||
T.assertEqual(expected, Decimal.trunc(n).valueOf());
|
T.assertEqual(expected, Decimal.trunc(n).valueOf());
|
||||||
T.assertEqual(expected, new Decimal(n).trunc().valueOf());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Decimal.config({
|
Decimal.config({
|
||||||
|
@ -36,7 +36,29 @@ T = (function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
T.assertDecimal = function (digits, exponent, sign, n) {
|
T.assertEqual = function (expected, actual) {
|
||||||
|
++testNumber;
|
||||||
|
if (expected === actual || expected !== expected && actual !== actual) {
|
||||||
|
++passed;
|
||||||
|
} else {
|
||||||
|
writeError('\n Test number ' + testNumber + ' failed: assertEqual');
|
||||||
|
writeError(' Expected: ' + expected);
|
||||||
|
writeError(' Actual: ' + actual);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
T.assertEqualDecimal = function (x, y) {
|
||||||
|
++testNumber;
|
||||||
|
if (x.eq(y) || x.isNaN() && y.isNaN()) {
|
||||||
|
++passed;
|
||||||
|
} else {
|
||||||
|
writeError('\n Test number ' + testNumber + ' failed: assertEqualDecimal');
|
||||||
|
writeError(' x: ' + x.valueOf());
|
||||||
|
writeError(' y: ' + y.valueOf());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
T.assertEqualProps = function (digits, exponent, sign, n) {
|
||||||
var i = 0,
|
var i = 0,
|
||||||
len = digits.length;
|
len = digits.length;
|
||||||
++testNumber;
|
++testNumber;
|
||||||
@ -44,27 +66,13 @@ T = (function () {
|
|||||||
if (i === len && i === n.d.length && exponent === n.e && sign === n.s) {
|
if (i === len && i === n.d.length && exponent === n.e && sign === n.s) {
|
||||||
++passed;
|
++passed;
|
||||||
} else {
|
} else {
|
||||||
writeError('\n Test number ' + testNumber + ' failed: assertDecimal');
|
writeError('\n Test number ' + testNumber + ' failed: assertEqualProps');
|
||||||
writeError(' Expected digits: ' + digits);
|
writeError(' Expected digits: ' + digits);
|
||||||
writeError(' Expected exponent: ' + exponent);
|
writeError(' Expected exponent: ' + exponent);
|
||||||
writeError(' Expected sign: ' + sign);
|
writeError(' Expected sign: ' + sign);
|
||||||
writeError(' Actual digits: ' + n.d);
|
writeError(' Actual digits: ' + n.d);
|
||||||
writeError(' Actual exponent: ' + n.e);
|
writeError(' Actual exponent: ' + n.e);
|
||||||
writeError(' Actual sign: ' + n.s);
|
writeError(' Actual sign: ' + n.s);
|
||||||
//process.exit();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
T.assertEqual = function (expected, actual) {
|
|
||||||
++testNumber;
|
|
||||||
if (expected === actual || expected !== expected && actual !== actual) {
|
|
||||||
++passed;
|
|
||||||
//write('\n Expected and actual: ' + actual);
|
|
||||||
} else {
|
|
||||||
writeError('\n Test number ' + testNumber + ' failed: assertEqual');
|
|
||||||
writeError(' Expected: ' + expected);
|
|
||||||
writeError(' Actual: ' + actual);
|
|
||||||
//process.exit();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -78,12 +86,10 @@ T = (function () {
|
|||||||
}
|
}
|
||||||
if (actual instanceof Error && /DecimalError/.test(actual.message)) {
|
if (actual instanceof Error && /DecimalError/.test(actual.message)) {
|
||||||
++passed;
|
++passed;
|
||||||
//write('\n Expected and actual: ' + actual);
|
|
||||||
} else {
|
} else {
|
||||||
writeError('\n Test number ' + testNumber + ' failed: assertException');
|
writeError('\n Test number ' + testNumber + ' failed: assertException');
|
||||||
writeError(' Expected: ' + msg + ' to raise a [DecimalError].');
|
writeError(' Expected: ' + msg + ' to raise a [DecimalError].');
|
||||||
writeError(' Actual: ' + (actual || 'no exception'));
|
writeError(' Actual: ' + (actual || 'no exception'));
|
||||||
//process.exit();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
'exp',
|
'exp',
|
||||||
'floor',
|
'floor',
|
||||||
'hypot',
|
'hypot',
|
||||||
|
'immutability',
|
||||||
'intPow',
|
'intPow',
|
||||||
'isFiniteEtc',
|
'isFiniteEtc',
|
||||||
'ln',
|
'ln',
|
||||||
|
@ -27,6 +27,7 @@ console.log('\n Testing decimal.js\n');
|
|||||||
'exp',
|
'exp',
|
||||||
'floor',
|
'floor',
|
||||||
'hypot',
|
'hypot',
|
||||||
|
'immutability',
|
||||||
'intPow',
|
'intPow',
|
||||||
'isFiniteEtc',
|
'isFiniteEtc',
|
||||||
'ln',
|
'ln',
|
||||||
|
Loading…
Reference in New Issue
Block a user