pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: 110876189@qq.com
Subject: BUG #15696: year field of interval type is not rounded to nearest integer
Date: Sat, 16 Mar 2019 03:55:11 +0000
Message-ID: <15696-50cd52decb98f630@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      15696
Logged by:          zhou xiaowei
Email address:      110876189@qq.com
PostgreSQL version: 11.2
Operating system:   Linux x86_64
Description:        

all fields of interval type use rule of rounding to nearest integer(by
rint() function), except for year field.
details:
postgres=# select '0.9999999999999999 year'::interval month;
 interval
----------
 11 mons
(1 row)

postgres=# select '0.9999999999999999 dec'::interval year;
 interval
----------
 9 years
(1 row)

postgres=# select '0.9999999999999999 cent'::interval year;
 interval
----------
 99 years
(1 row)

postgres=# select '0.9999999999999999 mil'::interval year;
 interval
-----------
 999 years
(1 row)

the reason is that assign double value to int value in function
DecodeInterval() :
int DecodeInterval(char **field, int *ftype, int nf, int range,
			   int *dtype, struct pg_tm *tm, fsec_t *fsec)
{
	double		fval;
	.......
		switch (ftype[i])
					case DTK_YEAR:
						tm->tm_year += val;
						if (fval != 0)
							tm->tm_mon += fval * MONTHS_PER_YEAR;
						tmask = DTK_M(YEAR);
						break;
					case DTK_DECADE:
						tm->tm_year += val * 10;
						if (fval != 0)
							tm->tm_mon += fval * MONTHS_PER_YEAR * 10;
						tmask = DTK_M(DECADE);
						break;

					case DTK_CENTURY:
						tm->tm_year += val * 100;
						if (fval != 0)
							tm->tm_mon += fval * MONTHS_PER_YEAR * 100;
						tmask = DTK_M(CENTURY);
						break;

					case DTK_MILLENNIUM:
						tm->tm_year += val * 1000;
						if (fval != 0)
							tm->tm_mon += fval * MONTHS_PER_YEAR * 1000;
						tmask = DTK_M(MILLENNIUM);
						break;
	......
}
The code 'tm->tm_mon' is int type.I think this is a bug,need use rint() to
adjust it’s result,like:
tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000);



view thread (3+ messages)  latest in thread

Message-ID: <15696-50cd52decb98f630@postgresql.org>
Permalink:  ../15696-50cd52decb98f630@postgresql.org/
Also on:    postgresql.org/message-id/15696-50cd52decb98f630@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, 110876189@qq.com
  Subject: Re: BUG #15696: year field of interval type is not rounded to nearest integer
  In-Reply-To: <15696-50cd52decb98f630@postgresql.org>
  References: <15696-50cd52decb98f630@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox