{"record":{"id":"7e162034a616175e","repo":"dgraph-io/dgraph","slug":"fractional-power-of-negative-number","errorCode":null,"errorMessage":"Fractional power of negative number","messagePattern":"Fractional power of negative number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":29,"sourceCode":"\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\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L11-L47","documentation":"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.","triggerScenarios":"math(a ^ b) where a < 0 and b is non-integral (e.g. math(x ^ 0.5) on negative x); returned by applyPow.","commonSituations":"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.","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"],"exampleFix":"// before\nmath(x ^ 0.5)\n// after\nmath(abs(x) ^ 0.5)","handlingStrategy":"validation","validationCode":"// Filter non-negative bases before pow:\n// { q(func: type(M)) @filter(ge(x, 0)) { r as math(x ^ 0.5) } }\nconst query = `{ q(func: type(M)) @filter(ge(x, 0)) { r as math(x ^ 0.5) } }`;","typeGuard":null,"tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(query);\n} catch (e) {\n  if (String(e).includes('Fractional power')) {\n    // retry with math(abs(x) ^ exp)\n  } else throw e;\n}","preventionTips":["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"],"tags":["dgraph","math","pow","query"],"backgroundTag":"invalid-math-domain","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}