This commit is contained in:
Brooke Vibber 2023-01-07 17:32:38 -08:00
parent 49831dc048
commit d32b630320

View file

@ -12,8 +12,10 @@ let shift = 4;
let base = 2 ** (powBits - shift); let base = 2 ** (powBits - shift);
// 16-bit lookup table // 16-bit lookup table
let reduction = 0;
// 12-bit lookup table // 12-bit lookup table
let reduction = 4; //let reduction = 4;
function toFixed(float) { function toFixed(float) {
return Math.round(float * base); return Math.round(float * base);
@ -73,21 +75,39 @@ process.exit(1);
*/ */
// now just try multipling numbers // now just try multipling numbers
let deltas = 0;
let count = 0;
let deltaCount = 0;
let results = 0;
function round(n, x) {
return Math.round(x * n) / n;
}
while (true) { while (true) {
let a = Math.trunc(Math.random() * logEntries); let a = Math.trunc(Math.random() * logEntries);
let b = Math.trunc(Math.random() * logEntries); let b = Math.trunc(Math.random() * logEntries);
let expected = a * b;
let result = mul(a, b);
let pExpected = a * b; let delta = Math.abs(result - expected);
let p = mul(a, b);
let delta = Math.abs(p - pExpected);
let epsilon = 1; let epsilon = 1;
if (delta >= epsilon) { if (delta >= epsilon) {
let percent = 100 * (delta / pExpected); let percent = 100 * (delta / expected);
console.log(`${a} * ${b} = ${pExpected}, but got ${p} delta ${delta} ${Math.round(percent * 100) / 100}%`); //console.log(`${a} * ${b} = ${expected}, but got ${result} delta ${delta} ${Math.round(percent * 100) / 100}%`);
deltas += delta;
deltaCount++;
} else { } else {
console.log(`${a} * ${b} = ${p}`); //console.log(`${a} * ${b} = ${result}`);
}
count++;
if (count == 100) {
deltaAvg = deltas / deltaCount;
console.log(`${count - deltaCount} of ${count} ok -- ${deltaCount} off by avg ${round(10,deltaAvg)})`);
count = 0;
deltas = 0;
deltaCount = 0;
} }
} }