*BSD News Article 32295


Return to BSD News archive

Newsgroups: comp.os.386bsd.bugs
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msuinfo!agate!howland.reston.ans.net!vixen.cso.uiuc.edu!milo.mcs.anl.gov!karrels
From: karrels@mcs.anl.gov (Edward L. Karrels)
Subject: float->string bug in FreeBSD 1.1 release
Message-ID: <KARRELS.94Jul2120712@elf.mcs.anl.gov>
Sender: usenet@mcs.anl.gov
Organization: Math & Computer Science Divison, ANL
Date: Sat, 2 Jul 1994 17:07:11 GMT
Lines: 46

FreeBSD 1.1 seems to have problems converting NaN floats to strings.
Other machines just convert them to "NaN", but I'm getting floating-point
exceptions.  Doubles seems OK, though I haven't really tested them.  I just
ran across this problem as I was converting random files into binary
representations of floats, doubles, ints, and longs.  Only floats had problems.
Both sprintf and printf (at least) have this problem.

Sample:

#include <stdio.h>

#define LITTLE_ENDIAN

typedef struct {
#ifdef LITTLE_ENDIAN
  /* little endian is when the lower addresses represent less significant
     bytes, right?  I always get that confused.  Anyway, for this to work
     properly (for this program, to work properly is to die of a floating-
     point exception) on an i386, you must define LITTLE_ENDIAN */
  unsigned long mantissa : 23;
  unsigned long exponent : 8;
  unsigned long sign : 1;
#else
  unsigned long sign : 1;
  unsigned long exponent : 8;
  unsigned long mantissa : 23;
#endif
} myfloat;

main () {
  union {
    myfloat m;
    float f;
  } z;

  z.m.mantissa = 0;		/* +infinity */
  z.m.exponent = 255;
  z.m.sign     = 0;

  printf( "z = %g\n", z.f );
  
  z.m.mantissa = 1;		/* NaN */

  printf( "z = %g\n", z.f );
  
}