forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
real_and_reactive_power.py
49 lines (40 loc) · 1.18 KB
/
real_and_reactive_power.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import math
def real_power(apparent_power: float, power_factor: float) -> float:
"""
Calculate real power from apparent power and power factor.
Examples:
>>> real_power(100, 0.9)
90.0
>>> real_power(0, 0.8)
0.0
>>> real_power(100, -0.9)
-90.0
"""
if (
not isinstance(power_factor, (int, float))
or power_factor < -1
or power_factor > 1
):
raise ValueError("power_factor must be a valid float value between -1 and 1.")
return apparent_power * power_factor
def reactive_power(apparent_power: float, power_factor: float) -> float:
"""
Calculate reactive power from apparent power and power factor.
Examples:
>>> reactive_power(100, 0.9)
43.58898943540673
>>> reactive_power(0, 0.8)
0.0
>>> reactive_power(100, -0.9)
43.58898943540673
"""
if (
not isinstance(power_factor, (int, float))
or power_factor < -1
or power_factor > 1
):
raise ValueError("power_factor must be a valid float value between -1 and 1.")
return apparent_power * math.sqrt(1 - power_factor**2)
if __name__ == "__main__":
import doctest
doctest.testmod()