dgraph-io/dgraph · error

Log of negative number

Error message

Log of negative number

What it means

ErrorNegativeLog is a sentinel error in Dgraph's math package returned by applyLog and applyLn when the logarithm argument (or, for logbase, the value) is <= 0, since log is undefined for non-positive real numbers.

Source

Thrown at query/math.go:30

	"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
	cr := mNode.Child[1].Const

	f := func(k uint64, lshard, rshard, destMapi *map[uint64]types.Val) error {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Filter non-positive values first: @filter(gt(x, 0)) before the math block
  2. Use math(log(x + 1)) (log1p style) for data that can be zero
  3. Apply abs() if only the magnitude matters

Example fix

// before
math(ln(x))
// after
math(ln(x + 1))
Defensive patterns

Strategy: validation

Validate before calling

// Filter positive values before log:
// { q(func: type(M)) @filter(gt(x, 0)) { l as math(ln(x)) } }
const query = `{ q(func: type(M)) @filter(gt(x, 0)) { l as math(ln(x)) } }`;

Try / catch

try {
  const res = await dgraph.newTxn().query(query);
} catch (e) {
  if (String(e).includes('Log of negative')) {
    // retry with math(ln(x + 1)) or after filtering
  } else throw e;
}

Prevention

When it happens

Trigger: math(log(a)) or math(ln(a)) where a <= 0; math(logbase(a, b)) with a <= 0; returned by applyLog and applyLn.

Common situations: Applying log scaling to metrics that can be zero or negative (deltas, temperatures in C); sparse predicates defaulting to 0; data corruption producing negatives.

Related errors


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