mathparse 0.2.8__py3-none-any.whl → 0.2.10__py3-none-any.whl

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/__init__.py CHANGED
@@ -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.8'
5
+ __version__ = '0.2.10'
mathparse/mathparse.py CHANGED
@@ -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
 
@@ -466,17 +470,21 @@ def to_postfix(tokens: list) -> list:
466
470
  """
467
471
  precedence = {
468
472
  '.': 5,
469
- '/': 4,
470
- '*': 4,
471
- '+': 3,
472
- '-': 3,
473
- '^': 2,
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
- # For binary operators, pop operators with higher or
501
- # equal precedence
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]] >= precedence[token]
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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.8
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
@@ -0,0 +1,8 @@
1
+ mathparse/__init__.py,sha256=1aGuZ_JSjOS7g1tg4PYOZ6TNnR6Q3LctIFYFPK63_dE,111
2
+ mathparse/mathparse.py,sha256=CaGRB4iaYkKctSCQhsD4dqPR_15A1dDrKfFrLtxXj3c,32176
3
+ mathparse/mathwords.py,sha256=Ui4K5Y1aDkSKtlUWDn5hO7C8FzNO4kYrZBXFaGjWWLI,48306
4
+ mathparse-0.2.10.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
5
+ mathparse-0.2.10.dist-info/METADATA,sha256=7ucti730Hh9YK-Fvv8SG5H4i-M1GbzxMWYVUI35lGMs,5158
6
+ mathparse-0.2.10.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
+ mathparse-0.2.10.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
8
+ mathparse-0.2.10.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- mathparse/__init__.py,sha256=JjEq33WHjALR_G8DWNwmgY0EIsYEqhGNNfA4umxOyYs,110
2
- mathparse/mathparse.py,sha256=KYuV1DIYxL4p7yscUZyQhOt_ucgH37icVvzmSfD73CM,31136
3
- mathparse/mathwords.py,sha256=Ui4K5Y1aDkSKtlUWDn5hO7C8FzNO4kYrZBXFaGjWWLI,48306
4
- mathparse-0.2.8.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
5
- mathparse-0.2.8.dist-info/METADATA,sha256=VNrAyhkQmsnGqS20PvrmH1IMnByRcJY-PwWYtYU76hs,5157
6
- mathparse-0.2.8.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
- mathparse-0.2.8.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
8
- mathparse-0.2.8.dist-info/RECORD,,