From: Jérôme Benoit Date: Wed, 13 Aug 2025 15:54:56 +0000 (+0200) Subject: fix: properly handle divide by zero in electric utils X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6593dcb31600239f96a93f330ba18a1253210705;p=e-mobility-charging-stations-simulator.git fix: properly handle divide by zero in electric utils Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/ElectricUtils.ts b/src/utils/ElectricUtils.ts index 923c6849..6c3d7103 100644 --- a/src/utils/ElectricUtils.ts +++ b/src/utils/ElectricUtils.ts @@ -14,6 +14,9 @@ export class ACElectricUtils { } static amperagePerPhaseFromPower (nbOfPhases: number, P: number, V: number, cosPhi = 1): number { + if (nbOfPhases <= 0) { + return 0 + } const amperage = ACElectricUtils.amperageTotalFromPower(P, V, cosPhi) const amperagePerPhase = amperage / nbOfPhases if (amperage % nbOfPhases === 0) { @@ -27,6 +30,9 @@ export class ACElectricUtils { } static amperageTotalFromPower (P: number, V: number, cosPhi = 1): number { + if (V === 0 || cosPhi === 0) { + return 0 + } const amperage = P / (V * cosPhi) if (cosPhi === 1 && P % V === 0) { return amperage @@ -57,6 +63,9 @@ export class DCElectricUtils { } static amperage (P: number, V: number): number { + if (V === 0) { + return 0 + } const amperage = P / V if (P % V === 0) { return amperage