Add calculate_digits.py and use Decimal instead of float
This commit is contained in:
parent
3d77dd0a79
commit
0372ca7030
3 changed files with 57 additions and 17 deletions
37
calculate_digits.py
Normal file
37
calculate_digits.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import sys
|
||||
from decimal import Decimal
|
||||
|
||||
from physics import calculate_collision
|
||||
|
||||
if len(sys.argv) != 2 or not sys.argv[1].isnumeric() or int(sys.argv[1]) < 0:
|
||||
print(f"Usage: {sys.argv[0]} <digits>")
|
||||
exit()
|
||||
|
||||
m1: int = 1
|
||||
m2: int = 100 ** int(sys.argv[1])
|
||||
v1: Decimal = Decimal(0)
|
||||
v2: Decimal = Decimal(-1)
|
||||
x1: Decimal = Decimal(100)
|
||||
x2: Decimal = Decimal(200)
|
||||
w: int = 20
|
||||
collisions: int = 0
|
||||
wall: bool = False
|
||||
|
||||
# simulate until blocks cannot collide
|
||||
while not (0 <= v1 <= v2):
|
||||
if wall:
|
||||
# left block collides with wall
|
||||
time_wall_collision: float = x1 / -v1
|
||||
x1 += time_wall_collision * v1
|
||||
x2 += time_wall_collision * v2
|
||||
v1 *= -1
|
||||
else:
|
||||
# blocks collide with each other
|
||||
time_block_collision: float = (w + x1 - x2) / (v2 - v1)
|
||||
x1 += time_block_collision * v1
|
||||
x2 += time_block_collision * v2
|
||||
v1, v2 = calculate_collision(m1, v1, m2, v2)
|
||||
collisions += 1
|
||||
wall = not wall
|
||||
|
||||
print(collisions)
|
17
physics.py
17
physics.py
|
@ -1,7 +1,8 @@
|
|||
from decimal import Decimal
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def calculate_collision(m1: float, v1: float, m2: float, v2: float) -> Tuple[float, float]:
|
||||
def calculate_collision(m1: int, v1: Decimal, m2: int, v2: Decimal) -> Tuple[Decimal, Decimal]:
|
||||
"""
|
||||
Calculates the new velocities after the two blocks collide.
|
||||
|
||||
|
@ -12,11 +13,11 @@ def calculate_collision(m1: float, v1: float, m2: float, v2: float) -> Tuple[flo
|
|||
:return: The two new velocities
|
||||
"""
|
||||
|
||||
mv: float = m1 * v1 + m2 * v2
|
||||
s: float = m1 + m2
|
||||
b: float = mv / s
|
||||
d: float = (b ** 2 - (mv ** 2 - (m1 * v1 ** 2 + m2 * v2 ** 2) * m2) / (m1 * s)) ** .5
|
||||
new_v1: float = max([b - d, b + d], key=lambda a: abs(a - v1))
|
||||
d: float = (b ** 2 - (mv ** 2 - (m1 * v1 ** 2 + m2 * v2 ** 2) * m1) / (m2 * s)) ** .5
|
||||
new_v2: float = max([b - d, b + d], key=lambda a: abs(a - v2))
|
||||
mv: Decimal = m1 * v1 + m2 * v2
|
||||
s: Decimal = m1 + m2
|
||||
b: Decimal = mv / s
|
||||
d: Decimal = (b ** 2 - (mv ** 2 - (m1 * v1 ** 2 + m2 * v2 ** 2) * m2) / (m1 * s)).sqrt()
|
||||
new_v1: Decimal = max([b - d, b + d], key=lambda a: abs(a - v1))
|
||||
d: Decimal = (b ** 2 - (mv ** 2 - (m1 * v1 ** 2 + m2 * v2 ** 2) * m1) / (m2 * s)).sqrt()
|
||||
new_v2: Decimal = max([b - d, b + d], key=lambda a: abs(a - v2))
|
||||
return new_v1, new_v2
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from PyQt5.QtCore import *
|
||||
from PyQt5.QtGui import *
|
||||
from PyQt5.QtWidgets import *
|
||||
from decimal import Decimal
|
||||
|
||||
from PyQt5.QtCore import Qt, QBasicTimer, QTimerEvent
|
||||
from PyQt5.QtGui import QPainter, QPaintEvent, QKeyEvent, QPen, QColor, QFont, QFontMetrics
|
||||
from PyQt5.QtWidgets import QWidget, QApplication
|
||||
|
||||
from physics import calculate_collision
|
||||
|
||||
|
@ -12,14 +14,14 @@ class Window(QWidget):
|
|||
self.setWindowTitle("Pi Collisions")
|
||||
self.setFixedSize(640, 480)
|
||||
|
||||
self.m1: float = 1
|
||||
self.v1: float = 0
|
||||
self.x1: float = 150
|
||||
self.m1: int = 1
|
||||
self.v1: Decimal = Decimal(0)
|
||||
self.x1: Decimal = Decimal(150)
|
||||
self.s1: int = 50
|
||||
|
||||
self.m2: float = 100
|
||||
self.v2: float = -1
|
||||
self.x2: float = 300
|
||||
self.m2: int = 100 ** 1
|
||||
self.v2: Decimal = Decimal(-1)
|
||||
self.x2: Decimal = Decimal(300)
|
||||
self.s2: int = 100
|
||||
|
||||
self.timer = QBasicTimer()
|
||||
|
|
Reference in a new issue