Skip to content

Commit

Permalink
Merge pull request #211 from pratikmota/mathfunc-part-5
Browse files Browse the repository at this point in the history
[Adding math function] Support for Dim, Copysign functions
  • Loading branch information
refaktor authored May 14, 2024
2 parents 5871a5c + 990288e commit 527dfea
Show file tree
Hide file tree
Showing 3 changed files with 83 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
17 changes: 17 additions & 0 deletions tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,23 @@ section "Math functions"
equal { do\in math { cbrt 27 } } 3.000000
equal { do\in math { cbrt 12.3 } } 2.308350239753609
}

group "copysign"
mold\nowrap ""
{ { string } }
{
equal { do\in math { copysign 4.9 -1 } } -4.9
equal { do\in math { copysign 4.9 1 } } 4.9
}

group "dim"
mold\nowrap ""
{ { string } }
{
equal { do\in math { dim 2 -3 } } 5.000000
equal { do\in math { dim 8 -1 } } 9.000000
equal { do\in math { dim -4 -2 } } 0.000000
}

}

Expand Down
9 changes: 9 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ func TruncateString(s string, maxLen int) string {
return string(runes[0:maxLen-3]) + "..."
}

// GetDimValue get max x-y or 0 value
func GetDimValue(x, y float64) float64 {
difference := x - y
if difference > 0 {
return difference
}
return 0
}

/*
func RemoveDuplicate[T comparable](sliceList []T) []T {
allKeys := make(map[T]bool)
Expand Down

0 comments on commit 527dfea

Please sign in to comment.