Comments on NOST 100-1.2

John E. Davis davis at space.mit.edu
Sun Jul 12 15:36:03 EDT 1998


On Sun, 12 Jul 1998 15:54:48 +0200 (DFT), Thierry Forveille <Thierry.Forveille at obs.ujf-grenoble.fr> said:
>I must say I have never seen any convincing argument for supporting
>unsigned values. The present "hack" as you call it offers all the
>functionalities for no cost, while adding unsigned as an additional
>supported format would cost significant extra code to everybody for no
>additional value. To me Occam's razor says it should stay out...

Please correct me if I am wrong, but my main objection to this
convention is inherently non-portable because the BZERO value that must
be used is outside the range of the integer data type.  For example,
to represent an unsigned 32bit integer, BZERO must be set to
2147483648 in the fits file.  Now, BZERO is an integer and this value
is outside the range of an integer.  Why is this a problem?  It is a
problem because parsing it as a 32 bit integer may result in its
truncation. Specifically, CFITSIO uses the strtol function call to
parse these values and this function will truncate 2147483648 to
2147483647.

To see this, consider:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
   long i;
   char *s;

   s = "2147483648";
   i = strtol (s, NULL, 10);
   fprintf (stdout, "%s = %ld\n", s, i);

   s = "2147483647";
   i = strtol (s, NULL, 10);
   fprintf (stdout, "%s = %ld\n", s, i);
   
   return 0;
}

     
Compiled with gcc on my Linux system, this produces:

   2147483648 = 2147483647
   2147483647 = 2147483647

The man page for strtol indicates:

RETURN VALUE
       The  strtol()  function  returns the result of the conver-
       sion, unless the value would underflow or overflow.  If an
       underflow  occurs, strtol() returns LONG_MIN.  If an over-
       flow occurs, strtol() returns LONG_MAX.   In  both  cases,
       errno is set to ERANGE.

So, without real support for unsiged types, I maintain that this
convention is flawed.

Thanks,
--John





More information about the fitsbits mailing list