Make modulo require units to be same or dimensionless

Fixes #19517
This commit is contained in:
Benjamin Nauck
2025-02-11 15:33:30 +01:00
committed by Chris Hennes
parent 82b5ce2d10
commit 0ba52f0e22
2 changed files with 12 additions and 6 deletions

View File

@@ -2471,7 +2471,9 @@ Py::Object FunctionExpression::evaluate(const Expression *expr, int f, const std
case MOD:
if (e2.isNone())
_EXPR_THROW("Invalid second argument.",expr);
unit = v1.getUnit() / v2.getUnit();
if (v1.getUnit() != v2.getUnit() && !v1.isDimensionless() && !v2.isDimensionless())
_EXPR_THROW("Units must be equal or dimensionless.",expr);
unit = v1.getUnit();
break;
case POW: {
if (e2.isNone())

View File

@@ -1088,14 +1088,18 @@ class SpreadsheetCases(unittest.TestCase):
self.assertEqual(sheet.B8, rot)
self.assertEqual(sheet.C8, pla)
def testIssue3128(self):
"""Regression test for issue 3128; mod should work with arbitrary units for both arguments"""
def testIssue19517(self):
"""Regression test for issue 19517; mod should work with units"""
sheet = self.doc.addObject("Spreadsheet::Sheet", "Spreadsheet")
sheet.set("A1", "=mod(7mm;3mm)")
sheet.set("A2", "=mod(7kg;3mm)")
self.doc.recompute()
self.assertEqual(sheet.A1, Units.Quantity("1"))
self.assertEqual(sheet.A2, Units.Quantity("1 kg/mm"))
self.assertEqual(sheet.A1, Units.Quantity("1 mm"))
try:
sheet.set("A2", "=mod(7kg;3mm)")
self.doc.recompute()
self.fail("Units need to be the same or dimensionless")
except Exception:
pass
def testIssue3363(self):
"""Regression test for issue 3363; Nested conditionals statement fails with additional conditional statement in false-branch"""