Jump to content

  • Log In with Google      Sign In   
  • 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.

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

#1 Waterlimon   Crossbones+   -  Reputation: 2356

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


Waterlimon (imagine this is handwritten please)


Sponsor:

#2 szecs   Members   -  Reputation: 2094

Posted 11 January 2013 - 11:03 AM

Maybe it's wired that way.



#3 Waterlimon   Crossbones+   -  Reputation: 2356

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


Waterlimon (imagine this is handwritten please)


#4 szecs   Members   -  Reputation: 2094

Posted 11 January 2013 - 11:08 AM

I never found it to be a very big issue.



#5 Madhed   Crossbones+   -  Reputation: 2492

Posted 11 January 2013 - 11:11 AM

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



#6 samoth   Crossbones+   -  Reputation: 4510

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   Crossbones+   -  Reputation: 2356

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.


Waterlimon (imagine this is handwritten please)


#8 JTippetts   Moderators   -  Reputation: 8159

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   Crossbones+   -  Reputation: 3007

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: 8157

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 slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#11 capn_midnight   Crossbones+   -  Reputation: 1375

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+ Github]

#12 Sik_the_hedgehog   Crossbones+   -  Reputation: 1485

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: 1601

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