From d77dc25af4149487da823dd3b4588fb9cf170380 Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Mon, 29 Dec 2025 13:58:57 -0800 Subject: [PATCH] sam: Divide by float 0 returns +/-Inf --- book/src/command/super.md | 10 +++++----- runtime/sam/expr/eval.go | 6 +----- runtime/ztests/expr/arith-divide-by-zero.yaml | 6 ++++++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/book/src/command/super.md b/book/src/command/super.md index 3f624a2624..5cb335f268 100644 --- a/book/src/command/super.md +++ b/book/src/command/super.md @@ -500,18 +500,18 @@ or trying to debug a halted query with a vague error message. For example, this query ```mdtest-command -echo '1 2 0 3' | super -s -c '10.0/this' - +echo '1 2 0 5' | super -s -c '10/this' - ``` produces ```mdtest-output -10. -5. +10 +5 error("divide by zero") -3.3333333333333335 +2 ``` and ```mdtest-command -echo '1 2 0 3' | super -c '10.0/this' - | super -s -c 'is_error(this)' - +echo '1 2 0 5' | super -c '10/this' - | super -s -c 'is_error(this)' - ``` produces just ```mdtest-output diff --git a/runtime/sam/expr/eval.go b/runtime/sam/expr/eval.go index c3893e913c..483c65484a 100644 --- a/runtime/sam/expr/eval.go +++ b/runtime/sam/expr/eval.go @@ -522,11 +522,7 @@ func (d *Divide) Eval(this super.Value) super.Value { } return super.NewInt(typ, toInt(lhsVal)/v) case super.IsFloat(id): - v := toFloat(rhsVal) - if v == 0 { - return d.sctx.NewError(DivideByZero) - } - return super.NewFloat(typ, toFloat(lhsVal)/v) + return super.NewFloat(typ, toFloat(lhsVal)/toFloat(rhsVal)) } return d.sctx.NewErrorf("type %s incompatible with '/' operator", sup.FormatType(typ)) } diff --git a/runtime/ztests/expr/arith-divide-by-zero.yaml b/runtime/ztests/expr/arith-divide-by-zero.yaml index c18f4d6559..27f8c214dd 100644 --- a/runtime/ztests/expr/arith-divide-by-zero.yaml +++ b/runtime/ztests/expr/arith-divide-by-zero.yaml @@ -6,6 +6,8 @@ input: | {a:null::uint64,b:0::uint64} {a:2::uint64,b:null::uint64} {a:5::uint64,b:0::uint64} + {a:1.,b:0.} + {a:-1.,b:0.} output: | null::uint64 @@ -14,3 +16,7 @@ output: | null::uint64 error("divide by zero") error("divide by zero") + +Inf + error("type float64 incompatible with '%' operator") + -Inf + error("type float64 incompatible with '%' operator")