int to float rounding errors?

Hi All,

I use some kind of sorting in my code. Before I post the actual code and it gets confusing, I have a general question.

When I compare a float to an int, to “sort” some floats into a box-like array, I compare the float value to the integer value like

[codebox]if(some_float < (float)some_integer) …[/codebox]

Most of the floats are in the correct “box” after that, but some of them, which are “close to the border”, such as 3.010003e2 will be in the “300 to 301” box instead of the “301 to 302” box. Is there any known bug/problem that causes this behaviour, or do I have to search “deeper” in my code?

Thanks for any help,

Philipp.

Your code is assuming that each integer can be exactly represented as a floating point number for the purpose of the comparison. That is almost certainly not the case.

Your code is assuming that each integer can be exactly represented as a floating point number for the purpose of the comparison. That is almost certainly not the case.

The way I always do this is round the float to an integer to determine which box it goes into: Guarunteed to always produce the right answer.

rintf even compiles to a single PTX instruction and is blazing fast on the GPU.

The way I always do this is round the float to an integer to determine which box it goes into: Guarunteed to always produce the right answer.

rintf even compiles to a single PTX instruction and is blazing fast on the GPU.

Thanks for that, I will implement that tomorrow when I’m back at work and write again whether it works or not

Thanks for that, I will implement that tomorrow when I’m back at work and write again whether it works or not