On Tue, Sep 10, 2013 at 8:40 PM, Claude Heiland-Allen claude@mathr.co.uk wrote:
Hi Kjetil,
In my own code I tend to exploit the incomparibility of NaN.
Instead of:
if (x < lo) x = lo; if (x > hi) x = hi;
I write:
if (! (x >= lo)) x = lo; if (! (x <= hi)) x = hi;
As any comparison with NaN gives false, the first version will pass NaN through unchanged, but the second version will replace NaN with lo. Behaviour with finite values and +/-Infinity should remain the same as the first version.
Hi Claude,
That is a nice solution, but is
" if (! (x >= lo)) x = lo; if (! (x <= hi)) x = hi; "
reallly faster than
" if(!isfinite(x)) x = 0.0f; if (x < lo) x = lo; if (x > hi) x = hi; "
?
If not, the second option is much clearer.