Merge pull request #2465 from kittycat2002/dev

Optimized favor calculations.
This commit is contained in:
hydroflame 2022-01-08 14:22:52 -05:00 committed by GitHub
commit bc1c5346f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,21 +1,13 @@
// The initial formulas was sum 0 to f of 500*1.02^f. // The initial formulas was sum 0 to f of 500*1.02^f.
// These formulas were derived on wolfram alpha. // see https://en.wikipedia.org/wiki/Geometric_series#Closed-form_formula
// for information on how to calculate this
// Wolfram Alpha: sum from 0 to n of 500*1.02^n
// 500 * ((pow(51, f+1)) / pow(50,f) - 50)
// Then we use https://herbie.uwplse.org/demo/ to simplify it and prevent
// Infinity issues.
export function favorToRep(f: number): number { export function favorToRep(f: number): number {
function fma(a: number, b: number, c: number): number { const raw = 25000 * (Math.pow(1.02, f) - 1);
return a * b + c;
}
const ex = fma(f - 1, Math.log(51.0) - Math.log(50.0), Math.log(51.0));
const raw = fma(500.0, Math.exp(ex), -25000.0);
return Math.round(raw * 10000) / 10000; // round to make things easier. return Math.round(raw * 10000) / 10000; // round to make things easier.
} }
// Wolfram Alpha: 500 (50^(-n) 51^(n + 1) - 50) solve for n
export function repToFavor(r: number): number { export function repToFavor(r: number): number {
const raw = Math.log((r + 25000) / 25500) / Math.log(1.02) + 1; const raw = Math.log(r / 25000 + 1) / Math.log(1.02);
return Math.round(raw * 10000) / 10000; // round to make things easier. return Math.round(raw * 10000) / 10000; // round to make things easier.
} }