forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archimedes_principle.py
49 lines (37 loc) · 1.23 KB
/
archimedes_principle.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
42
43
44
45
46
47
48
49
"""
Calculates buoyant force on object submerged within static fluid.
Discovered by greek mathematician, Archimedes. The principle is named after him.
Equation for calculating buoyant force:
Fb = ρ * V * g
Source:
- https://en.wikipedia.org/wiki/Archimedes%27_principle
"""
# Acceleration Constant on Earth (unit m/s^2)
g = 9.80665
def archimedes_principle(
fluid_density: float, volume: float, gravity: float = g
) -> float:
"""
Args:
fluid_density: density of fluid (kg/m^3)
volume: volume of object / liquid being displaced by object
gravity: Acceleration from gravity. Gravitational force on system,
Default is Earth Gravity
returns:
buoyant force on object in Newtons
>>> archimedes_principle(fluid_density=997, volume=0.5, gravity=9.8)
4885.3
>>> archimedes_principle(fluid_density=997, volume=0.7)
6844.061035
"""
if fluid_density <= 0:
raise ValueError("Impossible fluid density")
if volume < 0:
raise ValueError("Impossible Object volume")
if gravity <= 0:
raise ValueError("Impossible Gravity")
return fluid_density * gravity * volume
if __name__ == "__main__":
import doctest
# run doctest
doctest.testmod()