Skip to content

Commit d660e08

Browse files
committed
add math.min method
1 parent 9fb4da7 commit d660e08

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

library/modules/math.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func init() {
1717
RegisterMethod(MathMethods, "isZero", mathIsZero)
1818
RegisterMethod(MathMethods, "sin", mathSin)
1919
RegisterMethod(MathMethods, "tan", mathTan)
20+
RegisterMethod(MathMethods, "min", mathMin)
2021

2122
RegisterProperty(MathProperties, "pi", mathPi)
2223
RegisterProperty(MathProperties, "e", mathE)
@@ -129,6 +130,30 @@ func mathTan(scope *object.Scope, tok token.Token, args ...object.Object) object
129130
return &object.Number{Value: number.Value.Tan()}
130131
}
131132

133+
// mathMin returns the smallest number of the referenced numbers.
134+
func mathMin(scope *object.Scope, tok token.Token, args ...object.Object) object.Object {
135+
if len(args) < 2 {
136+
panic("math.min requires at least two arguments")
137+
}
138+
139+
if args[0].Type() != object.NUMBER {
140+
return nil
141+
}
142+
143+
if args[1].Type() != object.NUMBER {
144+
return nil
145+
}
146+
147+
number1 := args[0].(*object.Number)
148+
number2 := args[1].(*object.Number)
149+
150+
if number1.Value.LessThan(number2.Value) {
151+
return number1
152+
}
153+
154+
return number2
155+
}
156+
132157
// Properties
133158

134159
// mathPi returns the value of π, othewise known as Pi.

0 commit comments

Comments
 (0)