Skip to content

Commit

Permalink
add math.min method
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidesu committed Oct 21, 2023
1 parent 9fb4da7 commit d660e08
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/modules/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func init() {
RegisterMethod(MathMethods, "isZero", mathIsZero)
RegisterMethod(MathMethods, "sin", mathSin)
RegisterMethod(MathMethods, "tan", mathTan)
RegisterMethod(MathMethods, "min", mathMin)

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

// mathMin returns the smallest number of the referenced numbers.
func mathMin(scope *object.Scope, tok token.Token, args ...object.Object) object.Object {
if len(args) < 2 {
panic("math.min requires at least two arguments")
}

if args[0].Type() != object.NUMBER {
return nil
}

if args[1].Type() != object.NUMBER {
return nil
}

number1 := args[0].(*object.Number)
number2 := args[1].(*object.Number)

if number1.Value.LessThan(number2.Value) {
return number1
}

return number2
}

// Properties

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

0 comments on commit d660e08

Please sign in to comment.