🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fix: guard MathToolkit.math_divide against division by zero (#4121)
maoxin1234baf58ca8e436
diff --git a/camel/toolkits/math_toolkit.py b/camel/toolkits/math_toolkit.pyindex ead6bc4dc..a8d377efb 100644--- a/camel/toolkits/math_toolkit.py+++ b/camel/toolkits/math_toolkit.py@@ -13,7 +13,7 @@# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========import warnings-from typing import List+from typing import List, Unionfrom camel.toolkits.base import BaseToolkitfrom camel.toolkits.function_tool import FunctionTool@@ -70,7 +70,7 @@ class MathToolkit(BaseToolkit):def math_divide(self, a: float, b: float, decimal_places: int = 2- ) -> float:+ ) -> Union[float, str]:r"""Divides two numbers.Args:@@ -80,8 +80,11 @@ class MathToolkit(BaseToolkit):decimal places to round to. Defaults to 2.Returns:- float: The result of dividing :obj:`a` by :obj:`b`.+ Union[float, str]: The result of dividing :obj:`a` by :obj:`b`,+ or an error message if :obj:`b` is zero."""+ if b == 0:+ return "Error: Cannot divide by zero."return round(a / b, decimal_places)def math_round(self, a: float, decimal_places: int = 0) -> float:diff --git a/test/toolkits/test_math_functions.py b/test/toolkits/test_math_functions.pynew file mode 100644index 000000000..4c8aa3aeb--- /dev/null+++ b/test/toolkits/test_math_functions.py@@ -0,0 +1,42 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++from camel.toolkits import MathToolkit+++def test_math_divide_normal():+ toolkit = MathToolkit()+ assert toolkit.math_divide(10, 4) == 2.5+ assert toolkit.math_divide(10, 3, decimal_places=3) == 3.333+++def test_math_divide_by_zero_returns_message():+ toolkit = MathToolkit()+ result = toolkit.math_divide(1, 0)+ assert isinstance(result, str)+ assert "divide by zero" in result.lower()+++def test_basic_operations():+ toolkit = MathToolkit()+ assert toolkit.math_add(2, 3) == 5+ assert toolkit.math_subtract(5, 2) == 3+ assert toolkit.math_multiply(2, 3) == 6+ assert toolkit.math_round(3.14159, 2) == 3.14+++def test_get_tools():+ toolkit = MathToolkit()+ tools = toolkit.get_tools()+ assert len(tools) == 5