pow-approx-pow10.js (1535B)
1 if (typeof fdlibm === "undefined") { 2 var fdlibm = SpecialPowers.Cu.getJSTestingFunctions().fdlibm; 3 } 4 5 const f64 = new Float64Array(1); 6 const ui64 = new BigUint64Array(f64.buffer); 7 8 function toBits(n) { 9 f64[0] = n; 10 return ui64[0]; 11 } 12 13 function errorInULP(actual, expected) { 14 // Handle NaN and +0/-0. 15 if (Object.is(actual, expected)) { 16 return 0; 17 } 18 19 let x = toBits(actual); 20 let y = toBits(expected); 21 return x <= y ? Number(y - x) : Number(x - y); 22 } 23 24 const maxExponent = Math.trunc(Math.log10(Number.MAX_VALUE)); 25 const minExponent = Math.trunc(Math.log10(Number.MIN_VALUE)); 26 27 assertEq(Math.pow(10, maxExponent + 1), Infinity); 28 assertEq(Math.pow(10, minExponent - 1), 0); 29 30 // Ensure the error is less than 2 ULP when compared to fdlibm. 31 for (let i = minExponent; i <= maxExponent; ++i) { 32 let actual = Math.pow(10, i); 33 let expected = fdlibm.pow(10, i); 34 let error = errorInULP(actual, expected); 35 36 assertEq(error < 2, true, 37 `${10} ** ${i}: ${actual} (${toBits(actual).toString(16)}) != ${expected} (${toBits(expected).toString(16)})`); 38 } 39 40 // Ensure the error is less than 2 ULP when compared to parsed string |1ep|. 41 for (let i = minExponent; i <= maxExponent; ++i) { 42 let actual = Math.pow(10, i); 43 let expected = Number("1e" + i); 44 let error = errorInULP(actual, expected); 45 46 assertEq(error < 2, true, 47 `${10} ** ${i}: ${actual} (${toBits(actual).toString(16)}) != ${expected} (${toBits(expected).toString(16)})`); 48 } 49 50 if (typeof reportCompare === "function") 51 reportCompare(true, true);