Precedence of %

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Precedence of %
Date: 2005-06-04 03:56:46
Message-ID: 200506040356.j543uki09397@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Does anyone understand why the precedence of % is strange:

test=> select -25 % -10;
?column?
----------
-35
(1 row)

test=> select -25 % (-10);
?column?
----------
-5
(1 row)

Is it treating the first as -25 - 10? Why? Why are parens necessary to
get the right answer? I see this in gram.y:

%left '+' '-'
%left '*' '/' '%'

Look at this:

test=> select -25 + -10;
?column?
----------
-35
(1 row)

test=> select -25 * -10;
?column?
----------
250
(1 row)

test=> select -25 / -10;
?column?
----------
2
(1 row)

test=> select -25 % -10;
?column?
----------
-35
(1 row)

Only the '%' case looks wrong.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Neil Conway 2005-06-04 04:27:03 Re: need help
Previous Message Bruce Momjian 2005-06-04 02:59:43 Re: psql: \d+ show tablespace of indices