dgraph-io/dgraph · error

Float32 overflow

Error message

Float32 overflow

What it means

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.

Source

Thrown at query/math.go:27

	"sync"

	"github.com/golang/glog"
	"github.com/pkg/errors"

	"github.com/dgraph-io/dgraph/v25/types"
)

type mathTree struct {
	Fn    string
	Var   string
	Const types.Val // If its a const value node.
	Val   *types.ShardedMap
	Child []*mathTree
}

var (
	ErrorIntOverflow     = errors.New("Integer overflow")
	ErrorFloat32Overflow = errors.New("Float32 overflow")
	ErrorDivisionByZero  = errors.New("Division by zero")
	ErrorFractionalPower = errors.New("Fractional power of negative number")
	ErrorNegativeLog     = errors.New("Log of negative number")
	ErrorNegativeRoot    = errors.New("Root of negative number")
	ErrorVectorsNotMatch = errors.New("The length of vectors must match")
	ErrorArgsDisagree    = errors.New("Left and right arguments must match")
	ErrorShouldBeVector  = errors.New("Type should be []float, but is not. Cannot determine type.")
	ErrorBadVectorMult   = errors.New("Cannot multiply vector by vector")
)

// processBinary handles the binary operands like
// +, -, *, /, %, max, min, logbase, dot
func processBinary(mNode *mathTree) error {
	aggName := mNode.Fn

	mpl := mNode.Child[0].Val
	mpr := mNode.Child[1].Val
	cl := mNode.Child[0].Const

View on GitHub (pinned to 759e242be6)

Solutions

  1. Scale operands down before multiplying or up before dividing to keep results within float32 range
  2. Restructure the math expression to avoid extreme intermediates (e.g. multiply then divide in different order)
  3. Store the data at appropriate units to avoid huge magnitudes

Example fix

// before
math(huge / 1e-45)
// after
math(huge * 1e45) // equivalent, avoids float32 overflow via division by denormal
Defensive patterns

Strategy: try-catch

Validate before calling

// Check expected float32 range before querying:
function withinFloat32(x) {
  return Number.isFinite(x) && Math.abs(x) <= 3.4028235e38;
}

Try / catch

try {
  const res = await dgraph.newTxn().query(`{ q(func: uid(${id})) { v as math(a / b) } }`);
} catch (e) {
  if (String(e).includes('Float32 overflow')) {
    // rescale operands and retry
  } else throw e;
}

Prevention

When it happens

Trigger: math(a * b) or math(a / b) with float operands whose result magnitude exceeds float32 max; returned by applyMul and applyDiv.

Common situations: Dividing by a very small float constant; multiplying extremely large measurements (e.g. scientific data); cascading exponentials via repeated math operations.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/967249d122dadec4. Report an issue: GitHub.