mathparse 0.2.8__tar.gz → 0.2.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mathparse-0.2.8 → mathparse-0.2.10}/PKG-INFO +1 -1
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse/__init__.py +1 -1
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse/mathparse.py +44 -14
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse.egg-info/PKG-INFO +1 -1
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_complex_statements.py +9 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_unary_operations.py +27 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/LICENSE +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/README.md +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse/mathwords.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse.egg-info/SOURCES.txt +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse.egg-info/dependency_links.txt +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse.egg-info/requires.txt +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/mathparse.egg-info/top_level.txt +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/pyproject.toml +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/setup.cfg +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_binary_operations.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_language_support.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_prefix_unary_operations.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_replace_word_tokens.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_simplified_chinese.py +0 -0
- {mathparse-0.2.8 → mathparse-0.2.10}/tests/test_utils.py +0 -0
|
@@ -83,21 +83,25 @@ def is_word(word: str, language: str) -> bool:
|
|
|
83
83
|
|
|
84
84
|
def to_number(val) -> Union[int, float, str, Decimal]:
|
|
85
85
|
"""
|
|
86
|
-
Convert a
|
|
86
|
+
Convert a value to a Decimal if possible.
|
|
87
87
|
"""
|
|
88
|
-
# If already a number (
|
|
89
|
-
if isinstance(val,
|
|
88
|
+
# If already a number (Decimal), return as-is
|
|
89
|
+
if isinstance(val, Decimal):
|
|
90
90
|
return val
|
|
91
91
|
|
|
92
|
+
# Convert other numbers to Decimal
|
|
93
|
+
if isinstance(val, (int, float)):
|
|
94
|
+
return Decimal(str(val))
|
|
95
|
+
|
|
92
96
|
# Check if it's a constant and convert
|
|
93
97
|
if is_constant(val):
|
|
94
|
-
return mathwords.CONSTANTS[val]
|
|
98
|
+
return Decimal(mathwords.CONSTANTS[val])
|
|
95
99
|
|
|
96
100
|
# Otherwise, try to convert string to number
|
|
97
101
|
if is_int(val):
|
|
98
|
-
return
|
|
102
|
+
return Decimal(val)
|
|
99
103
|
elif is_float(val):
|
|
100
|
-
return
|
|
104
|
+
return Decimal(val)
|
|
101
105
|
|
|
102
106
|
return val
|
|
103
107
|
|
|
@@ -466,17 +470,21 @@ def to_postfix(tokens: list) -> list:
|
|
|
466
470
|
"""
|
|
467
471
|
precedence = {
|
|
468
472
|
'.': 5,
|
|
469
|
-
'
|
|
470
|
-
'
|
|
471
|
-
'
|
|
472
|
-
'
|
|
473
|
-
'
|
|
473
|
+
'^': 4,
|
|
474
|
+
'/': 3,
|
|
475
|
+
'*': 3,
|
|
476
|
+
'+': 2,
|
|
477
|
+
'-': 2,
|
|
474
478
|
'(': 1
|
|
475
479
|
}
|
|
476
480
|
|
|
477
481
|
# Unary functions have a higher precedence than binary operators
|
|
478
482
|
unary_precedence = max(precedence.values()) + 1
|
|
479
483
|
|
|
484
|
+
# Right-associative operators use strict inequality when popping,
|
|
485
|
+
# so that e.g. 2^3^2 evaluates as 2^(3^2) = 512, not (2^3)^2 = 64
|
|
486
|
+
right_associative = {'^'}
|
|
487
|
+
|
|
480
488
|
postfix = []
|
|
481
489
|
opstack = []
|
|
482
490
|
|
|
@@ -497,12 +505,18 @@ def to_postfix(tokens: list) -> list:
|
|
|
497
505
|
postfix.append(top_token)
|
|
498
506
|
top_token = opstack.pop()
|
|
499
507
|
elif is_binary(token):
|
|
500
|
-
#
|
|
501
|
-
#
|
|
508
|
+
# Pop operators with higher precedence, or equal precedence when
|
|
509
|
+
# the current token is left-associative (right-associative
|
|
510
|
+
# operators like ^ only yield to strictly higher precedence so
|
|
511
|
+
# that 2^3^2 = 2^(3^2) = 512 rather than (2^3)^2 = 64).
|
|
512
|
+
is_left_assoc = token not in right_associative
|
|
502
513
|
while (opstack != []) and (
|
|
503
514
|
(
|
|
504
515
|
opstack[-1] in precedence and token in precedence and (
|
|
505
|
-
precedence[opstack[-1]]
|
|
516
|
+
precedence[opstack[-1]] > precedence[token] or (
|
|
517
|
+
is_left_assoc and
|
|
518
|
+
precedence[opstack[-1]] == precedence[token]
|
|
519
|
+
)
|
|
506
520
|
)
|
|
507
521
|
) or
|
|
508
522
|
(
|
|
@@ -612,6 +626,9 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
612
626
|
|
|
613
627
|
result = stack.pop()
|
|
614
628
|
|
|
629
|
+
# Convert final result from string or Decimal to a public Python number
|
|
630
|
+
result = normalize_number(result)
|
|
631
|
+
|
|
615
632
|
# Convert final result from string to number if needed
|
|
616
633
|
if isinstance(result, str):
|
|
617
634
|
if is_int(result):
|
|
@@ -622,6 +639,19 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
622
639
|
return result
|
|
623
640
|
|
|
624
641
|
|
|
642
|
+
def normalize_number(val):
|
|
643
|
+
"""
|
|
644
|
+
Convert Decimal arithmetic results to public int or float values.
|
|
645
|
+
"""
|
|
646
|
+
if not isinstance(val, Decimal):
|
|
647
|
+
return val
|
|
648
|
+
|
|
649
|
+
if val == val.to_integral_value():
|
|
650
|
+
return int(val)
|
|
651
|
+
|
|
652
|
+
return float(val)
|
|
653
|
+
|
|
654
|
+
|
|
625
655
|
def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
626
656
|
"""
|
|
627
657
|
Convert a string into a list of mathematical tokens for processing.
|
|
@@ -78,3 +78,12 @@ class ComplexStatementsTestCase(TestCase):
|
|
|
78
78
|
"""
|
|
79
79
|
result = mathparse.parse('π + 1')
|
|
80
80
|
self.assertAlmostEqual(result, 3.141593 + 1, places=5)
|
|
81
|
+
|
|
82
|
+
def test_with_operations_and_single_numbers(self):
|
|
83
|
+
"""
|
|
84
|
+
Test mixed integer and float operations in a complex expression.
|
|
85
|
+
"""
|
|
86
|
+
result = mathparse.parse(
|
|
87
|
+
"(1 + 3 + 2.0 + 6) * 0.00001"
|
|
88
|
+
)
|
|
89
|
+
self.assertAlmostEqual(result, 0.00012, places=5)
|
|
@@ -10,6 +10,33 @@ class UnaryOperatorTestCase(TestCase):
|
|
|
10
10
|
|
|
11
11
|
self.assertEqual(result, 256)
|
|
12
12
|
|
|
13
|
+
def test_exponent_precedence_over_addition_and_multiplication(self):
|
|
14
|
+
"""
|
|
15
|
+
Exponentiation must bind tighter than + and *.
|
|
16
|
+
2 ^ 3 + 4 * 5 = (2^3) + (4*5) = 8 + 20 = 28
|
|
17
|
+
"""
|
|
18
|
+
result = mathparse.parse('2 ^ 3 + 4 * 5')
|
|
19
|
+
|
|
20
|
+
self.assertEqual(result, 28)
|
|
21
|
+
|
|
22
|
+
def test_exponent_precedence_mixed_expression(self):
|
|
23
|
+
"""
|
|
24
|
+
Exponentiation must bind tighter than * and +.
|
|
25
|
+
2 + 3 * 4 ^ 5 = 2 + 3 * (4^5) = 2 + 3*1024 = 3074
|
|
26
|
+
"""
|
|
27
|
+
result = mathparse.parse('2 + 3 * 4 ^ 5')
|
|
28
|
+
|
|
29
|
+
self.assertEqual(result, 3074)
|
|
30
|
+
|
|
31
|
+
def test_exponent_right_associative(self):
|
|
32
|
+
"""
|
|
33
|
+
Exponentiation is right-associative: 2^3^2 = 2^(3^2) = 512,
|
|
34
|
+
not (2^3)^2 = 64.
|
|
35
|
+
"""
|
|
36
|
+
result = mathparse.parse('2 ^ 3 ^ 2')
|
|
37
|
+
|
|
38
|
+
self.assertEqual(result, 512)
|
|
39
|
+
|
|
13
40
|
def test_without_unary_operator_fre(self):
|
|
14
41
|
result = mathparse.parse('50 * (85 / 100)', language='FRE')
|
|
15
42
|
self.assertEqual(result, 42.5)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|