Use numpy.testing.assert_almost_equal for floating point comparison

This replaces the previous custom float comparison function with a numpy
function.
This commit is contained in:
hangeer 2026-02-27 20:11:01 -06:00
commit df51916a0a

View file

@ -3,6 +3,8 @@ from __future__ import annotations
from contextlib import redirect_stdout
from io import StringIO
from numpy.testing import assert_almost_equal
from manim.constants import LEFT, RIGHT
from manim.mobject.text.text_mobject import MarkupText, Paragraph, Text
@ -36,28 +38,35 @@ def test_font_warnings():
def test_paragraph_alignment():
def float_eq(a: float, b: float, delta: float = 1e-2):
return abs(a - b) <= delta
# check paragraph left alignment
par_left = Paragraph("This is", "a left-aligned", "paragraph", alignment="left")
assert float_eq(par_left[0][0].get_x(LEFT), par_left[1][0].get_x(LEFT))
assert float_eq(par_left[0][0].get_x(LEFT), par_left[2][0].get_x(LEFT))
assert_almost_equal(
par_left[0][0].get_x(LEFT), par_left[1][0].get_x(LEFT), decimal=2
)
assert_almost_equal(
par_left[0][0].get_x(LEFT), par_left[2][0].get_x(LEFT), decimal=2
)
# check paragraph right alignment
par_right = Paragraph("This is", "a right-aligned", "paragraph", alignment="right")
assert float_eq(par_right[0][-1].get_x(RIGHT), par_right[1][-1].get_x(RIGHT))
assert float_eq(par_right[0][-1].get_x(RIGHT), par_right[2][-1].get_x(RIGHT))
assert_almost_equal(
par_right[0][-1].get_x(RIGHT), par_right[1][-1].get_x(RIGHT), decimal=2
)
assert_almost_equal(
par_right[0][-1].get_x(RIGHT), par_right[2][-1].get_x(RIGHT), decimal=2
)
# check paragraph center alignment
par_center = Paragraph(
"This is", "a center-aligned", "paragraph", alignment="center"
)
assert float_eq(
assert_almost_equal(
(par_center[0][0].get_x(LEFT) + par_center[0][-1].get_x(RIGHT)) / 2,
(par_center[1][0].get_x(LEFT) + par_center[1][-1].get_x(RIGHT)) / 2,
decimal=2,
)
assert float_eq(
assert_almost_equal(
(par_center[0][0].get_x(LEFT) + par_center[0][-1].get_x(RIGHT)) / 2,
(par_center[2][0].get_x(LEFT) + par_center[2][-1].get_x(RIGHT)) / 2,
decimal=2,
)