Floating point ops, NaN

I’m using the following statement to clip an interpolant between 0 and 1:

A = min(1.0f, max(0.0f, (fValue - fMinValue) / (fMaxValue - fMinValue)))

The 8800 tech spec states that min/max will return the valid operand so I assumed any divisions by zero would be caught. But I’m seeing the results of computations using A be reduced to zero. Unfortunately I can’t replicate the problem in emu-debug mode.

Any ideas?

try

A = saturate((fValue - fMinValue) / (fMaxValue - fMinValue))

?

Hi,

I’m afraid this is not the case, but any CUDA math operation involving a NaN will output NaN.

Hence saturate will as well output NaN in your situation. I think your best option is to check for zero divisor and use an if - else block setting A to either saturate((f-fMin)/(fMax-fMin)) or zero.

/Pyry

Thanks, I was afraid of that. Oh well, I’ll have more divergent threads :(

If (fValue - fMinValue) != 0.0f and (fMaxValue - fMinValue) == 0.0f, you should get A = min(1.0f, max(0.0f, +/-INF)) = min(1.0f, +/-INF) = 1.0f

If (fValue - fMinValue) == 0.0f and (fMaxValue - fMinValue) == 0.0f, you should get A = min(1.0f, max(0.0f, NaN)) = min(1.0f, 0.0f) = 0.0f

If you don’t, maybe the problem comes from conflicting with min/max macros from the host compiler.
Try using fminf/fmaxf instead.

Cyril