From 4fd95a322e8f2037b2fcc0d10ddf02e0be807bd3 Mon Sep 17 00:00:00 2001 From: kittycat2002 Date: Sat, 8 Jan 2022 08:14:13 -0500 Subject: [PATCH] Optimized favor calculations. --- src/Faction/formulas/favor.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Faction/formulas/favor.ts b/src/Faction/formulas/favor.ts index 04e615ebf..73790544b 100644 --- a/src/Faction/formulas/favor.ts +++ b/src/Faction/formulas/favor.ts @@ -1,21 +1,13 @@ // 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 { - function fma(a: number, b: number, c: number): number { - 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); + const raw = 25000 * (Math.pow(1.02, f) - 1); 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 { - 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. }