mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2026-03-02 03:49:24 +00:00
v3.0.0
This commit is contained in:
@@ -45,7 +45,6 @@
|
||||
<!-- <script src='../toSD.js'></script> -->
|
||||
<!-- <script src='../toStringEtc.js'></script> -->
|
||||
<!-- <script src='../trunc.js'></script> -->
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
130
test/random.js
130
test/random.js
@@ -1,6 +1,6 @@
|
||||
var count = (function random(Decimal) {
|
||||
var start = +new Date(),
|
||||
error, i, j, limit, log, pr, u,
|
||||
dp, error, i, j, k, log, m, r,
|
||||
passed = 0,
|
||||
total = 0;
|
||||
|
||||
@@ -14,20 +14,7 @@ var count = (function random(Decimal) {
|
||||
}
|
||||
|
||||
if (!Decimal && typeof require === 'function') {
|
||||
Decimal = require('../decimal');
|
||||
}
|
||||
|
||||
function assert(result, r, message) {
|
||||
total++;
|
||||
if (result === true) {
|
||||
passed++;
|
||||
//log('\n r: ' + r);
|
||||
} else {
|
||||
error('\n Test number: ' + total + ' failed');
|
||||
error(' r: ' + r);
|
||||
error(' ' + message);
|
||||
//process.exit();
|
||||
}
|
||||
Decimal = require('../decimal.js');
|
||||
}
|
||||
|
||||
function assertException(func, message) {
|
||||
@@ -49,74 +36,65 @@ var count = (function random(Decimal) {
|
||||
}
|
||||
}
|
||||
|
||||
function T(limit, pr){
|
||||
var i, r, d;
|
||||
|
||||
if ( !limit ) {
|
||||
limit = new Decimal(1);
|
||||
pr = Decimal.precision;
|
||||
}
|
||||
|
||||
for ( i = 0; i < 17; i++ ) {
|
||||
r = Decimal.random(limit, pr);
|
||||
//log(r.toString());
|
||||
d = r.c.length;
|
||||
|
||||
if ( pr == null ) {
|
||||
d = Math.max(d, r.e + 1) - r.e - 1;
|
||||
assert(d === 0, r, 'dp is not 0: ' + d);
|
||||
} else {
|
||||
assert(d <= pr, r, 'sd: ' + d + ' > pr: ' + pr);
|
||||
}
|
||||
|
||||
assert(r.gte(0), r, 'r < 0');
|
||||
assert(r.lt(limit), r, 'r >= limit: ' + limit);
|
||||
}
|
||||
}
|
||||
|
||||
log('\n Testing random...');
|
||||
|
||||
// First iteration crypto: false, second iteration crypto: true.
|
||||
Decimal.config({ crypto: false, errors: true });
|
||||
Decimal.config({ errors: true, crypto: false });
|
||||
|
||||
for ( i = 0; i < 2; i++ ) {
|
||||
//log( '\n crypto: ' + Decimal.crypto );
|
||||
for ( i = 0; i < 9996; i++ ) {
|
||||
|
||||
Decimal.precision = Math.random() * 100 + 1 | 0;
|
||||
//log( Decimal.precision );
|
||||
//Decimal.crypto = false;
|
||||
//Decimal.crypto = true;
|
||||
// 50% chance that Decimal.crypto is true.
|
||||
//Decimal.crypto = Math.random() > 0.5;
|
||||
|
||||
for ( j = 0; j < 10; j++ ) {
|
||||
|
||||
T();
|
||||
T(u);
|
||||
T(null);
|
||||
T(1);
|
||||
T(1, u);
|
||||
T(1, null);
|
||||
T(10);
|
||||
T(1000);
|
||||
|
||||
// limit will have 1 - 17 integer digits and 1 - 17 fraction digits.
|
||||
limit = +(Math.random() + '').slice(2, Math.random() * 17 + 3 | 0) +
|
||||
(Math.random() + '').slice(1, Math.random() * 17 + 3 | 0);
|
||||
|
||||
if ( +limit == 0 ) {
|
||||
limit = 1;
|
||||
}
|
||||
|
||||
//log(' limit: ' + limit);
|
||||
|
||||
T(limit);
|
||||
|
||||
// Precision. Integer 1 - 80.
|
||||
pr = Math.random() * 80 + 1 | 0;
|
||||
|
||||
//log(' pr: ' + pr);
|
||||
|
||||
T(limit, pr);
|
||||
if ( Math.random() > 0.5 ) {
|
||||
dp = Decimal.precision = Math.random() * 10 + 1 | 0;
|
||||
r = Decimal.random();
|
||||
} else {
|
||||
dp = Math.random() * 10 | 0;
|
||||
r = Decimal.random(dp);
|
||||
}
|
||||
|
||||
Decimal.config({ crypto: true });
|
||||
//log(r.toString());
|
||||
|
||||
if ( r.c[0] ) {
|
||||
j = r.c.length;
|
||||
k = r.c[j - 1];
|
||||
j *= 7;
|
||||
|
||||
// Decrement for trailing zeros in last element of r.c.
|
||||
for ( ; k % 10 === 0; k /= 10, j-- );
|
||||
} else {
|
||||
j = 0;
|
||||
}
|
||||
|
||||
// Check number of decimal places (j is actual dp).
|
||||
if ( j > dp ) {
|
||||
m = ' r.c.length - r.e - 1 > dp';
|
||||
|
||||
// Check 0 <= r < 1
|
||||
} else if ( r.lt(0) || r.gte(1) ) {
|
||||
m = ' r.lt(0) || r.gte(1)';
|
||||
|
||||
// Check that the attributes of r are formed correctly.
|
||||
} else if ( !r.eq( new Decimal(r) ) || !r.eq( new Decimal( r.toString() ) ) ) {
|
||||
m = ' !r.eq( new Decimal(r) ) || !r.eq( new Decimal( r.toString() ) )';
|
||||
}
|
||||
|
||||
total++;
|
||||
|
||||
if (m) {
|
||||
error('\n Test number: ' + total + ' failed');
|
||||
error(m);
|
||||
error(' r: ' + r);
|
||||
error(' r.c: ' + r.c);
|
||||
error(' r.e: ' + r.e);
|
||||
error(' r.s: ' + r.s);
|
||||
error(' dp: ' + dp);
|
||||
m = null;
|
||||
} else {
|
||||
passed++;
|
||||
}
|
||||
}
|
||||
|
||||
assertException(function () { Decimal.random(Infinity) }, 'Infinity');
|
||||
|
||||
@@ -15,7 +15,7 @@ var count = (function toFixed(Decimal) {
|
||||
str.replace('\n', '<br>') + '</div>' };
|
||||
}
|
||||
|
||||
if (!Decimal && typeof require === 'function') Decimal = require('../decimal');
|
||||
if (!Decimal && typeof require === 'function') Decimal = require('../decimal.js');
|
||||
|
||||
function assert(expected, actual) {
|
||||
total++;
|
||||
@@ -1051,11 +1051,8 @@ var count = (function toFixed(Decimal) {
|
||||
T('123.45000000000', '12.345e1', '1.1e1');
|
||||
|
||||
T('123.5', '12.345e1', 1);
|
||||
T('120', '12.345e1', '-1');
|
||||
T('0', '12.345e1', -23);
|
||||
T('123.45', '12.345e1', '-1');
|
||||
T('123.45', '12.345e1', 1e9 + 1);
|
||||
T('123', '12.345e1', '-0.01');
|
||||
T('123', '12.345e1', '-1e-1');
|
||||
T('123.45', '12.345e1', Infinity);
|
||||
T('123.45', '12.345e1', '-Infinity');
|
||||
|
||||
@@ -1070,7 +1067,7 @@ var count = (function toFixed(Decimal) {
|
||||
T('1', 0.55, 0);
|
||||
T('1', 0.56, 0);
|
||||
T('-1', -0.54, 0);
|
||||
T('-0', -0.5, 0); // test no. 1307
|
||||
T('-0', -0.5, 0);
|
||||
T('-1', -0.56, 0);
|
||||
T('-0.5', -0.5, 1);
|
||||
T('1.3', 1.25, 1);
|
||||
@@ -1119,27 +1116,6 @@ var count = (function toFixed(Decimal) {
|
||||
T('-1.00000000000000000', '-1.000000000000000000005', '17')
|
||||
T('-1.00000000000000000001', '-1.000000000000000000005', '20')
|
||||
|
||||
Decimal.rounding = 4;
|
||||
|
||||
T('1000', 999.5, -1);
|
||||
T('1000', 999.5, -2);
|
||||
T('1000', 999.5, -3);
|
||||
T('0', 999.5, -4);
|
||||
T('0', 999.5, -5);
|
||||
|
||||
T('0', 0.000123456, -1);
|
||||
T('9870', 9870.000123456, -1);
|
||||
T('9900', 9870.000123456, -2);
|
||||
T('10000', 9870.000123456, -3);
|
||||
|
||||
Decimal.rounding = 1;
|
||||
|
||||
T('990', 999.5, -1);
|
||||
T('900', 999.5, -2);
|
||||
T('0', 999.5, -3);
|
||||
T('0', 999.5, -4);
|
||||
T('0', 999.5, -5);
|
||||
|
||||
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
|
||||
return [passed, total];
|
||||
})(this.Decimal);
|
||||
|
||||
Reference in New Issue
Block a user