Re: [HACKERS] NAN code

From: "Thomas G(dot) Lockhart" <lockhart(at)alumni(dot)caltech(dot)edu>
To: Hal Snyder <hal(at)enteract(dot)com>
Cc: hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] NAN code
Date: 1999-01-04 16:30:54
Message-ID: 3690ECBE.A9CA5171@alumni.caltech.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 1999-01-04 16:41:19 Re: [HACKERS] NAN code
Previous Message Constantin Teodorescu 1999-01-04 16:30:51 Re: [HACKERS] SQLJ