-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.lua
More file actions
49 lines (47 loc) · 1.5 KB
/
Copy patharithmetic.lua
File metadata and controls
49 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
tool {
name = "arithmetic",
description = "Perform basic arithmetic operations (add, subtract, multiply, divide) on two numbers.",
parameters = {
type = "object",
properties = {
operation = {
type = "string",
description = "The operation: add, subtract, multiply, or divide"
},
a = {
type = "number",
description = "First operand"
},
b = {
type = "number",
description = "Second operand"
}
},
required = { "operation", "a", "b" }
},
handler = function(args)
local op = args.operation
local a = tonumber(args.a)
local b = tonumber(args.b)
if not op then
return "Error: 'operation' is required"
end
if not a or not b then
return "Error: 'a' and 'b' must be numbers"
end
if op == "add" then
return string.format("%.10g", a + b)
elseif op == "subtract" then
return string.format("%.10g", a - b)
elseif op == "multiply" then
return string.format("%.10g", a * b)
elseif op == "divide" then
if b == 0 then
return "Error: division by zero"
end
return string.format("%.10g", a / b)
else
return "Error: unknown operation '" .. op .. "'. Use add, subtract, multiply, or divide."
end
end
}