Skip to content

Commit

Permalink
adding dim and copysign function
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikmota committed May 12, 2024
1 parent 5871a5c commit 6e0f4a1
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"

"github.com/refaktor/rye/env"
"github.com/refaktor/rye/util"
)

// Integer represents an integer.
Expand Down Expand Up @@ -341,6 +342,62 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"copysign": {
Argsn: 2,
Doc: "Copysign returns a value with the magnitude of arg1 and the sign of arg2.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Integer:
switch val2 := arg1.(type) {
case env.Integer:
return *env.NewDecimal(math.Copysign(float64(val.Value), float64(val2.Value)))
case env.Decimal:
return *env.NewDecimal(math.Copysign(float64(val.Value), val2.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "copysign")
}
case env.Decimal:
switch val2 := arg1.(type) {
case env.Integer:
return *env.NewDecimal(math.Copysign(val.Value, float64(val2.Value)))
case env.Decimal:
return *env.NewDecimal(math.Copysign(val.Value, val2.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "copysign")
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "copysign")
}
},
},
"dim": {
Argsn: 2,
Doc: "Dim returns the maximum of arg1-arg2 or 0.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Integer:
switch val2 := arg1.(type) {
case env.Integer:
return *env.NewDecimal(util.GetDimValue(float64(val.Value), float64(val2.Value)))
case env.Decimal:
return *env.NewDecimal(util.GetDimValue(float64(val.Value), val2.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "dim")
}
case env.Decimal:
switch val2 := arg1.(type) {
case env.Integer:
return *env.NewDecimal(util.GetDimValue(val.Value, float64(val2.Value)))
case env.Decimal:
return *env.NewDecimal(util.GetDimValue(val.Value, val2.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "dim")
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "dim")
}
},
},
"pi": {
Argsn: 0,
Doc: "Return Pi constant.",
Expand Down

0 comments on commit 6e0f4a1

Please sign in to comment.