Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: fix 0/0
Browse files Browse the repository at this point in the history
Should be error, not NaN.

Closes #84

Change-Id: I061b7e32b069c1fea3123fb981a4eddf87c61910
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3044
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed Sep 3, 2019
1 parent 18637db commit 34988fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
15 changes: 9 additions & 6 deletions cue/binop.go
Expand Up @@ -956,30 +956,33 @@ func (x *numLit) binOp(ctx *context, src source, op op, other evaluated) evaluat
case opMul:
_, _ = ctx.Mul(&n.v, &x.v, &y.v)
case opQuo:
cond, _ := ctx.Quo(&n.v, &x.v, &y.v)
cond, err := ctx.Quo(&n.v, &x.v, &y.v)
if err != nil {
return ctx.mkErr(src, err.Error())
}
if cond.DivisionByZero() {
return ctx.mkErr(src, "divide by zero")
return ctx.mkErr(src, "division by zero")
}
_, _, _ = ctx.Reduce(&n.v, &n.v)
n.k = floatKind
case opIDiv:
if y.v.IsZero() {
return ctx.mkErr(src, "divide by zero")
return ctx.mkErr(src, "division by zero")
}
intOp(ctx, n, (*big.Int).Div, x, y)
case opIMod:
if y.v.IsZero() {
return ctx.mkErr(src, "divide by zero")
return ctx.mkErr(src, "division by zero")
}
intOp(ctx, n, (*big.Int).Mod, x, y)
case opIQuo:
if y.v.IsZero() {
return ctx.mkErr(src, "divide by zero")
return ctx.mkErr(src, "division by zero")
}
intOp(ctx, n, (*big.Int).Quo, x, y)
case opIRem:
if y.v.IsZero() {
return ctx.mkErr(src, "divide by zero")
return ctx.mkErr(src, "division by zero")
}
intOp(ctx, n, (*big.Int).Rem, x, y)
}
Expand Down
15 changes: 12 additions & 3 deletions cue/resolve_test.go
Expand Up @@ -137,8 +137,14 @@ func TestBasicRewrite(t *testing.T) {
div1: 2.0 / 3 * 6 // 4
div2: 2 / 3 * 6 // 4
divZero: 1.0 / 0
div00: 0 / 0
b: 1 != 4
idiv00: 0 div 0
imod00: 0 mod 0
iquo00: 0 quo 0
irem00: 0 rem 0
v1: 1.0T/2.0
v2: 2.0 == 2
v3: 2.0/3.0
Expand All @@ -159,8 +165,13 @@ func TestBasicRewrite(t *testing.T) {
`sum: 1, ` +
`div1: 4.00000000000000000000000, ` +
`div2: 4.00000000000000000000000, ` +
`divZero: _|_((1.0 / 0):divide by zero), ` +
`divZero: _|_((1.0 / 0):division by zero), ` +
`div00: _|_((0 / 0):division undefined), ` +
`b: true, ` +
`idiv00: _|_((0 div 0):division by zero), ` +
`imod00: _|_((0 mod 0):division by zero), ` +
`iquo00: _|_((0 quo 0):division by zero), ` +
`irem00: _|_((0 rem 0):division by zero), ` +
`v1: 5e+11, ` +
`v2: true, ` +
`v3: 0.666666666666666666666667, ` +
Expand Down Expand Up @@ -205,8 +216,6 @@ func TestBasicRewrite(t *testing.T) {
m4: -5 mod -2 // 1
me1: 2.0 mod 1
me2: 2 mod 1.0
// TODO: handle divide by zero
`,
out: `<0>{q1: 2, q2: -2, q3: -2, q4: 2, ` +
`qe1: _|_((2.0 quo 1):invalid operation 2.0 quo 1 (mismatched types float and int)), ` +
Expand Down
5 changes: 4 additions & 1 deletion cue/types_test.go
Expand Up @@ -342,7 +342,7 @@ func TestFloat(t *testing.T) {
float64: 0,
prec: 2,
fmt: 'f',
err: "divide by zero",
err: "division by zero",
kind: BottomKind,
}, {
value: "1.797693134862315708145274237317043567982e+308",
Expand Down Expand Up @@ -1754,6 +1754,9 @@ func TestMashalJSON(t *testing.T) {
}, {
value: `3.0e100`,
json: `3.0E+100`,
}, {
value: `0/0`,
err: "division undefined",
}, {
value: `[]`,
json: `[]`,
Expand Down

0 comments on commit 34988fa

Please sign in to comment.