Now i have to make a function for division where it always rounds down, since i dont care about the sign, i just want the origin to be 0 :c
Discuss
Posted 11 January 2013 - 10:51 AM
Now i have to make a function for division where it always rounds down, since i dont care about the sign, i just want the origin to be 0 :c
Discuss
Posted 11 January 2013 - 11:05 AM
Maybe it's wired that way.
And because of that it will never be changed and will continue to annoy people who assume it to always round down because it feels logical in this context. :C
Posted 11 January 2013 - 11:17 AM
http://en.cppreference.com/w/cpp/numeric/math/floor
Actually i didnt end up using that because its the rounding after division, what i do is check if the signs of a and b are different and if yes i subtract 1.
The reason why this caused problems was that i was treating the integer just like it wasnt signed (for coordinates on a map, i had the precise coord and had to divide to get the chunk coord), which caused problems with the rounding direction changing at the origin, making the 0 chunk twice bigger than the other ones.
Posted 11 January 2013 - 11:18 AM
Posted 11 January 2013 - 11:34 AM
Yeah, it's called truncating.
If you're always dividing by a power of two, you can use arithmetic bitshifts, they always round down (floor).
Alternatively, you can use this function for flooring arbitrary denominators:
inline int floor_div(int a_numerator, int a_denominator)
{
return (a_numerator / a_denominator) + ((a_numerator % a_denominator) >> 31);
}
Posted 11 January 2013 - 02:30 PM
If integer division didn't round towards zero, it would be inconsistent with the mathematical definition of integer division, and a lot of number theoretical code would have weird +1's and -1's everywhere, which sucks. I like it the way it is, and it's never gotten in my way.
Posted 14 January 2013 - 10:08 PM
Posted 14 January 2013 - 10:59 PM
Sounds like someone has inconveniently defined an origin.
How so? Using 0 as an origin is pretty common.