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

Function gravitational_law

physics/newtons_law_of_gravitation.py:28–92  ·  view source on GitHub ↗

Input Parameters ---------------- force : magnitude in Newtons mass_1 : mass in Kilograms mass_2 : mass in Kilograms distance : distance in Meters Returns ------- result : dict name, value pair of the parameter having Zero as it's value Returns the value

(
    force: float, mass_1: float, mass_2: float, distance: float
)

Source from the content-addressed store, hash-verified

26
27
28def gravitational_law(
29 force: float, mass_1: float, mass_2: float, distance: float
30) -> dict[str, float]:
31 """
32 Input Parameters
33 ----------------
34 force : magnitude in Newtons
35
36 mass_1 : mass in Kilograms
37
38 mass_2 : mass in Kilograms
39
40 distance : distance in Meters
41
42 Returns
43 -------
44 result : dict name, value pair of the parameter having Zero as it's value
45
46 Returns the value of one of the parameters specified as 0, provided the values of
47 other parameters are given.
48 >>> gravitational_law(force=0, mass_1=5, mass_2=10, distance=20)
49 {'force': 8.342875e-12}
50
51 >>> gravitational_law(force=7367.382, mass_1=0, mass_2=74, distance=3048)
52 {'mass_1': 1.385816317292268e+19}
53
54 >>> gravitational_law(force=36337.283, mass_1=0, mass_2=0, distance=35584)
55 Traceback (most recent call last):
56 ...
57 ValueError: One and only one argument must be 0
58
59 >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584)
60 Traceback (most recent call last):
61 ...
62 ValueError: Mass can not be negative
63
64 >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)
65 Traceback (most recent call last):
66 ...
67 ValueError: Gravitational force can not be negative
68 """
69
70 product_of_mass = mass_1 * mass_2
71
72 if (force, mass_1, mass_2, distance).count(0) != 1:
73 raise ValueError("One and only one argument must be 0")
74 if force < 0:
75 raise ValueError("Gravitational force can not be negative")
76 if distance < 0:
77 raise ValueError("Distance can not be negative")
78 if mass_1 < 0 or mass_2 < 0:
79 raise ValueError("Mass can not be negative")
80 if force == 0:
81 force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)
82 return {"force": force}
83 elif mass_1 == 0:
84 mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2)
85 return {"mass_1": mass_1}

Callers

nothing calls this directly

Calls 1

countMethod · 0.80

Tested by

no test coverage detected