operator-{sum,prod}.go: Fixed sum and prod operation with a fraction and a float

This commit is contained in:
Celestino Amoroso 2024-05-06 17:32:39 +02:00
parent 8b4dad1381
commit 5910345c08
2 changed files with 10 additions and 10 deletions

View File

@ -34,16 +34,16 @@ func evalMultiply(ctx ExprContext, self *term) (v any, err error) {
s, _ := leftValue.(string) s, _ := leftValue.(string)
n, _ := rightValue.(int64) n, _ := rightValue.(int64)
v = strings.Repeat(s, int(n)) v = strings.Repeat(s, int(n))
} else if isNumber(leftValue) && isNumber(rightValue) { } else if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
if isFloat(leftValue) || isFloat(rightValue) { if isFloat(leftValue) || isFloat(rightValue) {
v = numAsFloat(leftValue) * numAsFloat(rightValue) v = numAsFloat(leftValue) * numAsFloat(rightValue)
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = mulAnyFract(leftValue, rightValue)
} else { } else {
leftInt, _ := leftValue.(int64) leftInt, _ := leftValue.(int64)
rightInt, _ := rightValue.(int64) rightInt, _ := rightValue.(int64)
v = leftInt * rightInt v = leftInt * rightInt
} }
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = mulAnyFract(leftValue, rightValue)
} else { } else {
err = self.errIncompatibleTypes(leftValue, rightValue) err = self.errIncompatibleTypes(leftValue, rightValue)
} }
@ -71,7 +71,7 @@ func evalDivide(ctx ExprContext, self *term) (v any, err error) {
return return
} }
if isNumber(leftValue) && isNumber(rightValue) { if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
if isFloat(leftValue) || isFloat(rightValue) { if isFloat(leftValue) || isFloat(rightValue) {
d := numAsFloat(rightValue) d := numAsFloat(rightValue)
if d == 0.0 { if d == 0.0 {
@ -79,6 +79,8 @@ func evalDivide(ctx ExprContext, self *term) (v any, err error) {
} else { } else {
v = numAsFloat(leftValue) / d v = numAsFloat(leftValue) / d
} }
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = divAnyFract(leftValue, rightValue)
} else { } else {
leftInt, _ := leftValue.(int64) leftInt, _ := leftValue.(int64)
if rightInt, _ := rightValue.(int64); rightInt == 0 { if rightInt, _ := rightValue.(int64); rightInt == 0 {
@ -87,8 +89,6 @@ func evalDivide(ctx ExprContext, self *term) (v any, err error) {
v = leftInt / rightInt v = leftInt / rightInt
} }
} }
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = divAnyFract(leftValue, rightValue)
} else { } else {
err = self.errIncompatibleTypes(leftValue, rightValue) err = self.errIncompatibleTypes(leftValue, rightValue)
} }
@ -114,7 +114,7 @@ func evalDivideAsFloat(ctx ExprContext, self *term) (v any, err error) {
return return
} }
if isNumber(leftValue) && isNumber(rightValue) { if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
d := numAsFloat(rightValue) d := numAsFloat(rightValue)
if d == 0.0 { if d == 0.0 {
err = errors.New("division by zero") err = errors.New("division by zero")

View File

@ -86,9 +86,11 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) {
return return
} }
if isNumber(leftValue) && isNumber(rightValue) { if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
if isFloat(leftValue) || isFloat(rightValue) { if isFloat(leftValue) || isFloat(rightValue) {
v = numAsFloat(leftValue) - numAsFloat(rightValue) v = numAsFloat(leftValue) - numAsFloat(rightValue)
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = subAnyFract(leftValue, rightValue)
} else { } else {
leftInt, _ := leftValue.(int64) leftInt, _ := leftValue.(int64)
rightInt, _ := rightValue.(int64) rightInt, _ := rightValue.(int64)
@ -104,8 +106,6 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) {
} }
} }
v = &diffList v = &diffList
} else if isFraction(leftValue) || isFraction(rightValue) {
v, err = subAnyFract(leftValue, rightValue)
} else { } else {
err = self.errIncompatibleTypes(leftValue, rightValue) err = self.errIncompatibleTypes(leftValue, rightValue)
} }