From bc941df22bfc295d6845f522e122027bef7c09b9 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Thu, 9 Feb 2023 06:57:36 -0600 Subject: [PATCH] Updated formula typing for non-invertible params in invertible formulas --- src/game/formulas.ts | 465 +++++++++++++++++++----------------- tests/game/formulas.test.ts | 7 +- 2 files changed, 258 insertions(+), 214 deletions(-) diff --git a/src/game/formulas.ts b/src/game/formulas.ts index a3a692c..10b3fcd 100644 --- a/src/game/formulas.ts +++ b/src/game/formulas.ts @@ -6,7 +6,6 @@ import { computed, ComputedRef, ref, Ref, unref } from "vue"; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type GenericFormula = Formula; export type FormulaSource = ProcessedComputable | GenericFormula; -export type InvertibleFormulaSource = ProcessedComputable | InvertibleFormula; export type InvertibleFormula = GenericFormula & { invert: (value: DecimalSource) => DecimalSource; }; @@ -938,7 +937,7 @@ export default class Formula { * @param value The constant value for this formula. */ public static constant( - value: InvertibleFormulaSource + value: ProcessedComputable ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula { return new Formula({ inputs: [value] }) as InvertibleFormula; } @@ -1045,7 +1044,7 @@ export default class Formula { return new Formula({ inputs: [value], evaluate: Decimal.abs }); } - public static neg(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static neg(value: T): Omit; public static neg(value: FormulaSource): GenericFormula; public static neg(value: FormulaSource) { return new Formula({ @@ -1055,9 +1054,13 @@ export default class Formula { integrate: integrateNeg }); } + public static negate(value: T): Omit; + public static negate(value: FormulaSource): GenericFormula; public static negate(value: FormulaSource) { return Formula.neg(value); } + public static negated(value: T): Omit; + public static negated(value: FormulaSource): GenericFormula; public static negated(value: FormulaSource) { return Formula.neg(value); } @@ -1085,10 +1088,8 @@ export default class Formula { return new Formula({ inputs: [value], evaluate: Decimal.trunc }); } - public static add( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static add(value: T, other: FormulaSource): T; + public static add(value: FormulaSource, other: T): T; public static add(value: FormulaSource, other: FormulaSource): GenericFormula; public static add(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1099,14 +1100,15 @@ export default class Formula { invertIntegral: invertIntegrateAdd }); } + public static plus(value: T, other: FormulaSource): T; + public static plus(value: FormulaSource, other: T): T; + public static plus(value: FormulaSource, other: FormulaSource): GenericFormula; public static plus(value: FormulaSource, other: FormulaSource) { return Formula.add(value, other); } - public static sub( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static sub(value: T, other: FormulaSource): T; + public static sub(value: FormulaSource, other: T): T; public static sub(value: FormulaSource, other: FormulaSource): GenericFormula; public static sub(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1117,17 +1119,21 @@ export default class Formula { invertIntegral: invertIntegrateSub }); } + public static subtract(value: T, other: FormulaSource): T; + public static subtract(value: FormulaSource, other: T): T; + public static subtract(value: FormulaSource, other: FormulaSource): GenericFormula; public static subtract(value: FormulaSource, other: FormulaSource) { return Formula.sub(value, other); } + public static minus(value: T, other: FormulaSource): T; + public static minus(value: FormulaSource, other: T): T; + public static minus(value: FormulaSource, other: FormulaSource): GenericFormula; public static minus(value: FormulaSource, other: FormulaSource) { return Formula.sub(value, other); } - public static mul( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static mul(value: T, other: FormulaSource): T; + public static mul(value: FormulaSource, other: T): T; public static mul(value: FormulaSource, other: FormulaSource): GenericFormula; public static mul(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1138,17 +1144,21 @@ export default class Formula { invertIntegral: invertIntegrateMul }); } + public static multiply(value: T, other: FormulaSource): T; + public static multiply(value: FormulaSource, other: T): T; + public static multiply(value: FormulaSource, other: FormulaSource): GenericFormula; public static multiply(value: FormulaSource, other: FormulaSource) { return Formula.mul(value, other); } + public static times(value: T, other: FormulaSource): T; + public static times(value: FormulaSource, other: T): T; + public static times(value: FormulaSource, other: FormulaSource): GenericFormula; public static times(value: FormulaSource, other: FormulaSource) { return Formula.mul(value, other); } - public static div( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static div(value: T, other: FormulaSource): T; + public static div(value: FormulaSource, other: T): T; public static div(value: FormulaSource, other: FormulaSource): GenericFormula; public static div(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1159,13 +1169,14 @@ export default class Formula { invertIntegral: invertIntegrateDiv }); } + public static divide(value: T, other: FormulaSource): T; + public static divide(value: FormulaSource, other: T): T; + public static divide(value: FormulaSource, other: FormulaSource): GenericFormula; public static divide(value: FormulaSource, other: FormulaSource) { return Formula.div(value, other); } - public static recip( - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static recip(value: T): T; public static recip(value: FormulaSource): GenericFormula; public static recip(value: FormulaSource) { return new Formula({ @@ -1176,9 +1187,13 @@ export default class Formula { invertIntegral: invertIntegrateRecip }); } + public static reciprocal(value: T): T; + public static reciprocal(value: FormulaSource): GenericFormula; public static reciprocal(value: FormulaSource): GenericFormula { return Formula.recip(value); } + public static reciprocate(value: T): T; + public static reciprocate(value: FormulaSource): GenericFormula; public static reciprocate(value: FormulaSource) { return Formula.recip(value); } @@ -1215,21 +1230,15 @@ export default class Formula { return new Formula({ inputs: [value, max], evaluate: Decimal.clampMax }); } - public static pLog10(value: InvertibleFormulaSource): InvertibleFormula; - public static pLog10(value: FormulaSource): GenericFormula; - public static pLog10(value: FormulaSource) { + public static pLog10(value: FormulaSource): GenericFormula { return new Formula({ inputs: [value], evaluate: Decimal.pLog10 }); } - public static absLog10(value: InvertibleFormulaSource): InvertibleFormula; - public static absLog10(value: FormulaSource): GenericFormula; - public static absLog10(value: FormulaSource) { + public static absLog10(value: FormulaSource): GenericFormula { return new Formula({ inputs: [value], evaluate: Decimal.absLog10 }); } - public static log10( - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static log10(value: T): T; public static log10(value: FormulaSource): GenericFormula; public static log10(value: FormulaSource) { return new Formula({ @@ -1241,10 +1250,8 @@ export default class Formula { }); } - public static log( - value: InvertibleFormulaSource, - base: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static log(value: T, base: FormulaSource): T; + public static log(value: FormulaSource, base: T): T; public static log(value: FormulaSource, base: FormulaSource): GenericFormula; public static log(value: FormulaSource, base: FormulaSource) { return new Formula({ @@ -1255,13 +1262,14 @@ export default class Formula { invertIntegral: invertIntegrateLog }); } + public static logarithm(value: T, base: FormulaSource): T; + public static logarithm(value: FormulaSource, base: T): T; + public static logarithm(value: FormulaSource, base: FormulaSource): GenericFormula; public static logarithm(value: FormulaSource, base: FormulaSource) { return Formula.log(value, base); } - public static log2( - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static log2(value: T): T; public static log2(value: FormulaSource): GenericFormula; public static log2(value: FormulaSource) { return new Formula({ @@ -1273,9 +1281,7 @@ export default class Formula { }); } - public static ln( - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static ln(value: T): T; public static ln(value: FormulaSource): GenericFormula; public static ln(value: FormulaSource) { return new Formula({ @@ -1287,10 +1293,8 @@ export default class Formula { }); } - public static pow( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static pow(value: T, other: FormulaSource): T; + public static pow(value: FormulaSource, other: T): T; public static pow(value: FormulaSource, other: FormulaSource): GenericFormula; public static pow(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1302,9 +1306,7 @@ export default class Formula { }); } - public static pow10( - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static pow10(value: T): T; public static pow10(value: FormulaSource): GenericFormula; public static pow10(value: FormulaSource) { return new Formula({ @@ -1316,10 +1318,8 @@ export default class Formula { }); } - public static pow_base( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static pow_base(value: T, other: FormulaSource): T; + public static pow_base(value: FormulaSource, other: T): T; public static pow_base(value: FormulaSource, other: FormulaSource): GenericFormula; public static pow_base(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1331,10 +1331,8 @@ export default class Formula { }); } - public static root( - value: InvertibleFormulaSource, - other: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public static root(value: T, other: FormulaSource): T; + public static root(value: FormulaSource, other: T): T; public static root(value: FormulaSource, other: FormulaSource): GenericFormula; public static root(value: FormulaSource, other: FormulaSource) { return new Formula({ @@ -1358,7 +1356,7 @@ export default class Formula { return new Formula({ inputs: [value], evaluate: Decimal.lngamma }); } - public static exp(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static exp(value: T): Omit; public static exp(value: FormulaSource): GenericFormula; public static exp(value: FormulaSource) { return new Formula({ @@ -1369,27 +1367,35 @@ export default class Formula { }); } + public static sqr(value: T): T; + public static sqr(value: FormulaSource): GenericFormula; public static sqr(value: FormulaSource) { return Formula.pow(value, 2); } + public static sqrt(value: T): T; + public static sqrt(value: FormulaSource): GenericFormula; public static sqrt(value: FormulaSource) { return Formula.root(value, 2); } + public static cube(value: T): T; + public static cube(value: FormulaSource): GenericFormula; public static cube(value: FormulaSource) { return Formula.pow(value, 3); } + public static cbrt(value: T): T; + public static cbrt(value: FormulaSource): GenericFormula; public static cbrt(value: FormulaSource) { return Formula.root(value, 3); } - public static tetrate( - value: InvertibleFormulaSource, - height?: InvertibleFormulaSource, - payload?: InvertibleFormulaSource - ): InvertibleFormula; + public static tetrate( + value: T, + height?: FormulaSource, + payload?: FormulaSource + ): Omit; public static tetrate( value: FormulaSource, height?: FormulaSource, @@ -1407,11 +1413,11 @@ export default class Formula { }); } - public static iteratedexp( - value: InvertibleFormulaSource, - height?: InvertibleFormulaSource, - payload?: InvertibleFormulaSource - ): InvertibleFormula; + public static iteratedexp( + value: T, + height?: FormulaSource, + payload?: FormulaSource + ): Omit; public static iteratedexp( value: FormulaSource, height?: FormulaSource, @@ -1437,24 +1443,24 @@ export default class Formula { return new Formula({ inputs: [value, base, times], evaluate: iteratedLog }); } - public static slog( - value: InvertibleFormulaSource, - base?: InvertibleFormulaSource - ): InvertibleFormula; + public static slog( + value: T, + base?: FormulaSource + ): Omit; public static slog(value: FormulaSource, base?: FormulaSource): GenericFormula; public static slog(value: FormulaSource, base: FormulaSource = 10) { return new Formula({ inputs: [value, base], evaluate: slog, invert: invertSlog }); } - public static layeradd10(value: FormulaSource, diff: FormulaSource): GenericFormula { + public static layeradd10(value: FormulaSource, diff: FormulaSource) { return new Formula({ inputs: [value, diff], evaluate: Decimal.layeradd10 }); } - public static layeradd( - value: InvertibleFormulaSource, - diff: InvertibleFormulaSource, - base?: InvertibleFormulaSource - ): InvertibleFormula; + public static layeradd( + value: T, + diff: FormulaSource, + base?: FormulaSource + ): Omit; public static layeradd( value: FormulaSource, diff: FormulaSource, @@ -1468,13 +1474,17 @@ export default class Formula { }); } - public static lambertw(value: InvertibleFormulaSource): InvertibleFormula; + public static lambertw( + value: T + ): Omit; public static lambertw(value: FormulaSource): GenericFormula; public static lambertw(value: FormulaSource) { return new Formula({ inputs: [value], evaluate: Decimal.lambertw, invert: invertLambertw }); } - public static ssqrt(value: InvertibleFormulaSource): InvertibleFormula; + public static ssqrt( + value: T + ): Omit; public static ssqrt(value: FormulaSource): GenericFormula; public static ssqrt(value: FormulaSource) { return new Formula({ inputs: [value], evaluate: Decimal.ssqrt, invert: invertSsqrt }); @@ -1484,11 +1494,11 @@ export default class Formula { value: FormulaSource, height: FormulaSource = 2, payload: FormulaSource = Decimal.fromComponents_noNormalize(1, 0, 1) - ) { + ): GenericFormula { return new Formula({ inputs: [value, height, payload], evaluate: pentate }); } - public static sin(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static sin(value: T): Omit; public static sin(value: FormulaSource): GenericFormula; public static sin(value: FormulaSource) { return new Formula({ @@ -1499,7 +1509,7 @@ export default class Formula { }); } - public static cos(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static cos(value: T): Omit; public static cos(value: FormulaSource): GenericFormula; public static cos(value: FormulaSource) { return new Formula({ @@ -1510,7 +1520,7 @@ export default class Formula { }); } - public static tan(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static tan(value: T): Omit; public static tan(value: FormulaSource): GenericFormula; public static tan(value: FormulaSource) { return new Formula({ @@ -1521,7 +1531,7 @@ export default class Formula { }); } - public static asin(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static asin(value: T): Omit; public static asin(value: FormulaSource): GenericFormula; public static asin(value: FormulaSource) { return new Formula({ @@ -1532,7 +1542,7 @@ export default class Formula { }); } - public static acos(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static acos(value: T): Omit; public static acos(value: FormulaSource): GenericFormula; public static acos(value: FormulaSource) { return new Formula({ @@ -1543,7 +1553,7 @@ export default class Formula { }); } - public static atan(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static atan(value: T): Omit; public static atan(value: FormulaSource): GenericFormula; public static atan(value: FormulaSource) { return new Formula({ @@ -1554,7 +1564,7 @@ export default class Formula { }); } - public static sinh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static sinh(value: T): Omit; public static sinh(value: FormulaSource): GenericFormula; public static sinh(value: FormulaSource) { return new Formula({ @@ -1565,7 +1575,7 @@ export default class Formula { }); } - public static cosh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static cosh(value: T): Omit; public static cosh(value: FormulaSource): GenericFormula; public static cosh(value: FormulaSource) { return new Formula({ @@ -1576,7 +1586,7 @@ export default class Formula { }); } - public static tanh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static tanh(value: T): Omit; public static tanh(value: FormulaSource): GenericFormula; public static tanh(value: FormulaSource) { return new Formula({ @@ -1587,7 +1597,7 @@ export default class Formula { }); } - public static asinh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static asinh(value: T): Omit; public static asinh(value: FormulaSource): GenericFormula; public static asinh(value: FormulaSource) { return new Formula({ @@ -1598,7 +1608,7 @@ export default class Formula { }); } - public static acosh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static acosh(value: T): Omit; public static acosh(value: FormulaSource): GenericFormula; public static acosh(value: FormulaSource) { return new Formula({ @@ -1609,7 +1619,7 @@ export default class Formula { }); } - public static atanh(value: InvertibleFormulaSource): InvertibleFormula & IntegrableFormula; + public static atanh(value: T): Omit; public static atanh(value: FormulaSource): GenericFormula; public static atanh(value: FormulaSource) { return new Formula({ @@ -1644,13 +1654,19 @@ export default class Formula { return Formula.abs(this); } - public neg() { + public neg(this: T): Omit; + public neg(this: GenericFormula): GenericFormula; + public neg(this: GenericFormula) { return Formula.neg(this); } - public negate() { + public negate(this: T): Omit; + public negate(this: GenericFormula): GenericFormula; + public negate(this: GenericFormula) { return Formula.neg(this); } - public negated() { + public negated(this: T): Omit; + public negated(this: GenericFormula): GenericFormula; + public negated(this: GenericFormula) { return Formula.neg(this); } @@ -1677,112 +1693,94 @@ export default class Formula { return Formula.trunc(this); } - public add( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public add(this: FormulaSource, value: FormulaSource): GenericFormula; - public add(value: FormulaSource) { + public add(this: T, value: FormulaSource): T; + public add(this: GenericFormula, value: T): T; + public add(this: GenericFormula, value: FormulaSource): GenericFormula; + public add(this: GenericFormula, value: FormulaSource) { return Formula.add(this, value); } - public plus( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public plus(this: FormulaSource, value: FormulaSource): GenericFormula; + public plus(this: T, value: FormulaSource): T; + public plus(this: GenericFormula, value: T): T; + public plus(this: GenericFormula, value: FormulaSource): GenericFormula; public plus(value: FormulaSource) { return Formula.add(this, value); } - public sub( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public sub(this: FormulaSource, value: FormulaSource): GenericFormula; + public sub(this: T, value: FormulaSource): T; + public sub(this: GenericFormula, value: T): T; + public sub(this: GenericFormula, value: FormulaSource): GenericFormula; public sub(value: FormulaSource) { return Formula.sub(this, value); } - public subtract( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public subtract(this: FormulaSource, value: FormulaSource): GenericFormula; + public subtract(this: T, value: FormulaSource): T; + public subtract(this: GenericFormula, value: T): T; + public subtract(this: GenericFormula, value: FormulaSource): GenericFormula; public subtract(value: FormulaSource) { return Formula.sub(this, value); } - public minus( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public minus(this: FormulaSource, value: FormulaSource): GenericFormula; + public minus(this: T, value: FormulaSource): T; + public minus(this: GenericFormula, value: T): T; + public minus(this: GenericFormula, value: FormulaSource): GenericFormula; public minus(value: FormulaSource) { return Formula.sub(this, value); } - public mul( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public mul(this: FormulaSource, value: FormulaSource): GenericFormula; + public mul(this: T, value: FormulaSource): T; + public mul(this: GenericFormula, value: T): T; + public mul(this: GenericFormula, value: FormulaSource): GenericFormula; public mul(value: FormulaSource) { return Formula.mul(this, value); } - public multiply( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public multiply(this: FormulaSource, value: FormulaSource): GenericFormula; + public multiply(this: T, value: FormulaSource): T; + public multiply(this: GenericFormula, value: T): T; + public multiply(this: GenericFormula, value: FormulaSource): GenericFormula; public multiply(value: FormulaSource) { return Formula.mul(this, value); } - public times( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public times(this: FormulaSource, value: FormulaSource): GenericFormula; + public times(this: T, value: FormulaSource): T; + public times(this: GenericFormula, value: T): T; + public times(this: GenericFormula, value: FormulaSource): GenericFormula; public times(value: FormulaSource) { return Formula.mul(this, value); } - public div( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public div(this: FormulaSource, value: FormulaSource): GenericFormula; + public div(this: T, value: FormulaSource): T; + public div(this: GenericFormula, value: T): T; + public div(this: GenericFormula, value: FormulaSource): GenericFormula; public div(value: FormulaSource) { return Formula.div(this, value); } - public divide( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public divide(this: FormulaSource, value: FormulaSource): GenericFormula; + public divide(this: T, value: FormulaSource): T; + public divide(this: GenericFormula, value: T): T; + public divide(this: GenericFormula, value: FormulaSource): GenericFormula; public divide(value: FormulaSource) { return Formula.div(this, value); } - public divideBy( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public divideBy(this: FormulaSource, value: FormulaSource): GenericFormula; + public divideBy(this: T, value: FormulaSource): T; + public divideBy(this: GenericFormula, value: T): T; + public divideBy(this: GenericFormula, value: FormulaSource): GenericFormula; public divideBy(value: FormulaSource) { return Formula.div(this, value); } - public dividedBy( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; - public dividedBy(this: FormulaSource, value: FormulaSource): GenericFormula; + public dividedBy(this: T, value: FormulaSource): T; + public dividedBy(this: GenericFormula, value: T): T; + public dividedBy(this: GenericFormula, value: FormulaSource): GenericFormula; public dividedBy(value: FormulaSource) { return Formula.div(this, value); } + public recip(this: T): T; + public recip(this: FormulaSource): GenericFormula; public recip() { return Formula.recip(this); } + public reciprocal(this: T): T; + public reciprocal(this: FormulaSource): GenericFormula; public reciprocal() { return Formula.recip(this); } + public reciprocate(this: T): T; + public reciprocate(this: FormulaSource): GenericFormula; public reciprocate() { return Formula.recip(this); } @@ -1823,61 +1821,59 @@ export default class Formula { return Formula.absLog10(this); } + public log10(this: T): T; + public log10(this: FormulaSource): GenericFormula; public log10() { return Formula.log10(this); } - public log( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public log(this: T, value: FormulaSource): T; + public log(this: FormulaSource, value: T): T; public log(this: FormulaSource, value: FormulaSource): GenericFormula; public log(value: FormulaSource) { return Formula.log(this, value); } - public logarithm( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public logarithm(this: T, value: FormulaSource): T; + public logarithm(this: FormulaSource, value: T): T; public logarithm(this: FormulaSource, value: FormulaSource): GenericFormula; public logarithm(value: FormulaSource) { return Formula.log(this, value); } + public log2(this: T): T; + public log2(this: FormulaSource): GenericFormula; public log2() { return Formula.log2(this); } + public ln(this: T): T; + public ln(this: FormulaSource): GenericFormula; public ln() { return Formula.ln(this); } - public pow( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public pow(this: T, value: FormulaSource): T; + public pow(this: FormulaSource, value: T): T; public pow(this: FormulaSource, value: FormulaSource): GenericFormula; public pow(value: FormulaSource) { return Formula.pow(this, value); } + public pow10(this: T): T; + public pow10(this: FormulaSource): GenericFormula; public pow10() { return Formula.pow10(this); } - public pow_base( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public pow_base(this: T, value: FormulaSource): T; + public pow_base(this: FormulaSource, value: T): T; public pow_base(this: FormulaSource, value: FormulaSource): GenericFormula; public pow_base(value: FormulaSource) { return Formula.pow_base(this, value); } - public root( - this: InvertibleFormulaSource, - value: InvertibleFormulaSource - ): InvertibleFormula & IntegrableFormula & InvertibleIntegralFormula; + public root(this: T, value: FormulaSource): T; + public root(this: FormulaSource, value: T): T; public root(this: FormulaSource, value: FormulaSource): GenericFormula; public root(value: FormulaSource) { return Formula.root(this, value); @@ -1894,53 +1890,65 @@ export default class Formula { return Formula.lngamma(this); } - public exp() { + public exp(this: T): Omit; + public exp(this: FormulaSource): GenericFormula; + public exp(this: FormulaSource) { return Formula.exp(this); } + public sqr(this: T): T; + public sqr(this: FormulaSource): GenericFormula; public sqr() { return Formula.pow(this, 2); } + public sqrt(this: T): T; + public sqrt(this: FormulaSource): GenericFormula; public sqrt() { return Formula.root(this, 2); } + public cube(this: T): T; + public cube(this: FormulaSource): GenericFormula; public cube() { return Formula.pow(this, 3); } + public cbrt(this: T): T; + public cbrt(this: FormulaSource): GenericFormula; public cbrt() { return Formula.root(this, 3); } - public tetrate( - this: InvertibleFormulaSource, - height: InvertibleFormulaSource, - payload: InvertibleFormulaSource - ): InvertibleFormula; + public tetrate( + this: T, + height?: FormulaSource, + payload?: FormulaSource + ): Omit; public tetrate( this: FormulaSource, - height: FormulaSource, - payload: FormulaSource + height?: FormulaSource, + payload?: FormulaSource ): GenericFormula; public tetrate( + this: FormulaSource, height: FormulaSource = 2, payload: FormulaSource = Decimal.fromComponents_noNormalize(1, 0, 1) ) { return Formula.tetrate(this, height, payload); } - public iteratedexp( - this: InvertibleFormulaSource, - height: InvertibleFormulaSource, - payload: InvertibleFormulaSource - ): InvertibleFormula; + public iteratedexp( + this: T, + height?: FormulaSource, + payload?: FormulaSource + ): Omit; public iteratedexp( this: FormulaSource, - height: FormulaSource, - payload: FormulaSource + height?: FormulaSource, + payload?: FormulaSource ): GenericFormula; public iteratedexp( + this: FormulaSource, height: FormulaSource = 2, payload: FormulaSource = Decimal.fromComponents_noNormalize(1, 0, 1) ) { @@ -1951,9 +1959,12 @@ export default class Formula { return Formula.iteratedlog(this, base, times); } - public slog(this: InvertibleFormulaSource, base?: InvertibleFormulaSource): InvertibleFormula; + public slog( + this: T, + base?: FormulaSource + ): Omit; public slog(this: FormulaSource, base?: FormulaSource): GenericFormula; - public slog(base: FormulaSource = 10) { + public slog(this: FormulaSource, base: FormulaSource = 10) { return Formula.slog(this, base); } @@ -1961,21 +1972,25 @@ export default class Formula { return Formula.layeradd10(this, diff); } - public layeradd( - this: InvertibleFormulaSource, - diff: InvertibleFormulaSource, - base?: InvertibleFormulaSource - ): InvertibleFormula; + public layeradd( + this: T, + diff: FormulaSource, + base?: FormulaSource + ): Omit; public layeradd(this: FormulaSource, diff: FormulaSource, base?: FormulaSource): GenericFormula; - public layeradd(diff: FormulaSource, base: FormulaSource) { + public layeradd(this: FormulaSource, diff: FormulaSource, base: FormulaSource) { return Formula.layeradd(this, diff, base); } - public lambertw() { + public lambertw(this: T): Omit; + public lambertw(this: FormulaSource): GenericFormula; + public lambertw(this: FormulaSource) { return Formula.lambertw(this); } - public ssqrt() { + public ssqrt(this: T): Omit; + public ssqrt(this: FormulaSource): GenericFormula; + public ssqrt(this: FormulaSource) { return Formula.ssqrt(this); } @@ -1986,51 +2001,75 @@ export default class Formula { return Formula.pentate(this, height, payload); } - public sin() { + public sin(this: T): Omit; + public sin(this: FormulaSource): GenericFormula; + public sin(this: FormulaSource) { return Formula.sin(this); } - public cos() { + public cos(this: T): Omit; + public cos(this: FormulaSource): GenericFormula; + public cos(this: FormulaSource) { return Formula.cos(this); } - public tan() { + public tan(this: T): Omit; + public tan(this: FormulaSource): GenericFormula; + public tan(this: FormulaSource) { return Formula.tan(this); } - public asin() { + public asin(this: T): Omit; + public asin(this: FormulaSource): GenericFormula; + public asin(this: FormulaSource) { return Formula.asin(this); } - public acos() { + public acos(this: T): Omit; + public acos(this: FormulaSource): GenericFormula; + public acos(this: FormulaSource) { return Formula.acos(this); } - public atan() { + public atan(this: T): Omit; + public atan(this: FormulaSource): GenericFormula; + public atan(this: FormulaSource) { return Formula.atan(this); } - public sinh() { + public sinh(this: T): Omit; + public sinh(this: FormulaSource): GenericFormula; + public sinh(this: FormulaSource) { return Formula.sinh(this); } - public cosh() { + public cosh(this: T): Omit; + public cosh(this: FormulaSource): GenericFormula; + public cosh(this: FormulaSource) { return Formula.cosh(this); } - public tanh() { + public tanh(this: T): Omit; + public tanh(this: FormulaSource): GenericFormula; + public tanh(this: FormulaSource) { return Formula.tanh(this); } - public asinh() { + public asinh(this: T): Omit; + public asinh(this: FormulaSource): GenericFormula; + public asinh(this: FormulaSource) { return Formula.asinh(this); } - public acosh() { + public acosh(this: T): Omit; + public acosh(this: FormulaSource): GenericFormula; + public acosh(this: FormulaSource) { return Formula.acosh(this); } - public atanh() { + public atanh(this: T): Omit; + public atanh(this: FormulaSource): GenericFormula; + public atanh(this: FormulaSource) { return Formula.atanh(this); } } diff --git a/tests/game/formulas.test.ts b/tests/game/formulas.test.ts index fdfa1fd..47f8d26 100644 --- a/tests/game/formulas.test.ts +++ b/tests/game/formulas.test.ts @@ -250,7 +250,6 @@ describe("Creating Formulas", () => { } testConstant("number", () => Formula.constant(10)); testConstant("string", () => Formula.constant("10")); - testConstant("formula", () => Formula.constant(Formula.constant(10))); testConstant("decimal", () => Formula.constant(new Decimal("1e400")), "1e400"); testConstant("ref", () => Formula.constant(ref(10))); }); @@ -532,6 +531,12 @@ describe("Inverting", () => { const formula = Formula.add(variable, constant).times(constant); expect(formula.invert(100)).compare_tolerance(0); }); + + test("Inverting with non-invertible sections", () => { + const formula = Formula.add(variable, constant.ceil()); + expect(formula.isInvertible()).toBe(true); + expect(formula.invert(10)).compare_tolerance(7); + }); }); describe("Integrating", () => {