Обсуждение: Is PG built on any C compilers where int division floors?

Поиск
Список
Период
Сортировка

Is PG built on any C compilers where int division floors?

От
Chapman Flack
Дата:
C99 finally pinned down what / does on signed ints, truncating toward zero.

Before that, it could truncate toward zero, or floor toward -inf.

Is PostgreSQL built on any compilers/platforms that have the floor
behavior?

-Chap


Re: Is PG built on any C compilers where int division floors?

От
Tom Lane
Дата:
Chapman Flack <chap@anastigmatix.net> writes:
> C99 finally pinned down what / does on signed ints, truncating toward zero.
> Before that, it could truncate toward zero, or floor toward -inf.
> Is PostgreSQL built on any compilers/platforms that have the floor
> behavior?

I'm not sure if we still have any buildfarm animals that act that way[1],
but the project policy is that we target C90 not C99.  So wiring in any
assumptions about this would seem to be contrary to policy.

            regards, tom lane

[1] gaur/pademelon might, but it's turned off and 300 miles away, so
I can't check right now.


Re: Is PG built on any C compilers where int division floors?

От
Chapman Flack
Дата:
On 06/24/18 23:38, Tom Lane wrote:
> Chapman Flack <chap@anastigmatix.net> writes:
>> C99 finally pinned down what / does on signed ints, truncating toward zero.
>> Before that, it could truncate toward zero, or floor toward -inf.
>> Is PostgreSQL built on any compilers/platforms that have the floor
>> behavior?
> 
> I'm not sure if we still have any buildfarm animals that act that way[1],
> but the project policy is that we target C90 not C99.  So wiring in any


On a related note, does PG itself specify the behavior of its own signed int
/ and % (and div() and mod() functions)? Should it? I've been looking at

https://www.postgresql.org/docs/11/static/functions-math.html

and that doesn't specify. (There's a column of examples for the operators,
where the operands are positive. Changing nothing else, it might be more
revealing to toss in a negative-operand example: do we have div(-9,4) = -2
with mod(-9,4) = -1, or div(-9,4) = -3 with mod(-9,4) = +3 ?)

I don't see it specified in the standard either (2006 draft) - doesn't even
pay the issue enough notice to say it's implementation-defined.

Do PG's operators just do what the underlying C compiler generates?

Also, the PG manual doesn't seem to say whether >> is arithmetic or logical.
(I just tried it here and it was arithmetic; I assume it's consistent, but
would it be worth saying?)

-Chap


Re: Is PG built on any C compilers where int division floors?

От
Tom Lane
Дата:
Chapman Flack <chap@anastigmatix.net> writes:
> Do PG's operators just do what the underlying C compiler generates?

Yes.  This is generally true for both the int and float operators.

            regards, tom lane