mathparse 0.2.9__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.
Files changed (21) hide show
  1. {mathparse-0.2.9 → mathparse-0.2.10}/PKG-INFO +1 -1
  2. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse/__init__.py +1 -1
  3. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse/mathparse.py +26 -6
  4. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse.egg-info/PKG-INFO +1 -1
  5. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_complex_statements.py +9 -0
  6. {mathparse-0.2.9 → mathparse-0.2.10}/LICENSE +0 -0
  7. {mathparse-0.2.9 → mathparse-0.2.10}/README.md +0 -0
  8. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse/mathwords.py +0 -0
  9. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse.egg-info/SOURCES.txt +0 -0
  10. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse.egg-info/dependency_links.txt +0 -0
  11. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse.egg-info/requires.txt +0 -0
  12. {mathparse-0.2.9 → mathparse-0.2.10}/mathparse.egg-info/top_level.txt +0 -0
  13. {mathparse-0.2.9 → mathparse-0.2.10}/pyproject.toml +0 -0
  14. {mathparse-0.2.9 → mathparse-0.2.10}/setup.cfg +0 -0
  15. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_binary_operations.py +0 -0
  16. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_language_support.py +0 -0
  17. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_prefix_unary_operations.py +0 -0
  18. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_replace_word_tokens.py +0 -0
  19. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_simplified_chinese.py +0 -0
  20. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_unary_operations.py +0 -0
  21. {mathparse-0.2.9 → mathparse-0.2.10}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.9
3
+ Version: 0.2.10
4
4
  Summary: mathparse is a library for solving mathematical equations contained in strings
5
5
  Author: Gunther Cox
6
6
  License-Expression: MIT
@@ -2,4 +2,4 @@
2
2
  mathparse is a library for solving mathematical equations contained in strings
3
3
  """
4
4
 
5
- __version__ = '0.2.9'
5
+ __version__ = '0.2.10'
@@ -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 string to an int or float if possible.
86
+ Convert a value to a Decimal if possible.
87
87
  """
88
- # If already a number (int, float, Decimal), return as-is
89
- if isinstance(val, (int, float, Decimal)):
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 int(val)
102
+ return Decimal(val)
99
103
  elif is_float(val):
100
- return float(val)
104
+ return Decimal(val)
101
105
 
102
106
  return val
103
107
 
@@ -622,6 +626,9 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
622
626
 
623
627
  result = stack.pop()
624
628
 
629
+ # Convert final result from string or Decimal to a public Python number
630
+ result = normalize_number(result)
631
+
625
632
  # Convert final result from string to number if needed
626
633
  if isinstance(result, str):
627
634
  if is_int(result):
@@ -632,6 +639,19 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
632
639
  return result
633
640
 
634
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
+
635
655
  def tokenize(string: str, language: str = None, escape: str = '___') -> list:
636
656
  """
637
657
  Convert a string into a list of mathematical tokens for processing.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.9
3
+ Version: 0.2.10
4
4
  Summary: mathparse is a library for solving mathematical equations contained in strings
5
5
  Author: Gunther Cox
6
6
  License-Expression: MIT
@@ -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)
File without changes
File without changes
File without changes
File without changes