Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create real_and_reactive_power.py #8665

Merged
merged 7 commits into from
Apr 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions electronics/real_and_reactive_power.py
Original file line number Diff line number Diff line change
@@ -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()