Skip to content

Commit fa3bca6

Browse files
authored
Fixes TheAlgorithms#9191: Add module for calculating energy-mass equivalenc
1 parent fbbbd5d commit fa3bca6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Einstein's Mass-Energy Equivalence
2+
# This code calculates the energy equivalent of a given mass using Einstein's mass-energy equivalence equation, E=mc².
3+
4+
# Source: https://en.wikipedia.org/wiki/Mass%E2%80%93energy_equivalence
5+
6+
def mass_energy_equivalence(mass: float, c: float = 299792458) -> float:
7+
"""
8+
Calculate the energy equivalent of a given mass using Einstein's mass-energy equivalence equation, E=mc².
9+
10+
Args:
11+
mass (float): Mass in kilograms.
12+
c (float): Speed of light in meters per second (default value is the speed of light in a vacuum).
13+
14+
Returns:
15+
float: Energy equivalent in joules.
16+
17+
Examples:
18+
>>> mass_energy_equivalence(1)
19+
8.987551787368176e+16
20+
>>> mass_energy_equivalence(5, 3e8)
21+
1.125937723421022e+18
22+
"""
23+
if mass < 0:
24+
raise ValueError("Mass cannot be negative")
25+
if c <= 0:
26+
raise ValueError("Speed of light must be positive")
27+
28+
energy = mass * c**2
29+
return energy
30+
31+
if __name__ == "__main__":
32+
import doctest
33+
34+
# Run doctests
35+
doctest.testmod()

0 commit comments

Comments
 (0)