A community-maintained Python framework for creating mathematical animations.
Determine decimal places default by step size (#1667)
Ryan McCauley9a2bbc52485d
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.pyindex 4fa5e365b..5f5671fcd 100644--- a/manim/mobject/coordinate_systems.py+++ b/manim/mobject/coordinate_systems.py@@ -71,7 +71,7 @@ class CoordinateSystem:):self.dimension = dimension- default_step = 1.0+ default_step = 1if x_range is None:x_range = [round(-config["frame_x_radius"]),diff --git a/manim/mobject/number_line.py b/manim/mobject/number_line.pyindex 07a8886d7..396a4b90c 100644--- a/manim/mobject/number_line.py+++ b/manim/mobject/number_line.py@@ -53,7 +53,8 @@ class NumberLine(Line):tip_height : :class:`float`The height of the tip.include_numbers : :class:`bool`- Determines whether numbers are added to tick marks.+ Determines whether numbers are added to tick marks. The number of decimal places is determined+ by the step size, this default can be overridden by ``decimal_number_config``.label_direction : Union[:class:`list`, :class:`numpy.ndarray`]The specific position to which number mobjects are added on the line.line_to_number_buff : :class:`float`@@ -104,11 +105,6 @@ class NumberLine(Line):**kwargs):# avoid mutable arguments in defaults- if decimal_number_config is None:- decimal_number_config = {- "num_decimal_places": 0,- "font_size": 24,- } # font_size does nothingif numbers_to_exclude is None:numbers_to_exclude = []if numbers_with_elongated_ticks is None:@@ -118,13 +114,19 @@ class NumberLine(Line):x_range = [round(-config["frame_x_radius"]),round(config["frame_x_radius"]),- 1.0,+ 1,]elif len(x_range) == 2:# adds x_step if not specified. not sure how to feel about this. a user can't know default without peeking at source codex_range = [*x_range, 1]self.x_min, self.x_max, self.x_step = x_range+ if decimal_number_config is None:+ decimal_number_config = {+ "num_decimal_places": self.decimal_places_from_step(),+ "font_size": 24,+ } # font_size does nothing+self.length = lengthself.unit_size = unit_size# ticks@@ -300,6 +302,12 @@ class NumberLine(Line):self.numbers = numbersreturn numbers+ def decimal_places_from_step(self):+ step_as_str = str(self.x_step)+ if "." not in step_as_str:+ return 0+ return len(step_as_str.split(".")[-1])+class UnitInterval(NumberLine):def __init__(diff --git a/tests/test_number_line.py b/tests/test_number_line.pyindex 5ff3223e0..f633850d1 100644--- a/tests/test_number_line.py+++ b/tests/test_number_line.py@@ -10,3 +10,43 @@ def test_unit_vector():axis2 = NumberLine(width=12, x_min=-2, x_max=5)for axis in (axis1, axis2):assert np.linalg.norm(axis.get_unit_vector()) == axis.unit_size+++def test_decimal_determined_by_step():+ """Checks that step size is considered when determining the number of decimal+ places."""+ axis = NumberLine(x_range=[-2, 2, 0.5])+ expected_decimal_places = 1+ actual_decimal_places = axis.decimal_number_config["num_decimal_places"]+ assert actual_decimal_places == expected_decimal_places, (+ "Expected 1 decimal place but got " + actual_decimal_places+ )++ axis2 = NumberLine(x_range=[-1, 1, 0.25])+ expected_decimal_places = 2+ actual_decimal_places = axis2.decimal_number_config["num_decimal_places"]+ assert actual_decimal_places == expected_decimal_places, (+ "Expected 1 decimal place but got " + actual_decimal_places+ )+++def test_decimal_config_overrides_defaults():+ """Checks that ``num_decimal_places`` is determined by step size and gets overridden by ``decimal_number_config``."""+ axis = NumberLine(+ x_range=[-2, 2, 0.5], decimal_number_config={"num_decimal_places": 0}+ )+ expected_decimal_places = 0+ actual_decimal_places = axis.decimal_number_config["num_decimal_places"]+ assert actual_decimal_places == expected_decimal_places, (+ "Expected 1 decimal place but got " + actual_decimal_places+ )+++def test_whole_numbers_step_size_default_to_0_decimal_places():+ """Checks that ``num_decimal_places`` defaults to 0 when a whole number step size is passed."""+ axis = NumberLine(x_range=[-2, 2, 1])+ expected_decimal_places = 0+ actual_decimal_places = axis.decimal_number_config["num_decimal_places"]+ assert actual_decimal_places == expected_decimal_places, (+ "Expected 1 decimal place but got " + actual_decimal_places+ )