Skip to content

Read NaN and Infinity float values instead of 0.0 - #78

Open
jwage wants to merge 1 commit into
amphp:2.xfrom
jwage:fix-non-finite-floats
Open

jwage wants to merge 1 commit into
amphp:2.xfrom
jwage:fix-non-finite-floats

Conversation

@jwage

@jwage jwage commented Nov 28, 2026

Copy link
Copy Markdown

Both drivers read the non-finite values of float4 and float8 as 0.0:

$row = $connection->query("SELECT 'NaN'::float8 AS a, 'Infinity'::float8 AS b, '-Infinity'::float4 AS c")->fetchRow();
// before: ['a' => 0.0, 'b' => 0.0, 'c' => 0.0]
// after:  ['a' => NAN, 'b' => INF, 'c' => -INF]

Nothing fails, so the wrong number is easy to miss.

Cause and fix

PostgreSQL writes these values as the words NaN, Infinity and -Infinity.

  • ext-pgsql: PgSqlResultIterator cast them with (float). It now maps the three words first.
  • ext-pq: its own float conversion has the same result, so PqHandle registers a pq\Converter for the two float types. It applies to array elements too.

NaN is returned with fdiv(0, 0) because Psalm 6.15.1 on PHP 8.5 crashed analysing the NAN constant ("unexpected NAN value was coerced to string").

Tests

The new test covers scalars and a float8[] for both drivers, and fails on 2.x. The suite passes locally on the event and stream_select drivers, and Psalm and php-cs-fixer report nothing new. With the ev driver the pgsql tests are skipped or fail on the existing pecl-ev check, as on 2.x.

🤖 Generated with Claude Code

PostgreSQL writes the non-finite values of float4 and float8 as "NaN", "Infinity" and "-Infinity". Both drivers read
them as 0.0: PgSqlResultIterator cast them with (float), and ext-pq's own conversion does the same.

The pgsql iterator now maps the three words, and the pq connection registers a converter for the two float types.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant