MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / energy_from_mass

Function energy_from_mass

physics/mass_energy_equivalence.py:24–46  ·  view source on GitHub ↗

Calculates the Energy equivalence of the Mass using E = mc² in SI units J from Mass in kg. mass (float): Mass of body. Usage example: >>> energy_from_mass(124.56) 1.11948945063458e+19 >>> energy_from_mass(320) 2.8760165719578165e+19 >>> energy_from_mass(0)

(mass: float)

Source from the content-addressed store, hash-verified

22
23
24def energy_from_mass(mass: float) -> float:
25 """
26 Calculates the Energy equivalence of the Mass using E = mc²
27 in SI units J from Mass in kg.
28
29 mass (float): Mass of body.
30
31 Usage example:
32 >>> energy_from_mass(124.56)
33 1.11948945063458e+19
34 >>> energy_from_mass(320)
35 2.8760165719578165e+19
36 >>> energy_from_mass(0)
37 0.0
38 >>> energy_from_mass(-967.9)
39 Traceback (most recent call last):
40 ...
41 ValueError: Mass can't be negative.
42
43 """
44 if mass < 0:
45 raise ValueError("Mass can't be negative.")
46 return mass * c**2
47
48
49def mass_from_energy(energy: float) -> float:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected