-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathmass_energy_equivalence.py
41 lines (34 loc) · 1.23 KB
/
mass_energy_equivalence.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
"""
Mass-energy equivalence
Reference: https://en.wikipedia.org/wiki/Mass%E2%80%93energy_equivalence
Here it defines a Function to calculate energy(in joules) equivalent of a given
mass(in kilograms) using Einstein's Mass-energy equivalence equation E=mc^2
where m is mass and c = 299,792,458 meter per second is the speed of light
in vacuum (a constant value).
The function takes a single argument mass(in kg) having datatype float and
returns the energy(in joule) having datatype float
"""
SPEED_OF_LIGHT = 299792458
def calculate_energy(mass: float) -> float:
"""
get energy equivalent of given mass in joules:
>>> calculate_energy(1.2)
1.0785062144841811e+17
>>> calculate_energy(3.0)
2.6962655362104528e+17
>>> calculate_energy(5)
449377589368408820
>>> calculate_energy(-5)
Traceback (most recent call last):
...
raise TypeError("Mass cannot be negative")
TypeError: Mass cannot be negative
"""
if mass < 0:
raise TypeError("Mass cannot be negative")
energy = mass * SPEED_OF_LIGHT**2
return energy
if __name__ == "__main__":
mass = float(input("Enter mass (in kilograms): "))
energy = calculate_energy(mass)
print(f"Energy equivalent: {energy} joules")