Re: BUG #15519: Casting float4 into int4 gets the wrong sign instead of "integer out of range" error
Victor Petrovykh <victor@magic.io>
From: Victor Petrovykh <victor@magic.io>
To: andrew@tao11.riddles.org.uk
Cc: tgl@sss.pgh.pa.us, pgsql-bugs@lists.postgresql.org
Date: 2018-11-24T01:16:23Z
Lists: pgsql-bugs
I'm sorry I'm kinda new here. Am I missing something in thinking that the cast of a float to int should produce an error if the sign of the original value doesn't match the sign of the cast result? Presumably you have access to both values at some point and checking sign bits for sameness should be simple. Also, I would say that doing some float4 arithmetic and then casting into int4 or int8 should be consistent, so if SELECT (54610::float4 * 39324::float4)::int8; gets me 2147483648, then SELECT (54610::float4 * 39324::float4)::int4; should be an error rather than the unintuitive -2147483648. The scenario that I'm pointing at is basically first doing some float4 arithmetic, casting the result into an int4 and expecting that the result (if it was without error) is actually about as close to the original float value as float4 precision would normally allow. On Fri, Nov 23, 2018 at 7:45 PM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote: > >>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes: > > Tom> if (unlikely(num < (float4) INT_MIN || num >= (float4) INT_MAX || > isnan(num))) > > >> if (num < (float4)INT_MIN || num >= -(float4)INT_MIN || ... > > Tom> Meh. Seems to me that's relying on pretty much the same > Tom> assumptions > > No, because we know that INT_MIN is always exactly representable as a > float (whereas INT_MAX is not), and therefore the cast result will not > depend on any rounding choices whether at compile or run time. Nor does > it depend on knowing that float4 can't represent INT_MAX exactly. > > -- > Andrew (irc:RhodiumToad) >
Commits
-
Update additional float4/8 expected-output files.
- bf9fb00dd107 9.4.21 landed
- 74587148314a 10.7 landed
- 3645d31934f4 11.2 landed
- 298510caee63 9.5.16 landed
- 1f99d0867020 9.6.12 landed
-
Fix float-to-integer coercions to handle edge cases correctly.
- d5231253e309 9.4.21 landed
- 93eec1238663 9.6.12 landed
- 1e78603a54a7 9.5.16 landed
- e473e1f2b8f1 11.2 landed
- c382a2b66909 10.7 landed
- cbdb8b4c0155 12.0 landed
-
Adjust new test case for more portability.
- 452b637d4b02 12.0 landed