mathparse 0.2.5__py3-none-any.whl → 0.2.6__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.5'
5
+ __version__ = '0.2.6'
mathparse/mathparse.py CHANGED
@@ -18,11 +18,13 @@ def is_int(string: str) -> bool:
18
18
  """
19
19
  Return true if string is an integer.
20
20
  """
21
- try:
22
- int(string)
23
- return True
24
- except ValueError:
25
- return False
21
+ # Allow leading minus sign
22
+ if string[0] == '-':
23
+ # Must have at least one digit after the minus
24
+ return len(string) > 1 and string[1:].isdigit()
25
+ else:
26
+ # Must be all digits
27
+ return string.isdigit()
26
28
 
27
29
 
28
30
  def is_float(string: str) -> bool:
@@ -79,6 +81,27 @@ def is_word(word: str, language: str) -> bool:
79
81
  return word in words
80
82
 
81
83
 
84
+ def to_number(val) -> Union[int, float, str, Decimal]:
85
+ """
86
+ Convert a string to an int or float if possible.
87
+ """
88
+ # If already a number (int, float, Decimal), return as-is
89
+ if isinstance(val, (int, float, Decimal)):
90
+ return val
91
+
92
+ # Check if it's a constant and convert
93
+ if is_constant(val):
94
+ return mathwords.CONSTANTS[val]
95
+
96
+ # Otherwise, try to convert string to number
97
+ if is_int(val):
98
+ return int(val)
99
+ elif is_float(val):
100
+ return float(val)
101
+
102
+ return val
103
+
104
+
82
105
  def create_unicode_word_boundary_pattern(word: str) -> str:
83
106
  """
84
107
  Create a regex pattern with Unicode-aware word boundaries.
@@ -459,11 +482,11 @@ def to_postfix(tokens: list) -> list:
459
482
 
460
483
  for token in tokens:
461
484
  if is_int(token):
462
- postfix.append(int(token))
485
+ postfix.append(token)
463
486
  elif is_float(token):
464
- postfix.append(float(token))
487
+ postfix.append(token)
465
488
  elif token in mathwords.CONSTANTS:
466
- postfix.append(mathwords.CONSTANTS[token])
489
+ postfix.append(token)
467
490
  elif is_unary(token):
468
491
  opstack.append(token)
469
492
  elif token == '(':
@@ -516,6 +539,8 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
516
539
  stack.append(token)
517
540
  elif is_unary(token):
518
541
  a = stack.pop()
542
+ # Convert token (string) to number for unary function evaluation
543
+ a = to_number(a)
519
544
  total = mathwords.UNARY_FUNCTIONS[token](a)
520
545
  elif len(stack) == 1:
521
546
  raise PostfixTokenEvaluationException(
@@ -526,14 +551,15 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
526
551
  elif len(stack) > 1:
527
552
  b = stack.pop()
528
553
  a = stack.pop()
554
+
529
555
  if token == '+':
530
- total = a + b
556
+ total = to_number(a) + to_number(b)
531
557
  elif token == '-':
532
- total = a - b
558
+ total = to_number(a) - to_number(b)
533
559
  elif token == '*':
534
- total = a * b
560
+ total = to_number(a) * to_number(b)
535
561
  elif token == '^':
536
- total = a ** b
562
+ total = to_number(a) ** to_number(b)
537
563
  elif token == '/':
538
564
  if Decimal(str(b)) == 0:
539
565
  total = 'undefined'
@@ -543,19 +569,33 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
543
569
  # Treat decimal points as a binary operator that combines the
544
570
  # integer and fractional part of two numbers
545
571
  # Example: 53 . 25 = 53.25, -3 . 5 = -3.5
546
- if b == 0:
547
- total = Decimal(a)
572
+
573
+ # Convert b to number
574
+ numeric_b = to_number(b)
575
+
576
+ if numeric_b == 0:
577
+ # Convert a to number
578
+ numeric_a = to_number(a)
579
+ total = Decimal(numeric_a)
548
580
  else:
549
- # Count the digits in b to determine the divisor
550
- digits = len(str(int(b)))
581
+ # Check if 'a' has a negative sign (handles -0 case)
582
+ is_negative = str(a).startswith('-')
583
+
584
+ # Convert a to number for calculation
585
+ numeric_a = to_number(a)
586
+
587
+ # Count the digits in the original string b to preserve
588
+ # leading zeros (e.g., "01" has 2 digits, not 1)
589
+ digits = len(str(b))
551
590
  divisor = 10 ** digits
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
591
+ fractional_part = numeric_b / divisor
592
+
593
+ # Handle negatives: -3 . 5 = -3.5, not -2.5
594
+ # Also -0 . 5 = -0.5 (check string since -0 == 0)
595
+ if is_negative:
596
+ total = numeric_a - fractional_part
557
597
  else:
558
- total = a + fractional_part
598
+ total = numeric_a + fractional_part
559
599
  else:
560
600
  raise PostfixTokenEvaluationException(
561
601
  'Unknown token "{}"'.format(token)
@@ -570,7 +610,16 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
570
610
  'The postfix expression resulted in an empty stack'
571
611
  )
572
612
 
573
- return stack.pop()
613
+ result = stack.pop()
614
+
615
+ # Convert final result from string to number if needed
616
+ if isinstance(result, str):
617
+ if is_int(result):
618
+ result = int(result)
619
+ elif is_float(result):
620
+ result = float(result)
621
+
622
+ return result
574
623
 
575
624
 
576
625
  def tokenize(string: str, language: str = None, escape: str = '___') -> list:
@@ -822,4 +871,10 @@ def extract_expression(dirty_string: str, language: str) -> str:
822
871
  else:
823
872
  end_index -= 1
824
873
 
825
- return ' '.join(tokens[start_index:end_index])
874
+ result = ' '.join(tokens[start_index:end_index])
875
+
876
+ # Remove spaces around decimal points for cleaner output
877
+ # Replace " . " with "." to convert "-100 . 5" to "-100.5"
878
+ result = result.replace(' . ', '.')
879
+
880
+ return result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.5
3
+ Version: 0.2.6
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=W_RR0JdfkLI6xTdLLMADVg-Qr4t0w4RRUr_vrHP5h90,110
2
+ mathparse/mathparse.py,sha256=KYuV1DIYxL4p7yscUZyQhOt_ucgH37icVvzmSfD73CM,31136
3
+ mathparse/mathwords.py,sha256=0SA6rG4X7lSRkhaHr-oIC1g6HFKCFJysqeyLASbALLo,48306
4
+ mathparse-0.2.6.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
5
+ mathparse-0.2.6.dist-info/METADATA,sha256=MUkys02qKEdCGTjY_KclV1oDXBTBeJs_Ij7JWOV-Nvg,2325
6
+ mathparse-0.2.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ mathparse-0.2.6.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
8
+ mathparse-0.2.6.dist-info/RECORD,,
@@ -1,8 +0,0 @@
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,,