NaN traps

Michael J Zehr tada at athena.mit.edu
Sun Sep 17 03:27:23 AEST 1989


In article <2110 at munnari.oz.au> ok at cs.mu.oz.au (Richard O'Keefe) writes:
>This will
>give similar trouble on machines with VAX arithmetic (some bit patterns
>when considered as floats are "invalid operands" which trap when/if you
>touch them) and on machines with IEEE arithmetic and traps enabled (the
>IEEE default is _not_ to enable traps!) where some bit patterns (called
>Signalling NaNs) trap.

I once wasted a lot of code dealing with something like: (hopefully this
example will save someone else time recognizing it in the future...)

if (data == 0) {zero_cnt++; continue;}
if NaN(data) {nan_cnt++; continue;}
do_something(data);

the problem was that do_something() was getting garbage results.  the
reason was that if data was a bit pattern which was invalid, the
comparison of it to 0 would change it's value to a particular number!!!
(i think it was the maximum value for it's data type, but i never
verified that.)  the solution, of course, was to switch the two lines.

-michael j zehr



More information about the Comp.lang.c mailing list