mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Create real_and_reactive_power.py (#8665)
This commit is contained in:
parent
54dedf844a
commit
2b051a2de4
49
electronics/real_and_reactive_power.py
Normal file
49
electronics/real_and_reactive_power.py
Normal file
|
@ -0,0 +1,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()
|
Loading…
Reference in New Issue
Block a user