]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix: properly handle divide by zero in electric utils
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 15:54:56 +0000 (17:54 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 15:54:56 +0000 (17:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/utils/ElectricUtils.ts

index 923c6849be00268ad41bf1a88480c95ffef69084..6c3d710312466a8b4a136d5d913d270c9468e170 100644 (file)
@@ -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