delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/10/05:53:51

Message-Id: <3.0.1.32.19970610105322.005932a8@mailhost.sm.ic.ac.uk>
Date: Tue, 10 Jun 1997 10:53:22 +0100
To: djgpp AT delorie DOT com
From: Paul Dixon <p DOT dixon AT ic DOT ac DOT uk>
Subject: Re: Possible bug? (really IEEE float formats)
Mime-Version: 1.0

>>> ---- snip snip ---
>>> #include <stdio.h>
>>> #include <string.h>
>>> int main(void)
>>> {
>>>   double n;
>>>   memset(&n,0xFF,sizeof(double));  /* contrived, sure */
>>>   printf("%f\n",n);
>>> }
>>
>
>I'd say it's a good guess that a double made of 0xFF's is not a valid
>floating point number. I don't claim to be an 80x87 guru but I know you
>can't just load any old garbage into a FPU register and expect it to work.

This is NOT 'any old garbage' - it is defined by IEEE as a 'Not a Number'
(NAN). BUT you need to use _control87 to mask invalid exceptions to prevent
the crash. The following is cribbed from floating point code that I have
used for yonks and which does much the same (ie allows storing any float
value or a
'missing value' into a floating point variable)

#include <float.h>
#include <stdio.h>

static const long cnan[] = { -1L, -1L };
double dnan() { return *((double *)(cnan)); }

int main( int argc, char **argv )
{
  _control87( MCW_EM, MCW_EM );     << without this line the programme crashes
                                    << with, it prints 'NaN'
  printf( "%f\n", dnan() );         
  return 0;
}

--------------------------------------------------------------------
Paul Dixon                                   Email: p DOT dixon AT ic DOT ac DOT uk
Software Engineer                          tel:   +44 (171) 725 1098
Academic Dept of Paediatrics               fax:   +44 (171) 725 6284
St Mary's Hospital Medical School
 (a constituent college of 
                  Imperial College of Science, Technology & Medicine)
Norfolk Place, London W2 1PG, UK
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019