Hi.
Is it possible to return a closest integer to a floating number by defining a funtion that uses only +,-,*,/ operations?
Hi.
Is it possible to return a closest integer to a floating number by defining a funtion that uses only +,-,*,/ operations?
float round(float x) {
x += 12582912.0f;
x -= 12582912.0f;
return x;
}
I am not sure the exact range of inputs for which that works, but I think it's the best you can do.I must admit I do not see how this could work though. Could you elaborate on the definition a bit? About the two constants and such. Thanks a lot
What would the constant be for 64 bit floats? I gess the same since exponent is the same range as in 64 bit, but it seems not to work.
I could truncate the float by shifting significant bits to right by amount of (bias)-(exponent unbiased) times and then set exponent to bias. But I cannot afford bitwise operations, only algebraic ones, how could I do this with algebraic operation? Thanks a lot
Is this some kind of arbitrary challenge? What's the actual problem, and why can you only use +,-,*,/?
. 22 Racing Series .
Is this some kind of arbitrary challenge? What's the actual problem, and why can you only use +,-,*,/?
I need to do the operation on gpu, a float resulting to a float. So I cannot use things like modulo, or bitwise ops. Using standard operations would also make this definable as a proper math function, but that is not my concern.
GPU's can do floor natively, so using the built-in floor function will most likely be much faster than emulating it yourself using a bunch of arithmetic.
Is there a reason you're avoiding the built-in implementation?
. 22 Racing Series .