• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if (typeof T === 'undefined') require('../setup');
2
3T('pow against sqrt', function () {
4
5  Decimal.config({
6    toExpNeg: -7,
7    toExpPos: 21,
8    minE: -9e15,
9    maxE: 9e15
10  });
11
12  for (var e, n, p, r, s; total < 10000; ) {
13
14    // Get a random value in the range [0,1) with a random number of significant digits
15    // in the range [1, 40], as a string in exponential format.
16    e = Decimal.random( Math.random() * 40 + 1 | 0 ).toExponential();
17
18    // Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
19    r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
20      ( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
21    //console.log(' r:          ' + r);
22
23    // Random rounding mode.
24    Decimal.rounding = Math.random() * 9 | 0;
25
26    // Random precision in the range [1, 40].
27    Decimal.precision = Math.random() * 40 + 1 | 0;
28
29    p = r.pow(0.5);
30    //console.log(' r.pow(0.5): ' + p);
31
32    // sqrt is much faster than pow(0.5)
33    s = r.sqrt();
34    //console.log(' r.sqrt():   ' + s);
35
36    T.assertEqual(p.valueOf(), s.valueOf());
37  }
38});
39
40