Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

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.

  • You cannot reply to this topic
12 replies to this topic

#1 Waterlimon   Members   -  Reputation: 1010

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


The lack of awesome free resource gathering building sandbox games capable of running an user made 8 bit computer in the world disturbs me.

Sponsor:

#2 szecs   Members   -  Reputation: 1676

Posted 11 January 2013 - 11:03 AM

Maybe it's wired that way.



#3 Waterlimon   Members   -  Reputation: 1010

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


The lack of awesome free resource gathering building sandbox games capable of running an user made 8 bit computer in the world disturbs me.

#4 szecs   Members   -  Reputation: 1676

Posted 11 January 2013 - 11:08 AM

I never found it to be a very big issue.



#5 Madhed   Members   -  Reputation: 1245

Posted 11 January 2013 - 11:11 AM

http://en.cppreference.com/w/cpp/numeric/math/floor



#6 samoth   Members   -  Reputation: 1957

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.


#7 Waterlimon   Members   -  Reputation: 1010

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.


The lack of awesome free resource gathering building sandbox games capable of running an user made 8 bit computer in the world disturbs me.

#8 JTippetts   Moderators   -  Reputation: 5023

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.

#9 Matias Goldberg   Members   -  Reputation: 1606

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



#10 Bacterius   Crossbones+   -  Reputation: 3548

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.


"The best comment is a deleted comment."
website · blog

[maintenance in progress]


#11 capn_midnight   Members   -  Reputation: 1226

Posted 14 January 2013 - 10:08 PM

Sounds like someone has inconveniently defined an origin.
[See some of My Projects] - [Find me on twitter tumblr G+] - [Watch me burn stuff in Exile: the Family You Choose] - [Come hang out at Philadelphia's Premiere Hackerspace - Hive76]

#12 Sik_the_hedgehog   Members   -  Reputation: 957

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.

#13 wintertime   Members   -  Reputation: 532

Posted 15 January 2013 - 10:55 AM

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






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.



PARTNERS