Re: [NOVICE] Postgres storing time in strange manner - Mailing list pgsql-bugs

From Tom Lane
Subject Re: [NOVICE] Postgres storing time in strange manner
Date
Msg-id 1931.1032290094@sss.pgh.pa.us
Whole thread Raw
In response to Postgres storing time in strange manner  (Casey Allen Shobe <cshobe@secureworks.net>)
Responses Re: [NOVICE] Postgres storing time in strange manner  (Bruce Momjian <pgman@candle.pha.pa.us>)
Re: [NOVICE] Postgres storing time in strange manner  (Sean Chittenden <sean@chittenden.org>)
List pgsql-bugs
Ron Johnson <ron.l.johnson@cox.net> writes:
> Out of curiosity: why does -ffast-math break the datetime rounding code?

We dug into this last night, and it turns out that the culprit is code
like

    int hour = time / 3600;

where time is a double.  This yields an exact result when done
correctly, but with -ffast-math gcc will "improve" it to

    int hour = time * 0.000277777777777778;

the constant being the nearest double value to 1.0 / 3600.0.  The
problem is that the constant is inexact and in fact is slightly too
large; so for example if time is exactly 18000.0, you get a resulting
hour value of 4, not 5, after truncation to integer.  Repeated a couple
more times, what should have been 5:00:00 comes out as 4:59:60 ...

            regards, tom lane

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: Bug #771: rewrite rules on update or insert do not report errors
Next
From: Bruce Momjian
Date:
Subject: Re: [NOVICE] Postgres storing time in strange manner