fix: Make geom mean robust to low numbers by adding logs rather than multiplying small numbers. fix display of the number 1, which wasn't in any of the cases.

This commit is contained in:
NunoSempere 2022-01-29 17:45:23 -05:00
parent 5ec31fa365
commit fc91cdd2dd
2 changed files with 22 additions and 10 deletions

View File

@ -344,4 +344,3 @@ console.log(JSON.stringify(paths, null, 2))
let distances = findDistance({sourceElementId:2, targetElementId:4, links, nodes})
console.log(distances)
*/

View File

@ -13,7 +13,8 @@ export const truncateValueForDisplay = (value) => {
return Number(Math.round(value).toPrecision(2));
} else if (value > 1) {
return Math.round(value * 10) / 10;
} else if (value < 1) {
} else if (value <= 1) {
return value;
}
};
export const _transformSliderValueToPracticalValue = (value) =>
@ -37,15 +38,19 @@ export function numToAlphabeticalString(num) {
}
export function formatLargeOrSmall(num) {
if (num > 1) {
return toLocale(truncateValueForDisplay(num));
let result;
if (num >= 1) {
result = toLocale(truncateValueForDisplay(num));
} else if (num > 0) {
return num.toFixed(-Math.floor(Math.log(num) / Math.log(10)) + 1);
result = num.toFixed(-Math.floor(Math.log(num) / Math.log(10)) + 1);
} else if (num < -1) {
return num.toFixed(-Math.floor(Math.log(-num) / Math.log(10)) + 1);
result = num.toFixed(-Math.floor(Math.log(-num) / Math.log(10)) + 1);
} else {
return toLocale(num); //return "~0"
result = toLocale(num); //return "~0"
}
console.log(`${num} -> ${result}`);
return result;
}
const firstFewMaxMergeSortSequence = [
0, 0, 1, 3, 5, 8, 11, 14, 17, 21, 25, 29, 33, 37, 41, 45, 49, 54, 59, 64, 69,
@ -86,9 +91,17 @@ export function expectedNumMergeSortSteps(n) {
}
}
export const avg = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
export const geomMean = (arr) =>
arr.reduce((a, b) => a * b, 1) ^ (1 / arr.length);
export const avg = (arr) => sum(arr) / arr.length;
export const geomMean = (arr) => {
let n = arr.length;
let logavg = sum(arr.map((x) => Math.log(x))); // works for low numbers much better
let result = Math.exp(logavg / n);
return result;
};
// export const geomMean = (arr) => arr.reduce((a, b) => a * b, 1); // ^ (1 / arr.length); // didn't work so well for low numbers.
export const increasingList = (n) => Array.from(Array(n).keys());