BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: victor(at)magic(dot)io
Subject: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Date: 2018-11-23 22:03:56
Message-ID: 15519-4fc785b483201ff1@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 15519
Logged by: Victor Petrovykh
Email address: victor(at)magic(dot)io
PostgreSQL version: 10.5
Operating system: Gentoo Linux 4.14.20
Description:

Offending examples:
SELECT ((2147483647::float4) - 1.0::float4)::int4;
SELECT ((2147483590::float4) - 1.0::float4)::int4;
SELECT ((2147483647::float4) + 1.0::float4)::int4;

They all produce the same result: -2147483648

I understand that a float4 cannot represent large integers with the same
precision as int4, that's OK. What surprised me is that instead of getting
an "overflow error" or "integer out of range" I simply got a negative result
for a value that is actually close to maximum int4. To contrast this, the
query:
SELECT ((2147483647::float4) + 200.0::float4)::int4;
The above produces the expected "ERROR: integer out of range"

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrew Gierth 2018-11-23 22:21:07 Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Previous Message Tom Lane 2018-11-23 21:57:18 Re: BUG #15517: JSONB_BUILD_ARRAY and JSON_BUILD_ARRAY omit XML Declaration (if present) from XML colums