• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Waterlimon  Members

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

o3o

### #2szecs  Members

Posted 11 January 2013 - 11:03 AM

Maybe it's wired that way.

### #3Waterlimon  Members

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

o3o

### #4szecs  Members

Posted 11 January 2013 - 11:08 AM

I never found it to be a very big issue.

Posted 11 January 2013 - 11:11 AM

### #6samoth  Members

Posted 11 January 2013 - 11:15 AM

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, 11 January 2013 - 11:16 AM.

### #7Waterlimon  Members

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.

o3o

### #8JTippetts  Moderators

Posted 11 January 2013 - 11:18 AM

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.

### #9Matias Goldberg  Members

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);
}

### #10Bacterius  Members

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #11Sean T. McBeth  Members

Posted 14 January 2013 - 10:08 PM

Sounds like someone has inconveniently defined an origin.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

### #12Sik_the_hedgehog  Members

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #13wintertime  Members

Posted 15 January 2013 - 10:55 AM

Maybe just not put the origin in the middle but in a corner.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.