{"record":{"id":"967249d122dadec4","repo":"dgraph-io/dgraph","slug":"float32-overflow","errorCode":null,"errorMessage":"Float32 overflow","messagePattern":"Float32 overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":27,"sourceCode":"\t\"sync\"\n\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","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L9-L45","documentation":"ErrorFloat32Overflow is a sentinel error in Dgraph's math package returned when a float computation produces a value beyond float32 range (approximately ±3.4e38), which Dgraph uses for float storage. applyMul and applyDiv return it when the result cannot be represented.","triggerScenarios":"math(a * b) or math(a / b) with float operands whose result magnitude exceeds float32 max; returned by applyMul and applyDiv.","commonSituations":"Dividing by a very small float constant; multiplying extremely large measurements (e.g. scientific data); cascading exponentials via repeated math operations.","solutions":["Scale operands down before multiplying or up before dividing to keep results within float32 range","Restructure the math expression to avoid extreme intermediates (e.g. multiply then divide in different order)","Store the data at appropriate units to avoid huge magnitudes"],"exampleFix":"// before\nmath(huge / 1e-45)\n// after\nmath(huge * 1e45) // equivalent, avoids float32 overflow via division by denormal","handlingStrategy":"try-catch","validationCode":"// Check expected float32 range before querying:\nfunction withinFloat32(x) {\n  return Number.isFinite(x) && Math.abs(x) <= 3.4028235e38;\n}","typeGuard":null,"tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(`{ q(func: uid(${id})) { v as math(a / b) } }`);\n} catch (e) {\n  if (String(e).includes('Float32 overflow')) {\n    // rescale operands and retry\n  } else throw e;\n}","preventionTips":["Keep operands in a sane unit scale before math blocks","Avoid dividing by tiny float constants","Test math expressions with extreme data values"],"tags":["dgraph","math","float-overflow","query"],"backgroundTag":"float-overflow","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}