mathparse 0.2.4__tar.gz → 0.2.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.4
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
@@ -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.6'
@@ -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.
@@ -400,13 +423,15 @@ def preprocess_unary_operators(tokens: list) -> list:
400
423
  * At the beginning of the expression
401
424
  * After an opening parenthesis '('
402
425
  * After a binary operator `(+, -, *, /, ^)`
426
+ * After a unary function `(sqrt, log, neg)`
403
427
  """
404
428
  if not tokens:
405
429
  return tokens
406
430
 
407
431
  processed_tokens = []
408
432
 
409
- binary_operators = mathwords.BINARY_OPERATORS | {'('}
433
+ # A following minus sign should be treated as unary (negative)
434
+ unary_contexts = mathwords.BINARY_OPERATORS | {'('}
410
435
 
411
436
  for i, token in enumerate(tokens):
412
437
  if token == '-':
@@ -418,8 +443,9 @@ def preprocess_unary_operators(tokens: list) -> list:
418
443
  is_unary_minus = True
419
444
  elif i > 0:
420
445
  prev_token = tokens[i - 1]
421
- # A unary minus after opening parenthesis or binary operators
422
- if prev_token in binary_operators:
446
+ # A unary minus after opening parenthesis, binary operators,
447
+ # or unary functions
448
+ if prev_token in unary_contexts or is_unary(prev_token):
423
449
  is_unary_minus = True
424
450
 
425
451
  if is_unary_minus:
@@ -456,11 +482,11 @@ def to_postfix(tokens: list) -> list:
456
482
 
457
483
  for token in tokens:
458
484
  if is_int(token):
459
- postfix.append(int(token))
485
+ postfix.append(token)
460
486
  elif is_float(token):
461
- postfix.append(float(token))
487
+ postfix.append(token)
462
488
  elif token in mathwords.CONSTANTS:
463
- postfix.append(mathwords.CONSTANTS[token])
489
+ postfix.append(token)
464
490
  elif is_unary(token):
465
491
  opstack.append(token)
466
492
  elif token == '(':
@@ -513,6 +539,8 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
513
539
  stack.append(token)
514
540
  elif is_unary(token):
515
541
  a = stack.pop()
542
+ # Convert token (string) to number for unary function evaluation
543
+ a = to_number(a)
516
544
  total = mathwords.UNARY_FUNCTIONS[token](a)
517
545
  elif len(stack) == 1:
518
546
  raise PostfixTokenEvaluationException(
@@ -523,14 +551,15 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
523
551
  elif len(stack) > 1:
524
552
  b = stack.pop()
525
553
  a = stack.pop()
554
+
526
555
  if token == '+':
527
- total = a + b
556
+ total = to_number(a) + to_number(b)
528
557
  elif token == '-':
529
- total = a - b
558
+ total = to_number(a) - to_number(b)
530
559
  elif token == '*':
531
- total = a * b
560
+ total = to_number(a) * to_number(b)
532
561
  elif token == '^':
533
- total = a ** b
562
+ total = to_number(a) ** to_number(b)
534
563
  elif token == '/':
535
564
  if Decimal(str(b)) == 0:
536
565
  total = 'undefined'
@@ -539,14 +568,34 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
539
568
  elif token == '.':
540
569
  # Treat decimal points as a binary operator that combines the
541
570
  # integer and fractional part of two numbers
542
- # Example: 53 . 25 = 53.25
543
- if b == 0:
544
- total = Decimal(a)
571
+ # Example: 53 . 25 = 53.25, -3 . 5 = -3.5
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)
545
580
  else:
546
- # Count the digits in b to determine the divisor
547
- 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))
548
590
  divisor = 10 ** digits
549
- total = a + (b / divisor)
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
597
+ else:
598
+ total = numeric_a + fractional_part
550
599
  else:
551
600
  raise PostfixTokenEvaluationException(
552
601
  'Unknown token "{}"'.format(token)
@@ -561,7 +610,16 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
561
610
  'The postfix expression resulted in an empty stack'
562
611
  )
563
612
 
564
- 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
565
623
 
566
624
 
567
625
  def tokenize(string: str, language: str = None, escape: str = '___') -> list:
@@ -612,8 +670,22 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
612
670
  string = string.replace(spaced_phrase, phrase)
613
671
 
614
672
  # Binary operators must have space around them to be tokenized properly
673
+ # Special handling for minus sign: preserve leading negatives
615
674
  for operator in mathwords.BINARY_OPERATORS:
616
- string = string.replace(operator, f' {operator} ')
675
+ if operator == '-':
676
+ # For minus sign, use a pattern that only spaces it when it's
677
+ # clearly a binary operator (after digits or closing parenthesis).
678
+ # This preserves leading negatives like "-3" and distinguishes
679
+ # between:
680
+ # - "What is -3 + 3" --> "-3 + 3" (minus is part of number)
681
+ # - "5 - 3" --> "5 - 3" (minus is binary operator, needs spacing)
682
+ # - "math - 4" --> "- 4" (minus after letters is not a binary
683
+ # operator)
684
+ # Only add spaces around minus when preceded by a digit or closing
685
+ # parenthesis
686
+ string = re.sub(r'([\d)])\s*-\s*', r'\1 - ', string)
687
+ else:
688
+ string = string.replace(operator, f' {operator} ')
617
689
 
618
690
  # Parenthesis must have space around them to be tokenized properly
619
691
  string = string.replace('(', ' ( ')
@@ -752,11 +824,46 @@ def extract_expression(dirty_string: str, language: str) -> str:
752
824
  start_index = 0
753
825
  end_index = len(tokens)
754
826
 
755
- for part in tokens:
827
+ # Find the start of the mathematical expression
828
+ # Skip over non-mathematical tokens AND isolated binary operators
829
+ for i, part in enumerate(tokens):
756
830
  if is_symbol(part) or is_word(part, language):
757
- break
758
- else:
759
- start_index += 1
831
+ # A potential start was found, so check if it's a standalone binary
832
+ # operator. Binary operators (except '(') are only valid at the
833
+ # start if they are unary (such as a leading minus for a negative
834
+ # number)
835
+ if part in mathwords.BINARY_OPERATORS and part != '(':
836
+ # For a binary operator to be the start of an expression, it
837
+ # must be:
838
+ # 1. At the very beginning (position 0) - could be unary minus
839
+ # 2. OR all previous tokens were non-mathematical - also could
840
+ # be unary
841
+ # If there were non-math tokens before it, it's likely a
842
+ # separator
843
+
844
+ # Check if all previous tokens are non-mathematical
845
+ all_prev_non_math = True
846
+ for j in range(i):
847
+ if is_symbol(tokens[j]) or is_word(tokens[j], language):
848
+ all_prev_non_math = False
849
+ break
850
+
851
+ # Only include this operator if at start OR all previous
852
+ # non-math AND followed by a mathematical token
853
+ if all_prev_non_math and i + 1 < len(tokens):
854
+ next_token = tokens[i + 1]
855
+ if (
856
+ is_int(next_token) or is_float(next_token) or
857
+ is_constant(next_token) or is_unary(next_token) or
858
+ next_token == '(' or is_word(next_token, language)
859
+ ):
860
+ start_index = i
861
+ break
862
+ else:
863
+ # Start indexes can be an opening parenthesis, or a non-binary
864
+ # operator
865
+ start_index = i
866
+ break
760
867
 
761
868
  for part in reversed(tokens):
762
869
  if is_symbol(part) or is_word(part, language):
@@ -764,4 +871,10 @@ def extract_expression(dirty_string: str, language: str) -> str:
764
871
  else:
765
872
  end_index -= 1
766
873
 
767
- 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.4
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
@@ -12,6 +12,7 @@ mathparse.egg-info/top_level.txt
12
12
  tests/test_binary_operations.py
13
13
  tests/test_complex_statements.py
14
14
  tests/test_language_support.py
15
+ tests/test_prefix_unary_operations.py
15
16
  tests/test_replace_word_tokens.py
16
17
  tests/test_simplified_chinese.py
17
18
  tests/test_unary_operations.py
@@ -0,0 +1,397 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class NegativeNumberTestCase(TestCase):
6
+ """
7
+ Test cases for basic negative number operations.
8
+ """
9
+
10
+ def test_leading_negative_with_addition(self):
11
+ """
12
+ Test: -3 + 3 should equal 0
13
+ """
14
+ result = mathparse.parse('-3 + 3')
15
+ self.assertEqual(result, 0)
16
+
17
+ def test_leading_negative_with_subtraction(self):
18
+ """
19
+ Test: -3 - 5 should equal -8
20
+ """
21
+ result = mathparse.parse('-3 - 5')
22
+ self.assertEqual(result, -8)
23
+
24
+ def test_leading_negative_with_multiplication(self):
25
+ """
26
+ Test: -10 * 2 should equal -20
27
+ """
28
+ result = mathparse.parse('-10 * 2')
29
+ self.assertEqual(result, -20)
30
+
31
+ def test_negative_in_parentheses(self):
32
+ """
33
+ Test: (-3) should equal -3
34
+ """
35
+ result = mathparse.parse('(-3)')
36
+ self.assertEqual(result, -3)
37
+
38
+ def test_negative_in_parentheses_with_addition(self):
39
+ """
40
+ Test: (-3) + 5 should equal 2
41
+ """
42
+ result = mathparse.parse('(-3) + 5')
43
+ self.assertEqual(result, 2)
44
+
45
+ def test_addition_with_negative_in_parentheses(self):
46
+ """
47
+ Test: 5 + (-3) should equal 2
48
+ """
49
+ result = mathparse.parse('5 + (-3)')
50
+ self.assertEqual(result, 2)
51
+
52
+ def test_two_negatives_in_parentheses(self):
53
+ """
54
+ Test: (-3) * (-2) should equal 6
55
+ """
56
+ result = mathparse.parse('(-3) * (-2)')
57
+ self.assertEqual(result, 6)
58
+
59
+ def test_multiplication_by_negative(self):
60
+ """
61
+ Test: 3 * -2 should equal -6
62
+ """
63
+ result = mathparse.parse('3 * -2')
64
+ self.assertEqual(result, -6)
65
+
66
+ def test_subtraction_of_negative(self):
67
+ """
68
+ Test: 3 - -2 should equal 5 (subtracting a negative)
69
+ """
70
+ result = mathparse.parse('3--2')
71
+ self.assertEqual(result, 5)
72
+
73
+ def test_two_separate_negatives_with_addition(self):
74
+ """
75
+ Test: -3 + -5 should equal -8
76
+ """
77
+ result = mathparse.parse('-3 + -5')
78
+ self.assertEqual(result, -8)
79
+
80
+ def test_complex_expression_with_leading_negative(self):
81
+ """
82
+ Test: (-3 + 5) * 2 should equal 4
83
+ """
84
+ result = mathparse.parse('(-3 + 5) * 2')
85
+ self.assertEqual(result, 4)
86
+
87
+ def test_negative_after_operator_in_expression(self):
88
+ """
89
+ Test: 2 * (-3 + 5) should equal 4
90
+ """
91
+ result = mathparse.parse('2 * (-3 + 5)')
92
+ self.assertEqual(result, 4)
93
+
94
+ def test_negative_of_expression(self):
95
+ """
96
+ Test: -(3 + 5) should equal -8
97
+ """
98
+ result = mathparse.parse('-(3 + 5)')
99
+ self.assertEqual(result, -8)
100
+
101
+ def test_negative_decimal(self):
102
+ """
103
+ Test: -3.5 should equal -3.5 (parsed as -3 . 5)
104
+ """
105
+ result = mathparse.parse('-3.5')
106
+ self.assertEqual(float(result), -3.5)
107
+
108
+ def test_negative_decimal_in_expression(self):
109
+ """
110
+ Test: -10.25 + 5 should work correctly
111
+ """
112
+ result = mathparse.parse('-10.25 + 5')
113
+ # Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25
114
+ self.assertEqual(float(result), -5.25)
115
+
116
+ def test_positive_decimal(self):
117
+ """
118
+ Test: 3.5 should equal 3.5
119
+ """
120
+ result = mathparse.parse('3.5')
121
+ self.assertEqual(float(result), 3.5)
122
+
123
+ def test_negative_decimal_with_multiple_digits(self):
124
+ """
125
+ Test: -53.25 should equal -53.25
126
+ """
127
+ result = mathparse.parse('-53.25')
128
+ self.assertEqual(float(result), -53.25)
129
+
130
+ def test_negative_decimal_in_multiplication(self):
131
+ """
132
+ Test: -2.5 * 4 should equal -10.0
133
+ """
134
+ result = mathparse.parse('-2.5 * 4')
135
+ self.assertEqual(float(result), -10.0)
136
+
137
+
138
+ class NegativeWithUnaryFunctionsTestCase(TestCase):
139
+ """
140
+ Test cases for negative numbers with unary functions.
141
+ """
142
+
143
+ def test_sqrt_with_negative_causes_math_error(self):
144
+ """
145
+ Test: sqrt -16 should parse correctly but give a math domain error.
146
+
147
+ The tokenizer keeps -16 as a single token '-16', which is correctly
148
+ identified as an integer and passed to sqrt, causing the expected
149
+ math domain error.
150
+ """
151
+ # Verify tokenization
152
+ tokens = mathparse.tokenize('sqrt -16')
153
+ self.assertEqual(tokens, ['sqrt', '-16'])
154
+
155
+ # Verify it causes the expected math error
156
+ with self.assertRaises(ValueError) as context:
157
+ mathparse.parse('sqrt -16')
158
+ self.assertIn("math domain error", str(context.exception).lower())
159
+
160
+ def test_log_with_negative_causes_math_error(self):
161
+ """
162
+ Test: log -10 should parse correctly but give a math domain error.
163
+ """
164
+ # Verify tokenization
165
+ tokens = mathparse.tokenize('log -10')
166
+ self.assertEqual(tokens, ['log', '-10'])
167
+
168
+ # Verify it causes the expected math error
169
+ with self.assertRaises(ValueError) as context:
170
+ mathparse.parse('log -10')
171
+ self.assertIn("math domain error", str(context.exception).lower())
172
+
173
+ def test_sqrt_with_positive_after_operator(self):
174
+ """
175
+ Test: 3 + sqrt 16 should equal 7.0
176
+ """
177
+ result = mathparse.parse('3 + sqrt 16')
178
+ self.assertEqual(result, 7.0)
179
+
180
+ def test_multiple_negatives_with_operations(self):
181
+ """
182
+ Test: -2 * -3 + -1 should equal 5
183
+ """
184
+ result = mathparse.parse('-2 * -3 + -1')
185
+ self.assertEqual(result, 5)
186
+
187
+
188
+ class ExtractExpressionNegativeTestCase(TestCase):
189
+ """
190
+ Test cases for extract_expression with negative numbers.
191
+ """
192
+
193
+ def test_extract_leading_negative(self):
194
+ """
195
+ Test: 'What is -3 + 3?' should extract '-3 + 3'
196
+ """
197
+ result = mathparse.extract_expression(
198
+ 'What is -3 + 3?', language='ENG'
199
+ )
200
+ self.assertEqual(result, '-3 + 3')
201
+
202
+ def test_extract_negative_with_multiplication(self):
203
+ """
204
+ Test: 'Calculate -5 * 2' should extract '-5 * 2'
205
+ """
206
+ result = mathparse.extract_expression(
207
+ 'Calculate -5 * 2', language='ENG'
208
+ )
209
+ self.assertEqual(result, '-5 * 2')
210
+
211
+ def test_extract_binary_subtraction(self):
212
+ """
213
+ Test: 'The answer to 10 - 5 is' should extract '10 - 5'
214
+ """
215
+ result = mathparse.extract_expression(
216
+ 'The answer to 10 - 5 is', language='ENG'
217
+ )
218
+ self.assertEqual(result, '10 - 5')
219
+
220
+ def test_extract_pure_negative_expression(self):
221
+ """
222
+ Test: '-3 + 3' should extract '-3 + 3' (with no extra text)
223
+ """
224
+ result = mathparse.extract_expression('-3 + 3', language='ENG')
225
+ self.assertEqual(result, '-3 + 3')
226
+
227
+ def test_extract_pure_subtraction(self):
228
+ """
229
+ Test: '5 - 3' should extract '5 - 3' (with no extra text)
230
+ """
231
+ result = mathparse.extract_expression('5 - 3', language='ENG')
232
+ self.assertEqual(result, '5 - 3')
233
+
234
+ def test_extract_negative_decimal(self):
235
+ """
236
+ Test: Extract leading negative decimal from sentence
237
+ """
238
+ result = mathparse.extract_expression(
239
+ 'What is -100.5 * 20?', language='ENG'
240
+ )
241
+ self.assertEqual(result, '-100.5 * 20')
242
+
243
+ def test_extract_negative_in_parentheses_with_words(self):
244
+ """
245
+ Test: Extract expression with negative in parentheses from sentence
246
+ """
247
+ result = mathparse.extract_expression(
248
+ 'Compute (-3) + 5', language='ENG'
249
+ )
250
+ # NOTE: Spaces are currently added, but ideally these will be removed
251
+ # in the future
252
+ self.assertEqual(result, '( -3 ) + 5')
253
+
254
+
255
+ class NegativeDecimalsLessThanOneTestCase(TestCase):
256
+ """
257
+ Test cases for negative decimal numbers between -1 and 0.
258
+
259
+ This addresses a bug where negative decimals less than 1
260
+ (e.g., -0.2, -0.5) were losing their negative sign during parsing.
261
+ The issue was caused by Python's -0 == 0 equality, where int('-0')
262
+ returns 0, losing sign information.
263
+ """
264
+
265
+ def test_negative_zero_point_two(self):
266
+ """
267
+ Test: -0.2 should equal -0.2
268
+ """
269
+ result = mathparse.parse('-0.2')
270
+ self.assertEqual(result, -0.2)
271
+
272
+ def test_negative_zero_point_five(self):
273
+ """
274
+ Test: -0.5 should equal -0.5
275
+ """
276
+ result = mathparse.parse('-0.5')
277
+ self.assertEqual(result, -0.5)
278
+
279
+ def test_negative_zero_point_nine(self):
280
+ """
281
+ Test: -0.9 should equal -0.9
282
+ """
283
+ result = mathparse.parse('-0.9')
284
+ self.assertEqual(result, -0.9)
285
+
286
+ def test_negative_zero_point_one(self):
287
+ """
288
+ Test: -0.1 should equal -0.1
289
+ """
290
+ result = mathparse.parse('-0.1')
291
+ self.assertEqual(result, -0.1)
292
+
293
+ def test_negative_zero_point_ninety_nine(self):
294
+ """
295
+ Test: -0.99 should equal -0.99"""
296
+ result = mathparse.parse('-0.99')
297
+ self.assertEqual(result, -0.99)
298
+
299
+ def test_negative_zero_point_zero_one(self):
300
+ """
301
+ Test: -0.01 should equal -0.01
302
+ """
303
+ result = mathparse.parse('-0.01')
304
+ self.assertEqual(result, -0.01)
305
+
306
+ def test_negative_zero_point_nine_nine_nine(self):
307
+ """
308
+ Test: -0.999 should equal -0.999
309
+ """
310
+ result = mathparse.parse('-0.999')
311
+ self.assertEqual(result, -0.999)
312
+
313
+ def test_negative_decimal_in_addition(self):
314
+ """
315
+ Test: -0.5 + 1 should equal 0.5
316
+ """
317
+ result = mathparse.parse('-0.5 + 1')
318
+ self.assertEqual(result, 0.5)
319
+
320
+ def test_negative_decimal_in_subtraction(self):
321
+ """
322
+ Test: 2 - -0.5 should equal 2.5
323
+ """
324
+ result = mathparse.parse('2 - -0.5')
325
+ self.assertEqual(result, 2.5)
326
+
327
+ def test_negative_decimal_in_multiplication(self):
328
+ """
329
+ Test: -0.5 * 4 should equal -2.0
330
+ """
331
+ result = mathparse.parse('-0.5 * 4')
332
+ self.assertEqual(result, -2.0)
333
+
334
+ def test_negative_decimal_in_division(self):
335
+ """
336
+ Test: -0.8 / 2 should equal -0.4
337
+ """
338
+ result = mathparse.parse('-0.8 / 2')
339
+ self.assertEqual(float(result), -0.4)
340
+
341
+ def test_multiple_negative_decimals(self):
342
+ """
343
+ Test: -0.3 + -0.2 should equal -0.5
344
+ """
345
+ result = mathparse.parse('-0.3 + -0.2')
346
+ self.assertAlmostEqual(result, -0.5, places=10)
347
+
348
+ def test_negative_decimal_with_parentheses(self):
349
+ """
350
+ Test: (-0.5) should equal -0.5
351
+ """
352
+ result = mathparse.parse('(-0.5)')
353
+ self.assertEqual(result, -0.5)
354
+
355
+ def test_negative_decimal_in_complex_expression(self):
356
+ """
357
+ Test: (-0.5 + 1) * 2 should equal 1.0
358
+ """
359
+ result = mathparse.parse('(-0.5 + 1) * 2')
360
+ self.assertEqual(result, 1.0)
361
+
362
+ def test_positive_decimals_still_work(self):
363
+ """
364
+ Test: 0.5 should equal 0.5
365
+ """
366
+ result = mathparse.parse('0.5')
367
+ self.assertEqual(result, 0.5)
368
+
369
+ def test_negative_integers_still_work(self):
370
+ """
371
+ Test: -5 should equal -5
372
+ """
373
+ result = mathparse.parse('-5')
374
+ self.assertEqual(result, -5)
375
+
376
+ def test_negative_decimals_greater_than_one_still_work(self):
377
+ """
378
+ Test: -1.5 should equal -1.5
379
+ """
380
+ result = mathparse.parse('-1.5')
381
+ self.assertEqual(result, -1.5)
382
+
383
+ def test_temperature_conversion_use_case(self):
384
+ """
385
+ -0.2 Celsius to Fahrenheit formula: (-0.2 * 9/5) + 32 = 31.64
386
+ (Example calculation)
387
+ """
388
+ result = mathparse.parse('-0.2 * 9 / 5 + 32')
389
+ self.assertAlmostEqual(float(result), 31.64, places=2)
390
+
391
+ def test_financial_calculation_use_case(self):
392
+ """
393
+ Balance: $100, transaction: -$0.25, expected: $99.75
394
+ (Example calculation)
395
+ """
396
+ result = mathparse.parse('100 + -0.25')
397
+ self.assertEqual(result, 99.75)
@@ -11,12 +11,18 @@ class BooleanChecksTestCase(TestCase):
11
11
  def test_is_not_integer(self):
12
12
  self.assertFalse(mathparse.is_int('42.2'))
13
13
 
14
+ def test_is_not_integer_negative(self):
15
+ self.assertFalse(mathparse.is_int('-42.2'))
16
+
14
17
  def test_is_float(self):
15
18
  self.assertTrue(mathparse.is_float('0.5'))
16
19
 
17
20
  def test_is_not_float(self):
18
21
  self.assertFalse(mathparse.is_float('5'))
19
22
 
23
+ def test_is_not_float_negative(self):
24
+ self.assertTrue(mathparse.is_float('-0.5'))
25
+
20
26
  def test_is_constant(self):
21
27
  self.assertTrue(mathparse.is_constant('pi'))
22
28
 
@@ -81,6 +87,18 @@ class ExtractExpressionTestCase(TestCase):
81
87
 
82
88
  self.assertEqual(result, '3 + 3')
83
89
 
90
+ def test_extract_expression_with_negative(self):
91
+ result = mathparse.extract_expression('-3 + 3', language='ENG')
92
+
93
+ self.assertEqual(result, '-3 + 3')
94
+
95
+ def test_extract_expression_with_words(self):
96
+ result = mathparse.extract_expression(
97
+ 'three plus three', language='ENG'
98
+ )
99
+
100
+ self.assertEqual(result, 'three plus three')
101
+
84
102
  def test_ignore_punctuation(self):
85
103
  result = mathparse.extract_expression('3?', language='ENG')
86
104
 
File without changes
File without changes
File without changes
File without changes