From 6593dcb31600239f96a93f330ba18a1253210705 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 13 Aug 2025 17:54:56 +0200 Subject: [PATCH] fix: properly handle divide by zero in electric utils MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/ElectricUtils.ts | 9 +++++++++ 1 file changed, 9 insertions(+) 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 -- 2.43.0