Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(normalize): keep the sign in normalize_precision (#2098)

ShryukGrandhib08ab496d2ab
diff --git a/semantica/normalize/number_normalizer.py b/semantica/normalize/number_normalizer.py
index 37dd9fe3..5af27615 100644
--- a/semantica/normalize/number_normalizer.py
+++ b/semantica/normalize/number_normalizer.py
@@ -30,6 +30,7 @@ Author: Semantica Contributors
License: MIT
"""
+import math
import re
from typing import Any, Dict, List, Optional, Union
@@ -843,4 +844,4 @@ class ScientificNotationHandler:
# Round to significant digits
rounded = round(magnitude, significant_digits - 1)
- return rounded * (10**order)
+ return math.copysign(rounded * (10**order), number)
diff --git a/tests/normalize/test_number_normalizer.py b/tests/normalize/test_number_normalizer.py
index 6e9f7481..2ce3c382 100644
--- a/tests/normalize/test_number_normalizer.py
+++ b/tests/normalize/test_number_normalizer.py
@@ -140,6 +140,11 @@ class TestScientificNotationHandler(unittest.TestCase):
self.assertEqual(self.handler.parse_scientific_notation("1.23e4"), 12300.0)
self.assertEqual(self.handler.parse_scientific_notation("1.23E-2"), 0.0123)
+ def test_normalize_precision_keeps_sign(self):
+ self.assertEqual(self.handler.normalize_precision(1234.5, 2), 1200.0)
+ self.assertEqual(self.handler.normalize_precision(-1234.5, 2), -1200.0)
+ self.assertEqual(self.handler.normalize_precision(-0.000123, 2), -0.00012)
+
if __name__ == "__main__":
unittest.main()