Rejection of the smallest int8

Started by Kenji Sugitaover 24 years ago7 messageshackers
Jump to latest
#1Kenji Sugita
sugita@sra.co.jp

Attached is a patch to accept the smallest value of int8.

The smallest value -9223372036854775808 is rejected as follows:

test=# create table test_int8 (val int8);
CREATE
test=# insert into test_int8 values (-9223372036854775807);
INSERT 4026531936 1
test=# insert into test_int8 values (-9223372036854775808);
ERROR: int8 value out of range: "-9223372036854775808"
test=#

Attachments:

smallest-int8.patchtext/plain; charset=us-asciiDownload+14-2
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kenji Sugita (#1)
Re: Rejection of the smallest int8

sugita@sra.co.jp writes:

Attached is a patch to accept the smallest value of int8.

This has been proposed before. The problem with it is that it's
not portable: the C standard does not specify the direction of rounding
of integer division when the dividend is negative. So the test
inside the loop that tries to detect overflow would be likely to fail
on some machines.

If you can see a way around that, we're all ears ...

regards, tom lane

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#2)
Re: Rejection of the smallest int8

I said:

Attached is a patch to accept the smallest value of int8.

This has been proposed before. The problem with it is that it's
not portable: the C standard does not specify the direction of rounding
of integer division when the dividend is negative.

BTW, does anyone have a copy of the ANSI C standard to check this?

I have a draft of C99, which says that truncation is towards 0
regardless of the sign, but I think that this is something that was
tightened up in C99; we can't rely on older compilers to follow it.

regards, tom lane

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#2)
Re: Rejection of the smallest int8

I said:

If you can see a way around that, we're all ears ...

Of course there's always the brute-force solution:

if (strcmp(ptr, "-9223372036854775808") == 0)
return -9223372036854775808;
else
<<proceed with int8in>>

(modulo some #ifdef hacking to attach the correct L or LL suffix to the
constant, but you get the idea)

This qualifies as pretty durn ugly, but might indeed be more portable
than any other alternative. Comments?

regards, tom lane

#5Peter Eisentraut
peter_e@gmx.net
In reply to: Tom Lane (#2)
Re: Rejection of the smallest int8

Tom Lane writes:

This has been proposed before. The problem with it is that it's
not portable: the C standard does not specify the direction of rounding
of integer division when the dividend is negative. So the test
inside the loop that tries to detect overflow would be likely to fail
on some machines.

If you can see a way around that, we're all ears ...

Use strtoll/strtoull if available. They should be on "most" systems
anyway.

--
Peter Eisentraut peter_e@gmx.net

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#5)
Re: Rejection of the smallest int8

Peter Eisentraut <peter_e@gmx.net> writes:

Use strtoll/strtoull if available. They should be on "most" systems
anyway.

Mph. The reason int8in is coded the way it is is to avoid having to
deal with strtoll configuration (does it exist? Is it the right thing?
Don't forget Alphas, where int8 == long). We'd still need a fallback
if it doesn't exist, so I'm not that excited about this answer.

regards, tom lane

#7Kenji Sugita
sugita@sra.co.jp
In reply to: Tom Lane (#4)
Re: Rejection of the smallest int8

From: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: [PATCHES] Rejection of the smallest int8
Date: Wed, 21 Nov 2001 12:54:31 -0500

;;; I said:
;;; > If you can see a way around that, we're all ears ...
;;;
;;; Of course there's always the brute-force solution:
;;;
;;; if (strcmp(ptr, "-9223372036854775808") == 0)
;;; return -9223372036854775808;
;;; else
;;; <<proceed with int8in>>
;;;
;;; (modulo some #ifdef hacking to attach the correct L or LL suffix to the
;;; constant, but you get the idea)
;;;
;;; This qualifies as pretty durn ugly, but might indeed be more portable
;;; than any other alternative. Comments?

I made a new patch. Toward zero fault is fixed.

Kind regards,

Kenji Sugita
sugita@sra.co.jp

Attachments:

smallest-int8.patchtext/plain; charset=us-asciiDownload+19-4