1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
MikeMcl_decimal.js/test/modules/powSqrt.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-01-25 00:11:32 +00:00
if (typeof T === 'undefined') require('../setup');
2016-02-06 17:51:29 +00:00
T('pow against sqrt', function () {
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
Decimal.config({
toExpNeg: -7,
toExpPos: 21,
minE: -9e15,
maxE: 9e15
});
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
for (var e, n, p, r, s; total < 10000; ) {
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
// Get a random value in the range [0,1) with a random number of significant digits
// in the range [1, 40], as a string in exponential format.
e = Decimal.random( Math.random() * 40 + 1 | 0 ).toExponential();
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
// 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 ? '-' : '' ) +
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
//console.log(' r: ' + r);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
// Random rounding mode.
Decimal.rounding = Math.random() * 9 | 0;
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
// Random precision in the range [1, 40].
Decimal.precision = Math.random() * 40 + 1 | 0;
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
p = r.pow(0.5);
//console.log(' r.pow(0.5): ' + p);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
// sqrt is much faster than pow(0.5)
s = r.sqrt();
//console.log(' r.sqrt(): ' + s);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
T.assertEqual(p.valueOf(), s.valueOf());
}
2016-02-06 17:51:29 +00:00
});
2016-01-25 00:11:32 +00:00