{"record":{"id":"abd733717e62fd68","repo":"dgraph-io/dgraph","slug":"division-by-zero-abd733","errorCode":null,"errorMessage":"Division by zero","messagePattern":"Division by zero","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":28,"sourceCode":"\n\t\"github.com/golang/glog\"\n\t\"github.com/pkg/errors\"\n\n\t\"github.com/dgraph-io/dgraph/v25/types\"\n)\n\ntype mathTree struct {\n\tFn    string\n\tVar   string\n\tConst types.Val // If its a const value node.\n\tVal   *types.ShardedMap\n\tChild []*mathTree\n}\n\nvar (\n\tErrorIntOverflow     = errors.New(\"Integer overflow\")\n\tErrorFloat32Overflow = errors.New(\"Float32 overflow\")\n\tErrorDivisionByZero  = errors.New(\"Division by zero\")\n\tErrorFractionalPower = errors.New(\"Fractional power of negative number\")\n\tErrorNegativeLog     = errors.New(\"Log of negative number\")\n\tErrorNegativeRoot    = errors.New(\"Root of negative number\")\n\tErrorVectorsNotMatch = errors.New(\"The length of vectors must match\")\n\tErrorArgsDisagree    = errors.New(\"Left and right arguments must match\")\n\tErrorShouldBeVector  = errors.New(\"Type should be []float, but is not. Cannot determine type.\")\n\tErrorBadVectorMult   = errors.New(\"Cannot multiply vector by vector\")\n)\n\n// processBinary handles the binary operands like\n// +, -, *, /, %, max, min, logbase, dot\nfunc processBinary(mNode *mathTree) error {\n\taggName := mNode.Fn\n\n\tmpl := mNode.Child[0].Val\n\tmpr := mNode.Child[1].Val\n\tcl := mNode.Child[0].Const\n\tcr := mNode.Child[1].Const","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L10-L46","documentation":"ErrorDivisionByZero is a sentinel error in Dgraph's math package returned when a division or modulus operation has a zero divisor, or when logbase/log is called with an invalid base of 1 (or log of 1 yielding division by zero). The query fails for the affected value.","triggerScenarios":"math(a / b) or math(a % b) where b evaluates to 0; applyLog when base is 1; returned by applyDiv, applyMod, applyLog.","commonSituations":"Computing ratios where a count predicate is missing/zero on some nodes; percentage math over sparse data; user-driven math templates without zero guards.","solutions":["Filter out zero denominators first, e.g. add a filter func: gt(denominator, 0) on the affected nodes","Use a conditional-free reformulation, e.g. add a tiny epsilon: math(a / (b + 1e-9))","Handle the sentinel error at the client/transport layer and skip or default the affected results"],"exampleFix":"// before\nvalue @filter(type(Metric)) { ratio as math(x / y) }\n// after\nvalue @filter(type(Metric) AND gt(y, 0)) { ratio as math(x / y) }","handlingStrategy":"validation","validationCode":"// Add a filter so the divisor can never be zero:\n// { q(func: type(Metric)) @filter(gt(y, 0)) { ratio as math(x / y) } }\nconst query = `{ q(func: type(Metric)) @filter(gt(y, 0)) { x\\n y\\n ratio as math(x / y) } }`;","typeGuard":null,"tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(query);\n} catch (e) {\n  if (String(e).includes('Division by zero')) {\n    // add gt(divisor, 0) filter or epsilon and retry\n  } else throw e;\n}","preventionTips":["Always filter gt(denominator, 0) before ratio math","Use an epsilon (math(x / (y + 1e-9))) when zeros are legitimate data","Remember % and logbase can also raise this error"],"tags":["dgraph","math","division-by-zero","query"],"backgroundTag":"division-by-zero","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}