Hi,
Is it really safer to use :
inline int sign(float a)
{
return (a > 0.0f) - (a < 0.0f);
}
Than :
inline int sign(float a)
{
return (a > 0.0f) ? 1 : (a < 0.0f) ? -1 : 0;
}
Thanks
Hi,
Is it really safer to use :
inline int sign(float a)
{
return (a > 0.0f) - (a < 0.0f);
}
Than :
inline int sign(float a)
{
return (a > 0.0f) ? 1 : (a < 0.0f) ? -1 : 0;
}
Thanks
inline int sign(float a)
{
return (a > 0.0f ? 1 : 0) - (a < 0.0f ? 1 : 0);
}
Venturing off-ttopic, is there a reason to have three results? Often it's simpler to say that zero is positive by convention.inline float sign(float a)
{
return (a >= 0.0f) ? 1.0f : 0.0f;
}
Venturing off-ttopic, is there a reason to have three results? Often it's simpler to say that zero is positive by convention.
Talking about safety, wouldn't it make sense to use an epsilon instead of 0.0f, as is done with most floating point math?
const float EPSILON = 0.00001f; // pass in to the function if this needs to be different throughout the program
inline int sign(float a)
{
return (a > EPSILON) - (a < EPSILON);
}
Unless you explicitely want 0.00000002 to be positive and not 0 in your whole application.
0.00001f is many million trillion trillion times bigger than the smallest positive float - that's a large bracket to round to zero without a specific computer-science or maths reason :DTalking about safety, wouldn't it make sense to use an epsilon instead of 0.0f,
Hah, as a graphics/engine programmer I just don't want the codegen to be crap. e.g. the sign function in HLSL covers these 3 cases, but should almost never be used as there's usually more optimal algorithms than ones that use that function.As someone with a mathematical background I would find that extremely vexing and force me to curse the author for at least twenty minutes. If you call it like the popular function then let it do what the popular function does. If you want something different, have the courtesy to call it differently.
I'll remeber to call my function sign_bit if I ever write that "optimized" version
I'll remeber to call my function sign_bit if I ever write that "optimized" version
Talking about safety, wouldn't it make sense to use an epsilon instead of 0.0f, as is done with most floating point math?
Unless you explicitely want 0.00000002 to be positive and not 0 in your whole application.const float EPSILON = 0.00001f; // pass in to the function if this needs to be different throughout the program inline int sign(float a) { return (a > EPSILON) - (a < EPSILON); }