Mail Archives: djgpp/1997/06/10/05:53:51
>>> ---- 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 -