• Advertisement
Sign in to follow this  

Whos idea was to make integer division round towards 0? -.-

This topic is 1829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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

Share this post


Link to post
Share on other sites
To answer the actual question ("Whose idea was it..."), that would be William James Cody, with contributions of half a dozen people from IBM, Apple, and the University of Berkeley. Edited by samoth

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Because it's not rounding, it's truncating. Rounding wouldn't really be appropriate in this context and, in my opinion at least, it would be so much more annoying for it to round by default and necessitate special functionality to truncate instead.

Share this post


Link to post
Share on other sites

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:

[code]inline int floor_div(int a_numerator, int a_denominator)
{
return (a_numerator / a_denominator) + ((a_numerator % a_denominator) >> 31);
}[/code]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement