added operator './' (float division) and test
This commit is contained in:
@@ -91,8 +91,43 @@ func evalDivide(ctx exprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//-------- divide as float term
|
||||
|
||||
func newDivideAsFloatTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
class: classOperator,
|
||||
kind: kindUnknown,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priProduct,
|
||||
evalFunc: evalDivideAsFloat,
|
||||
}
|
||||
}
|
||||
|
||||
func evalDivideAsFloat(ctx exprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if isNumber(leftValue) && isNumber(rightValue) {
|
||||
d := numAsFloat(rightValue)
|
||||
if d == 0.0 {
|
||||
err = errors.New("division by zero")
|
||||
} else {
|
||||
v = numAsFloat(leftValue) / d
|
||||
}
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymStar, newMultiplyTerm)
|
||||
registerTermConstructor(SymSlash, newDivideTerm)
|
||||
registerTermConstructor(SymDotSlash, newDivideAsFloatTerm)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user