dgraph-io/dgraph · error
Fractional power of negative number
Error message
Fractional power of negative number
What it means
ErrorFractionalPower is a sentinel error in Dgraph's math package returned by applyPow when a negative base is raised to a fractional exponent, which has no real-valued result. Dgraph does not return complex numbers, so the operation is rejected.
Source
Thrown at query/math.go:29
"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
cr := mNode.Child[1].Const
View on GitHub (pinned to 759e242be6)
Solutions
- Guard the base with abs(): math(abs(x) ^ 0.5) if sign is irrelevant
- Filter out negative bases before the math block using @filter(ge(x, 0))
- Use an integer exponent when working with negative bases
Example fix
// before math(x ^ 0.5) // after math(abs(x) ^ 0.5)
Defensive patterns
Strategy: validation
Validate before calling
// Filter non-negative bases before pow:
// { q(func: type(M)) @filter(ge(x, 0)) { r as math(x ^ 0.5) } }
const query = `{ q(func: type(M)) @filter(ge(x, 0)) { r as math(x ^ 0.5) } }`; Try / catch
try {
const res = await dgraph.newTxn().query(query);
} catch (e) {
if (String(e).includes('Fractional power')) {
// retry with math(abs(x) ^ exp)
} else throw e;
} Prevention
- Wrap potentially negative bases in abs() when using fractional exponents
- Prefer sqrt() with abs over x ^ 0.5 on signed data
- Validate data signs upstream
When it happens
Trigger: math(a ^ b) where a < 0 and b is non-integral (e.g. math(x ^ 0.5) on negative x); returned by applyPow.
Common situations: Taking fractional roots (sqrt, cube root) of signed data that can be negative; using pow on normalized values that dipped below zero due to upstream bugs.
Related errors
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/7e162034a616175e.
Report an issue: GitHub.