From 448fae3c4fcbb2c81aea60e915861b0ca8055497 Mon Sep 17 00:00:00 2001 From: mpy-dev Date: Thu, 4 Nov 2021 19:07:27 +0100 Subject: [PATCH 1/3] New function atan2() added. --- README.md | 84 +++++++++++++++++++----------------- mpy_decimal/mpy_decimal.py | 41 +++++++++++++++++- tests/perf_decimal_number.py | 9 ++++ tests/perf_pi_pico.txt | 76 ++++++++++++++++---------------- tests/test_decimal_number.py | 39 ++++++++++++++++- 5 files changed, 170 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index fa7c05c..da8c4c3 100644 --- a/README.md +++ b/README.md @@ -31,55 +31,57 @@ All the internal operations of **DecimalNumber** are done with integers (*int* b +---------------------------------------------------------------+ Scale (max. decimals): 16 Iterations per test: 1000 - Number 1: 676800226403580.0763143540562581 - Number 2: -588054433.4243183156555517 - Addition (n1 + n2): 1.566 ms - Subtraction (n1 - n2): 1.658 ms - Multiplication (n1 * n2): 1.008 ms - Division (n1 / n2): 1.165 ms - Square root abs(n1): 3.894 ms - Power: (pi/2) ** 15 9.742 ms - DecimalNumber from int: 0.373 ms - DecimalNumber from string: 3.771 ms + Number 1: 63107666423864.1503618336011148 + Number 2: 21513455188640.8462728528253754 + Addition (n1 + n2): 1.564 ms + Subtraction (n1 - n2): 1.695 ms + Multiplication (n1 * n2): 0.988 ms + Division (n1 / n2): 1.184 ms + Square root abs(n1): 3.984 ms + Power: (pi/2) ** 15 9.799 ms + DecimalNumber from int: 0.378 ms + DecimalNumber from string: 3.685 ms Iterations per test: 10 - Sine: sin(0.54321) 86.1 ms - Cosine: cos(0.54321) 85.4 ms - Tangent: tan(0.54321) 206.2 ms - Arcsine: asin(0.54321) 267.6 ms - Arccosine: acos(0.65432) 466.6 ms - Arctangent: atan(1.2345) 482.9 ms - Exponential: exp(12.345) 150.3 ms - Natural logarithm: ln(12.345) 150.3 ms + Sine: sin(0.54321) 85.80001 ms + Cosine: cos(0.54321) 86.7 ms + Tangent: tan(0.54321) 206.7 ms + Arcsine: asin(0.54321) 270.2 ms + Arccosine: acos(0.65432) 468.8 ms + Arctangent: atan(1.2345) 485.5 ms + Arctangent2: atan2(1.23, 2.34) 349.2 ms + Exponential: exp(12.345) 151.0 ms + Natural logarithm: ln(12.345) 152.0 ms +---------------------------------------------------------------+ | PERFORMANCE WITH SCALE = 50 | +---------------------------------------------------------------+ Scale (max. decimals): 50 Iterations per test: 400 - Number 1: -52120532222624450128770268842457267852564.2827656475718343863261636466317618740063535442755 - Number 2: 45521155711104405467504347326435032.84784860134353338157872524608016421466524813041101 - Addition (n1 + n2): 2.065 ms - Subtraction (n1 - n2): 2.1925 ms - Multiplication (n1 * n2): 1.55 ms - Division (n1 / n2): 1.45 ms - Square root abs(n1): 11.87 ms - Power: (pi/2) ** 15 11.69 ms - DecimalNumber from int: 0.3825 ms - DecimalNumber from string: 10.64 ms + Number 1: 1008440840554324243744283306032711702.13787157234850455642441824006520576852374144010113 + Number 2: -603053031456028063831871487068.81514266625502376325215658564112814751837118860547 + Addition (n1 + n2): 1.9575 ms + Subtraction (n1 - n2): 2.25 ms + Multiplication (n1 * n2): 1.4625 ms + Division (n1 / n2): 1.52 ms + Square root abs(n1): 12.455 ms + Power: (pi/2) ** 15 11.7425 ms + DecimalNumber from int: 0.385 ms + DecimalNumber from string: 10.055 ms Iterations per test: 4 - Sine: sin(0.54321) 183.25 ms - Cosine: cos(0.54321) 185.5 ms - Tangent: tan(0.54321) 397.25 ms - Arcsine: asin(0.54321) 826.4999 ms - Arccosine: acos(0.65432) 1314.5 ms - Arctangent: atan(1.2345) 1368.75 ms - Exponential: exp(12.345) 280.25 ms - Natural logarithm: ln(12.345) 280.0 ms + Sine: sin(0.54321) 186.25 ms + Cosine: cos(0.54321) 184.5 ms + Tangent: tan(0.54321) 398.75 ms + Arcsine: asin(0.54321) 831.25 ms + Arccosine: acos(0.65432) 1322.25 ms + Arctangent: atan(1.2345) 1377.75 ms + Arctangent2: atan2(1.23, 2.34) 893.4999 ms + Exponential: exp(12.345) 281.75 ms + Natural logarithm: ln(12.345) 281.75 ms +---------------------------------------------------------------+ | CALCULATING PI | +---------------------------------------------------------------+ - Pi with 300 decimals: 5.838 s + Pi with 300 decimals: 5.862 s 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274 ## How to use @@ -330,7 +332,9 @@ The argument of trigonometric functions sin(), cos() and tan() is an angle expre **atan()**: arctangent. -The functions asin(), acos() and atan() return an angle in radians. +**atan2()**: 2-argument arctangent. + +The functions asin(), acos(), atan() and atan2() return an angle in radians. Example: @@ -344,6 +348,8 @@ n.tan() # 0.8985257469396026 n.asin() # 0.821252884452186 n.acos() # 0.7495434423427106 n.atan() # 0.6318812315412357 +n2 = DecimalNumber("1.732") +DecimalNumber.atan(n, n2) # 0.3998638956924461 ``` ### Other methods ### @@ -437,7 +443,7 @@ This module defines four exceptions: * **DecimalNumberExceptionBadInit**: this exception is raised when a negative number of decimals is provided when initializing a **DecimalNumber**. -* **DecimalNumberExceptionMathDomainError**: this exception occurs when trying to calculate the square root of a negative number. +* **DecimalNumberExceptionMathDomainError**: this exception occurs when trying to calculate the square root of a negative number or atan2(0, 0). * **DecimalNumberExceptionDivisionByZeroError**: this is the division by zero exception. diff --git a/mpy_decimal/mpy_decimal.py b/mpy_decimal/mpy_decimal.py index e4bce83..d427d6b 100644 --- a/mpy_decimal/mpy_decimal.py +++ b/mpy_decimal/mpy_decimal.py @@ -458,6 +458,45 @@ def atan(self) -> "DecimalNumber": DecimalNumber.set_scale(scale) return +a + @staticmethod + def atan2(y: "DecimalNumber", x: "DecimalNumber") -> "DecimalNumber": + """Calculates atan2(y, x), 2-argument arctangent + It uses: + if x > 0: atan(y/x) + if x < 0: + if y >= 0: atan(y/x) + pi + if y < 0: atan(y/x) - pi + if x = 0: + if y > 0: +pi/2 + if y < 0: -pi/2 + if y = 0: undefined + """ + if isinstance(y, int): + y = DecimalNumber(y) + if isinstance(x, int): + x = DecimalNumber(x) + + scale: int = DecimalNumber.get_scale() + DecimalNumber.set_scale(DecimalNumber.get_scale() + 4) # extra digits for intermediate steps + r = DecimalNumber() + if x == 0: + if y == 0: + raise DecimalNumberExceptionMathDomainError( + "Undefined value for atan2(0, 0)") + elif y > 0: + r = (DecimalNumber.pi() / 2) + else: + r = (-DecimalNumber.pi() / 2) + else: + r = (y / x).atan() + if x < 0: + if y >= 0: + r += DecimalNumber.pi() + else: + r -= DecimalNumber.pi() + + DecimalNumber.set_scale(scale) + return +r @staticmethod def version() -> str: @@ -619,11 +658,11 @@ def square_root(self) -> "DecimalNumber": It converts the DecimalNumber to an integer (without decimals), calculates its square root using _isqrt() and then it sets the decimals. """ - n = DecimalNumber() if not self._is_positive: raise DecimalNumberExceptionMathDomainError( "No square root for negative numbers") + n = DecimalNumber() num_integer: int = self._number num_integer *= (10 ** (DecimalNumber.get_scale() * 2)) additional_decimals: int = 0 diff --git a/tests/perf_decimal_number.py b/tests/perf_decimal_number.py index cc574bc..afdfcee 100644 --- a/tests/perf_decimal_number.py +++ b/tests/perf_decimal_number.py @@ -187,6 +187,15 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: t = get_time_ms() - t print("{:<30}".format("Arctangent: atan(" + str(n) + ")"), t / limit2, "ms") + # 2-argument arctangent + n = DecimalNumber("1.23") + n2 = DecimalNumber("2.34") + t = get_time_ms() + for _ in range(0, limit2): + n3 = DecimalNumber.atan2(n, n2) + t = get_time_ms() - t + print("{:<30}".format("Arctangent2: atan2(" + str(n) + ", " + str(n2) + ")"), t / limit2, "ms") + # Exponential n = DecimalNumber("12.345") t = get_time_ms() diff --git a/tests/perf_pi_pico.txt b/tests/perf_pi_pico.txt index b32e22d..f89e0d6 100644 --- a/tests/perf_pi_pico.txt +++ b/tests/perf_pi_pico.txt @@ -11,53 +11,55 @@ CPU frequency: 125 Mhz +---------------------------------------------------------------+ Scale (max. decimals): 16 Iterations per test: 1000 -Number 1: 676800226403580.0763143540562581 -Number 2: -588054433.4243183156555517 -Addition (n1 + n2): 1.566 ms -Subtraction (n1 - n2): 1.658 ms -Multiplication (n1 * n2): 1.008 ms -Division (n1 / n2): 1.165 ms -Square root abs(n1): 3.894 ms -Power: (pi/2) ** 15 9.742 ms -DecimalNumber from int: 0.373 ms -DecimalNumber from string: 3.771 ms +Number 1: 63107666423864.1503618336011148 +Number 2: 21513455188640.8462728528253754 +Addition (n1 + n2): 1.564 ms +Subtraction (n1 - n2): 1.695 ms +Multiplication (n1 * n2): 0.988 ms +Division (n1 / n2): 1.184 ms +Square root abs(n1): 3.984 ms +Power: (pi/2) ** 15 9.799 ms +DecimalNumber from int: 0.378 ms +DecimalNumber from string: 3.685 ms Iterations per test: 10 -Sine: sin(0.54321) 86.1 ms -Cosine: cos(0.54321) 85.4 ms -Tangent: tan(0.54321) 206.2 ms -Arcsine: asin(0.54321) 267.6 ms -Arccosine: acos(0.65432) 466.6 ms -Arctangent: atan(1.2345) 482.9 ms -Exponential: exp(12.345) 150.3 ms -Natural logarithm: ln(12.345) 150.3 ms +Sine: sin(0.54321) 85.80001 ms +Cosine: cos(0.54321) 86.7 ms +Tangent: tan(0.54321) 206.7 ms +Arcsine: asin(0.54321) 270.2 ms +Arccosine: acos(0.65432) 468.8 ms +Arctangent: atan(1.2345) 485.5 ms +Arctangent2: atan2(1.23, 2.34) 349.2 ms +Exponential: exp(12.345) 151.0 ms +Natural logarithm: ln(12.345) 152.0 ms +---------------------------------------------------------------+ | PERFORMANCE WITH SCALE = 50 | +---------------------------------------------------------------+ Scale (max. decimals): 50 Iterations per test: 400 -Number 1: -52120532222624450128770268842457267852564.2827656475718343863261636466317618740063535442755 -Number 2: 45521155711104405467504347326435032.84784860134353338157872524608016421466524813041101 -Addition (n1 + n2): 2.065 ms -Subtraction (n1 - n2): 2.1925 ms -Multiplication (n1 * n2): 1.55 ms -Division (n1 / n2): 1.45 ms -Square root abs(n1): 11.87 ms -Power: (pi/2) ** 15 11.69 ms -DecimalNumber from int: 0.3825 ms -DecimalNumber from string: 10.64 ms +Number 1: 1008440840554324243744283306032711702.13787157234850455642441824006520576852374144010113 +Number 2: -603053031456028063831871487068.81514266625502376325215658564112814751837118860547 +Addition (n1 + n2): 1.9575 ms +Subtraction (n1 - n2): 2.25 ms +Multiplication (n1 * n2): 1.4625 ms +Division (n1 / n2): 1.52 ms +Square root abs(n1): 12.455 ms +Power: (pi/2) ** 15 11.7425 ms +DecimalNumber from int: 0.385 ms +DecimalNumber from string: 10.055 ms Iterations per test: 4 -Sine: sin(0.54321) 183.25 ms -Cosine: cos(0.54321) 185.5 ms -Tangent: tan(0.54321) 397.25 ms -Arcsine: asin(0.54321) 826.4999 ms -Arccosine: acos(0.65432) 1314.5 ms -Arctangent: atan(1.2345) 1368.75 ms -Exponential: exp(12.345) 280.25 ms -Natural logarithm: ln(12.345) 280.0 ms +Sine: sin(0.54321) 186.25 ms +Cosine: cos(0.54321) 184.5 ms +Tangent: tan(0.54321) 398.75 ms +Arcsine: asin(0.54321) 831.25 ms +Arccosine: acos(0.65432) 1322.25 ms +Arctangent: atan(1.2345) 1377.75 ms +Arctangent2: atan2(1.23, 2.34) 893.4999 ms +Exponential: exp(12.345) 281.75 ms +Natural logarithm: ln(12.345) 281.75 ms +---------------------------------------------------------------+ | CALCULATING PI | +---------------------------------------------------------------+ -Pi with 300 decimals: 5.838 s +Pi with 300 decimals: 5.862 s 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274 diff --git a/tests/test_decimal_number.py b/tests/test_decimal_number.py index 259032d..3758167 100644 --- a/tests/test_decimal_number.py +++ b/tests/test_decimal_number.py @@ -1301,7 +1301,7 @@ def test_acos(self) -> bool: def test_atan(self) -> bool: """Tests atan() - It tests a list of numbers calculating acos(number) + It tests a list of numbers calculating atan(number) and checking the expected result. """ self.test_counter += 1 @@ -1335,12 +1335,47 @@ def test_atan(self) -> bool: DecimalNumber.set_scale(50) for n in list_numbers: a = str(DecimalNumber(n[0]).atan()) - if not self.assertEqual(a, n[1], "Error calculating acos({0})".format(n[0])): + if not self.assertEqual(a, n[1], "Error calculating atan({0})".format(n[0])): failed = True DecimalNumber.set_scale(current_scale) return failed + def test_atan2(self) -> bool: + """Tests atan2() + It tests a list of numbers calculating atan(number) + and checking the expected result. + """ + self.test_counter += 1 + failed: bool = False + + list_numbers = [ + ('0', '1', '0'), + ('1', '1', '0.785398163397448309615660845819875721049292349843776455243736148076954101571552249657008706335529267'), + ('1', '0', '1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412671058534'), + ('1', '-1', '2.356194490192344928846982537459627163147877049531329365731208444230862304714656748971026119006587801'), + ('0', '-1', '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068'), + ('-1', '-1', '-2.356194490192344928846982537459627163147877049531329365731208444230862304714656748971026119006587801'), + ('-1', '0', '-1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412671058534'), + ('-1', '1', '-0.785398163397448309615660845819875721049292349843776455243736148076954101571552249657008706335529267'), + ('12345678.90123456789', '23456789.0123456789', '0.4844779448504728440723160650537673828414181888506758249301810902601712799253653679799329328565153514') + ] + + current_scale: int = DecimalNumber.get_scale() + DecimalNumber.set_scale(100) + for n in list_numbers: + y = DecimalNumber(n[0]) + x = DecimalNumber(n[1]) + a = str(DecimalNumber.atan2(y, x)) + if not self.assertEqual(a, n[2], "Error calculating atan2({0}, {1})".format(n[0], n[1])): + failed = True + + if not self.assertRaises(DecimalNumberExceptionMathDomainError, lambda: DecimalNumber.atan2(0, 0)): + failed = True + + DecimalNumber.set_scale(current_scale) + + return failed # def print_exception(exc: Exception) -> None: From 972d4fb86be1903da26f4de148f28f0632fb5fff Mon Sep 17 00:00:00 2001 From: mpy-dev Date: Wed, 5 Jan 2022 20:43:10 +0100 Subject: [PATCH 2/3] Test added --- tests/perf_decimal_number.py | 64 ++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/perf_decimal_number.py b/tests/perf_decimal_number.py index afdfcee..5a57f40 100644 --- a/tests/perf_decimal_number.py +++ b/tests/perf_decimal_number.py @@ -16,22 +16,21 @@ iteration_limit2: int = 400 pi_decimals: int = 300 -line: str = '+' + ('-' * 63) + '+' - +format_str: str = "{:<36}" def system_machine_info() -> None: """It prints system information.""" - print("{:<30}".format("Implementation name:"), sys.implementation.name) - print("{:<30}".format("Implementation version:"), + print(format_str.format("Implementation name:"), sys.implementation.name) + print(format_str.format("Implementation version:"), "{0}.{1}.{2}".format( sys.implementation.version[0], sys.implementation.version[1], sys.implementation.version[2] ) ) - print("{:<30}".format("Implementation platform:"), sys.platform) + print(format_str.format("Implementation platform:"), sys.platform) if sys.implementation.name == "micropython": - print("{:<30}".format("CPU frequency:"), + print(format_str.format("CPU frequency:"), machine.freq() // 1000000, "Mhz") def get_time_ms() -> int: @@ -61,44 +60,44 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: global iteration_limit """Performance calculations of DecimalNumber class""" - print("{:<30}".format("Scale (max. decimals):"), DecimalNumber.get_scale()) - print("{:<30}".format("Iterations per test:"), limit1) + print(format_str.format("Scale (max. decimals):"), DecimalNumber.get_scale()) + print(format_str.format("Iterations per test:"), limit1) n1 = gen_random_number() zero: bool = True while zero: n2 = gen_random_number() zero = (n2 == DecimalNumber(0)) - print("{:<30}".format("Number 1:"), n1) - print("{:<30}".format("Number 2:"), n2) + print(format_str.format("Number 1:"), n1) + print(format_str.format("Number 2:"), n2) # Addition t = get_time_ms() for _ in range(0, limit1): n3 = n1 + n2 t = get_time_ms() - t - print("{:<30}".format("Addition (n1 + n2):"), t / limit1, "ms") + print(format_str.format("Addition (n1 + n2):"), t / limit1, "ms") # Subtraction t = get_time_ms() for _ in range(0, limit1): n3 = n1 - n2 t = get_time_ms() - t - print("{:<30}".format("Subtraction (n1 - n2):"), t / limit1, "ms") + print(format_str.format("Subtraction (n1 - n2):"), t / limit1, "ms") # Multiplication t = get_time_ms() for _ in range(0, limit1): n3 = n1 * n2 t = get_time_ms() - t - print("{:<30}".format("Multiplication (n1 * n2):"), t / limit1, "ms") + print(format_str.format("Multiplication (n1 * n2):"), t / limit1, "ms") # Division t = get_time_ms() for _ in range(0, limit1): n3 = n1 / n2 t = get_time_ms() - t - print("{:<30}".format("Division (n1 / n2):"), t / limit1, "ms") + print(format_str.format("Division (n1 / n2):"), t / limit1, "ms") # Square root n = abs(n1) @@ -106,7 +105,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit1): n3 = n.square_root() t = get_time_ms() - t - print("{:<30}".format("Square root abs(n1):"), t / limit1, "ms") + print(format_str.format("Square root abs(n1):"), t / limit1, "ms") # Power n = DecimalNumber.pi() / 2 @@ -115,7 +114,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit1): n3 = n ** e t = get_time_ms() - t - print("{:<30}".format("Power: (pi/2) ** 15"), t / limit1, "ms") + print(format_str.format("Power: (pi/2) ** 15"), t / limit1, "ms") # Creation from integer n = n1._number @@ -124,7 +123,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit1): n3 = DecimalNumber(n, d) t = get_time_ms() - t - print("{:<30}".format("DecimalNumber from int:"), t / limit1, "ms") + print(format_str.format("DecimalNumber from int:"), t / limit1, "ms") # Creation from string n = str(n1) @@ -132,11 +131,11 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit1): n3 = DecimalNumber(n) t = get_time_ms() - t - print("{:<30}".format("DecimalNumber from string:"), t / limit1, "ms") + print(format_str.format("DecimalNumber from string:"), t / limit1, "ms") # From this point, the iterations are reduced - print("{:<30}".format("Iterations per test:"), limit2) + print(format_str.format("Iterations per test:"), limit2) # Sine n = DecimalNumber("0.54321") @@ -144,7 +143,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.sin() t = get_time_ms() - t - print("{:<30}".format("Sine: sin(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Sine: sin(" + str(n) + ")"), t / limit2, "ms") # Cosine n = DecimalNumber("0.54321") @@ -152,7 +151,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.sin() t = get_time_ms() - t - print("{:<30}".format("Cosine: cos(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Cosine: cos(" + str(n) + ")"), t / limit2, "ms") # Tangent n = DecimalNumber("0.54321") @@ -160,7 +159,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.tan() t = get_time_ms() - t - print("{:<30}".format("Tangent: tan(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Tangent: tan(" + str(n) + ")"), t / limit2, "ms") # Arcsine @@ -169,7 +168,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.asin() t = get_time_ms() - t - print("{:<30}".format("Arcsine: asin(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Arcsine: asin(" + str(n) + ")"), t / limit2, "ms") # Arccosine n = DecimalNumber("0.65432") @@ -177,7 +176,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.acos() t = get_time_ms() - t - print("{:<30}".format("Arccosine: acos(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Arccosine: acos(" + str(n) + ")"), t / limit2, "ms") # Arctangent n = DecimalNumber("1.2345") @@ -185,16 +184,16 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.atan() t = get_time_ms() - t - print("{:<30}".format("Arctangent: atan(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Arctangent: atan(" + str(n) + ")"), t / limit2, "ms") # 2-argument arctangent - n = DecimalNumber("1.23") - n2 = DecimalNumber("2.34") + n = DecimalNumber("2.3456") + n2 = DecimalNumber("1.2334") t = get_time_ms() for _ in range(0, limit2): n3 = DecimalNumber.atan2(n, n2) t = get_time_ms() - t - print("{:<30}".format("Arctangent2: atan2(" + str(n) + ", " + str(n2) + ")"), t / limit2, "ms") + print(format_str.format("Arctangent2: atan2(" + str(n) + ", " + str(n2) + ")"), t / limit2, "ms") # Exponential n = DecimalNumber("12.345") @@ -202,7 +201,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.exp() t = get_time_ms() - t - print("{:<30}".format("Exponential: exp(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Exponential: exp(" + str(n) + ")"), t / limit2, "ms") # Natural logarithm n = DecimalNumber("12.345") @@ -210,7 +209,7 @@ def perf_decimal_number(limit1: int, limit2: int) -> None: for _ in range(0, limit2): n3 = n.exp() t = get_time_ms() - t - print("{:<30}".format("Natural logarithm: ln(" + str(n) + ")"), t / limit2, "ms") + print(format_str.format("Natural logarithm: ln(" + str(n) + ")"), t / limit2, "ms") def perf_decimal_number_pi() -> None: """Performance of the calculation of PI.""" @@ -224,15 +223,16 @@ def perf_decimal_number_pi() -> None: t = get_time_ms() pi = DecimalNumber.pi() t = get_time_ms() - t - print("{:<30}".format("Pi with " + str(pi_decimals) + " decimals:"), t/1000, "s") + print(format_str.format("Pi with " + str(pi_decimals) + " decimals:"), t/1000, "s") print(pi) DecimalNumber.set_scale(current_scale) def print_title(title: str) -> None: """Auxiliary function to print a title.""" + line: str = '+' + ('-' * 73) + '+' print("") print(line) - print("| " + "{:<59}".format(title) + " |") + print("| " + "{:<69}".format(title) + " |") print(line) From 15e1216e19bda5aca0456485b2b7ccc317ee2795 Mon Sep 17 00:00:00 2001 From: mpy-dev Date: Wed, 8 Jun 2022 22:19:55 +0200 Subject: [PATCH 3/3] Change in testing --- tests/test_decimal_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_decimal_number.py b/tests/test_decimal_number.py index 3758167..646eeaa 100644 --- a/tests/test_decimal_number.py +++ b/tests/test_decimal_number.py @@ -1400,7 +1400,7 @@ def test_atan2(self) -> bool: if failed: failed_counter += 1 - print("Tests ran:", test.test_counter) + print("Tests run:", test.test_counter) if failed_counter == 0: print("Result:", "OK") elif failed_counter == 1: