{"record":{"id":"5473638afc07cde9","repo":"dgraph-io/dgraph","slug":"unsupported-floating-point-number-in-float-field","errorCode":null,"errorMessage":"Unsupported floating point number in float field","messagePattern":"Unsupported floating point number in float field","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/outputnode.go","lineNumber":670,"sourceCode":"\tcase types.IntID:\n\t\t// In types.Convert(), we always convert to int64 for IntID type. fmt.Sprintf is slow\n\t\t// and hence we are using strconv.FormatInt() here. Since int64 and int are most common int\n\t\t// types we are using FormatInt for those.\n\t\tswitch num := v.Value.(type) {\n\t\tcase int64:\n\t\t\treturn []byte(strconv.FormatInt(num, 10)), nil\n\t\tcase int:\n\t\t\treturn []byte(strconv.FormatInt(int64(num), 10)), nil\n\t\tdefault:\n\t\t\treturn []byte(fmt.Sprintf(\"%d\", v.Value)), nil\n\t\t}\n\tcase types.FloatID:\n\t\tf, fOk := v.Value.(float64)\n\n\t\t// +Inf, -Inf and NaN are not representable in JSON.\n\t\t// Please see https://golang.org/src/encoding/json/encode.go?s=6458:6501#L573\n\t\tif !fOk || math.IsInf(f, 0) || math.IsNaN(f) {\n\t\t\treturn nil, errors.New(\"Unsupported floating point number in float field\")\n\t\t}\n\n\t\treturn []byte(fmt.Sprintf(\"%g\", f)), nil\n\tcase types.BoolID:\n\t\tif v.Value.(bool) {\n\t\t\treturn boolTrue, nil\n\t\t}\n\t\treturn boolFalse, nil\n\tcase types.DateTimeID:\n\t\tt := v.Value.(time.Time)\n\t\treturn marshalTimeJson(t)\n\tcase types.GeoID:\n\t\treturn geojson.Marshal(v.Value.(geom.T))\n\tcase types.BigFloatID:\n\t\tb := v.Value.(big.Float)\n\t\treturn b.MarshalText()\n\tcase types.UidID:\n\t\treturn []byte(fmt.Sprintf(\"\\\"%#x\\\"\", v.Value)), nil","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/outputnode.go#L652-L688","documentation":"Dgraph serializes query results to JSON, and JSON has no representation for IEEE-754 special values. When valToBytes encounters a float field whose value is NaN, +Inf, -Inf, or is not actually a float64, it refuses to emit invalid JSON and returns this error.","triggerScenarios":"A predicate with float schema stores or computes NaN/Inf — e.g. result of math operations like log(-1), division by zero on floats, or a value unmarshalled into a non-float64 Go type before reaching valToBytes via getObjectVal/AddListValue.","commonSituations":"Math-heavy queries (math.sqrt of negatives, math.log(0)) in Dgraph; client writes NaN via JSON (Go encoding/json normally rejects it, but custom mutation paths may not); corrupted or wrongly-typed stored values after schema changes.","solutions":["Fix the query math to avoid NaN/Inf: guard math.log(0), math.sqrt(-1), and division by zero with cond or math.isNaN-style checks in the query","Sanitize data before mutation: reject or clamp NaN/Inf float values at write time","Check the schema: ensure the predicate is declared float and the value written is a JSON number, not string or object","If the value came from a stored document, re-mutate the offending node with a valid finite float"],"exampleFix":"// before (query)\nroot as var(func: eq(score, math.log(0)))\n// after\nscores as var(func: has(score))\nq(func: uid(scores)) @filter(gt(score, 0.0)) {\n  safeScore as score\n}","handlingStrategy":"validation","validationCode":"const v = Number(value);\nif (Number.isNaN(v) || !Number.isFinite(v)) throw new Error(`float field must be finite, got ${value}`);","typeGuard":"function isFiniteNumber(v: unknown): v is number {\n  return typeof v === 'number' && Number.isFinite(v);\n}","tryCatchPattern":"try {\n  const res = await dgraph.query(q);\n} catch (e) {\n  if (String(e).includes('Unsupported floating point number in float field')) {\n    // sanitize offending math and retry without NaN-producing expressions\n  }\n  throw e;\n}","preventionTips":["Never write NaN/Infinity literals via mutations; validate floats client-side before sending","Avoid math.log(0), math.sqrt of negatives, and float division by zero in Dgraph math expressions","Add @filter bounds on float predicates used in math computations","After schema changes, scan data for non-finite stored floats"],"tags":["json","serialization","float","dgraph"],"backgroundTag":"nan-inf-not-json-representable","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}