jackc/pgx · error

cannot convert NaN to integer

Error message

cannot convert NaN to integer

What it means

Numeric.toBigInt refuses to convert special values to integers; this branch fires specifically when the Numeric's NaN flag is set — 'not a number' has no integer representation. Callers like Int64Value and int64 scans surface it directly. (The listed RAISED-IN attribution to ScanScientific is incidental; the guard lives in toBigInt.)

Solutions

  1. Check Numeric.NaN before attempting integer conversion and handle it separately
  2. Filter or reject NaN values at the query level with IS NOT NULL-style predicates / WHERE col <> 'NaN'::numeric
  3. Keep the value as Numeric rather than converting to an integer type
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pgtype/numeric.go:152 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/75d3c0dd36bb5829. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/numeric.go:152

	if bigF, ok := new(big.Float).SetString(src); ok {
		smallF, _ := bigF.Float64()
		src = strconv.FormatFloat(smallF, 'f', -1, 64)
	}

	num, exp, err := parseNumericString(src)
	if err != nil {
		return err
	}

	*n = Numeric{Int: num, Exp: exp, Valid: true}

	return nil
}

func (n *Numeric) toBigInt() (*big.Int, error) {
	if n.NaN {
		return nil, fmt.Errorf("cannot convert NaN to integer")
	} else if n.InfinityModifier != Finite {
		return nil, fmt.Errorf("cannot convert %v to integer", n.InfinityModifier)
	}

	if n.Int == nil {
		return big.NewInt(0), nil
	}

	if n.Exp == 0 {
		return n.Int, nil
	}

	num := &big.Int{}
	num.Set(n.Int)
	if n.Exp > 0 {
		mul := &big.Int{}
		mul.Exp(big10, big.NewInt(int64(n.Exp)), nil)
		num.Mul(num, mul)

View on GitHub (pinned to ec1a0befd2)