mathparse 0.2.4__py3-none-any.whl → 0.2.5__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.4'
5
+ __version__ = '0.2.5'
mathparse/mathparse.py CHANGED
@@ -400,13 +400,15 @@ def preprocess_unary_operators(tokens: list) -> list:
400
400
  * At the beginning of the expression
401
401
  * After an opening parenthesis '('
402
402
  * After a binary operator `(+, -, *, /, ^)`
403
+ * After a unary function `(sqrt, log, neg)`
403
404
  """
404
405
  if not tokens:
405
406
  return tokens
406
407
 
407
408
  processed_tokens = []
408
409
 
409
- binary_operators = mathwords.BINARY_OPERATORS | {'('}
410
+ # A following minus sign should be treated as unary (negative)
411
+ unary_contexts = mathwords.BINARY_OPERATORS | {'('}
410
412
 
411
413
  for i, token in enumerate(tokens):
412
414
  if token == '-':
@@ -418,8 +420,9 @@ def preprocess_unary_operators(tokens: list) -> list:
418
420
  is_unary_minus = True
419
421
  elif i > 0:
420
422
  prev_token = tokens[i - 1]
421
- # A unary minus after opening parenthesis or binary operators
422
- if prev_token in binary_operators:
423
+ # A unary minus after opening parenthesis, binary operators,
424
+ # or unary functions
425
+ if prev_token in unary_contexts or is_unary(prev_token):
423
426
  is_unary_minus = True
424
427
 
425
428
  if is_unary_minus:
@@ -539,14 +542,20 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
539
542
  elif token == '.':
540
543
  # Treat decimal points as a binary operator that combines the
541
544
  # integer and fractional part of two numbers
542
- # Example: 53 . 25 = 53.25
545
+ # Example: 53 . 25 = 53.25, -3 . 5 = -3.5
543
546
  if b == 0:
544
547
  total = Decimal(a)
545
548
  else:
546
549
  # Count the digits in b to determine the divisor
547
550
  digits = len(str(int(b)))
548
551
  divisor = 10 ** digits
549
- total = a + (b / divisor)
552
+ fractional_part = b / divisor
553
+ # Handle negative numbers correctly: -3 . 5 should be -3.5,
554
+ # not -2.5
555
+ if a < 0:
556
+ total = a - fractional_part
557
+ else:
558
+ total = a + fractional_part
550
559
  else:
551
560
  raise PostfixTokenEvaluationException(
552
561
  'Unknown token "{}"'.format(token)
@@ -612,8 +621,22 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
612
621
  string = string.replace(spaced_phrase, phrase)
613
622
 
614
623
  # Binary operators must have space around them to be tokenized properly
624
+ # Special handling for minus sign: preserve leading negatives
615
625
  for operator in mathwords.BINARY_OPERATORS:
616
- string = string.replace(operator, f' {operator} ')
626
+ if operator == '-':
627
+ # For minus sign, use a pattern that only spaces it when it's
628
+ # clearly a binary operator (after digits or closing parenthesis).
629
+ # This preserves leading negatives like "-3" and distinguishes
630
+ # between:
631
+ # - "What is -3 + 3" --> "-3 + 3" (minus is part of number)
632
+ # - "5 - 3" --> "5 - 3" (minus is binary operator, needs spacing)
633
+ # - "math - 4" --> "- 4" (minus after letters is not a binary
634
+ # operator)
635
+ # Only add spaces around minus when preceded by a digit or closing
636
+ # parenthesis
637
+ string = re.sub(r'([\d)])\s*-\s*', r'\1 - ', string)
638
+ else:
639
+ string = string.replace(operator, f' {operator} ')
617
640
 
618
641
  # Parenthesis must have space around them to be tokenized properly
619
642
  string = string.replace('(', ' ( ')
@@ -752,11 +775,46 @@ def extract_expression(dirty_string: str, language: str) -> str:
752
775
  start_index = 0
753
776
  end_index = len(tokens)
754
777
 
755
- for part in tokens:
778
+ # Find the start of the mathematical expression
779
+ # Skip over non-mathematical tokens AND isolated binary operators
780
+ for i, part in enumerate(tokens):
756
781
  if is_symbol(part) or is_word(part, language):
757
- break
758
- else:
759
- start_index += 1
782
+ # A potential start was found, so check if it's a standalone binary
783
+ # operator. Binary operators (except '(') are only valid at the
784
+ # start if they are unary (such as a leading minus for a negative
785
+ # number)
786
+ if part in mathwords.BINARY_OPERATORS and part != '(':
787
+ # For a binary operator to be the start of an expression, it
788
+ # must be:
789
+ # 1. At the very beginning (position 0) - could be unary minus
790
+ # 2. OR all previous tokens were non-mathematical - also could
791
+ # be unary
792
+ # If there were non-math tokens before it, it's likely a
793
+ # separator
794
+
795
+ # Check if all previous tokens are non-mathematical
796
+ all_prev_non_math = True
797
+ for j in range(i):
798
+ if is_symbol(tokens[j]) or is_word(tokens[j], language):
799
+ all_prev_non_math = False
800
+ break
801
+
802
+ # Only include this operator if at start OR all previous
803
+ # non-math AND followed by a mathematical token
804
+ if all_prev_non_math and i + 1 < len(tokens):
805
+ next_token = tokens[i + 1]
806
+ if (
807
+ is_int(next_token) or is_float(next_token) or
808
+ is_constant(next_token) or is_unary(next_token) or
809
+ next_token == '(' or is_word(next_token, language)
810
+ ):
811
+ start_index = i
812
+ break
813
+ else:
814
+ # Start indexes can be an opening parenthesis, or a non-binary
815
+ # operator
816
+ start_index = i
817
+ break
760
818
 
761
819
  for part in reversed(tokens):
762
820
  if is_symbol(part) or is_word(part, language):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.4
3
+ Version: 0.2.5
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=ZBWkFcEyYlzjaH0qWeBIOxXUn9vIJOxbdpHpfXMtFTI,110
2
+ mathparse/mathparse.py,sha256=JNB33gr1dk_2pG_ftkcCrBBaoZHzWX_fQ-XLxStOjGw,29337
3
+ mathparse/mathwords.py,sha256=0SA6rG4X7lSRkhaHr-oIC1g6HFKCFJysqeyLASbALLo,48306
4
+ mathparse-0.2.5.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
5
+ mathparse-0.2.5.dist-info/METADATA,sha256=vy9LB508HGmsaNN6Ri62ncBacsVlaqLsRDO4538vn6c,2325
6
+ mathparse-0.2.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ mathparse-0.2.5.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
8
+ mathparse-0.2.5.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- mathparse/__init__.py,sha256=8ezmwPLTfjAFn-5dEizcN50r1djK_RTFEwQjzZ8J4IE,110
2
- mathparse/mathparse.py,sha256=QaCuSQGptxx7C88ZZNDmauuvqpc-amrfDJoTDpBl0lU,26237
3
- mathparse/mathwords.py,sha256=0SA6rG4X7lSRkhaHr-oIC1g6HFKCFJysqeyLASbALLo,48306
4
- mathparse-0.2.4.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
5
- mathparse-0.2.4.dist-info/METADATA,sha256=H3MXoDbR7HtsNgbx-xqj9rV7Jf5_5aKMZqi09TWrqBM,2325
6
- mathparse-0.2.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
- mathparse-0.2.4.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
8
- mathparse-0.2.4.dist-info/RECORD,,