Re: [HACKERS] NAN code - Mailing list pgsql-hackers

From Thomas G. Lockhart
Subject Re: [HACKERS] NAN code
Date
Msg-id 3690ECBE.A9CA5171@alumni.caltech.edu
Whole thread Raw
In response to Re: [HACKERS] NAN code  (orion.SAPserv.Hamburg.dsh.de!wieck@sapserv.debis.de)
List pgsql-hackers
> >     Seems  that only isnan() is defined as part of Posix. But not
> >     a definition that can force a NAN.  So  we  have  to  find  a
> >     portable  way  to  define the value NaN for double and float.
> >     Does anybody know of such a way?
> Bruce changed it to defining NAN as (0.0/0.0) if NAN isn't already
> defined. This works on FreeBSD. I don't know if it will break on other
> systems that don't have NAN already.

The NaN concept is not portable on every platform, since it is an IEEE
754 floating point feature. Older machines like VAX don't have it, and
older compilers don't support it cleanly. The divide-by-zero technique
to get NaN is likely to result in a signaled FPE, where we want a quiet
assignment of a special value (NaN on those platforms for which it is
available).

Here is the code we already have in Postgres to support the date/time
data types; you'll see NAN, HUGE_VAL, and DBL_MAX used for special
values:

#ifdef NAN
#define DT_INVALID      (NAN)
#else
#define DT_INVALID      (DBL_MIN+DBL_MIN)
#endif
#ifdef HUGE_VAL
#define DT_NOBEGIN      (-HUGE_VAL)
#define DT_NOEND        (HUGE_VAL)
#else
#define DT_NOBEGIN      (-DBL_MAX)
#define DT_NOEND        (DBL_MAX)
#endif
#define DT_CURRENT      (DBL_MIN)
#define DT_EPOCH        (-DBL_MIN)

#define DATETIME_INVALID(j)     {j = DT_INVALID;}
#ifdef NAN
#define DATETIME_IS_INVALID(j)  (isnan(j))
#else
#define DATETIME_IS_INVALID(j)  (j == DT_INVALID)
#endif

Of course, for historical reasons the date/time stuff has these special
reserved values which may not be necessary for the numerics. If
necessary, I would suggest something similar for the numeric stuff, or
perhaps on platforms without NAN and no way to generate it then just
throw an error.

As an aside, I worked on this a few years ago on my Alpha-dunix boxes
(for converting back and forth between VAX and IEEE floating point
binary values) and found that for the DEC compiler you had to use a
particular compiler flag to keep NaN as an unsignaled value. Otherwise
the call to isnan() failed when the value was put on the stack to set up
the function call to isnan() :/
                      - Tom


pgsql-hackers by date:

Previous
From: "Vazsonyi Peter[ke]"
Date:
Subject: Q: Log, via triggers
Next
From: jwieck@debis.com (Jan Wieck)
Date:
Subject: Re: [HACKERS] NAN code