3 ms·
It's like in Ruby, when you divide 3/2 you get 1 (integer division) which is almost never the behavior you want (unless you cast all you numbers to float which
by sunseb 8y ago
It's like in Ruby, when you divide 3/2 you get 1 (integer division) which is almost never the behavior you want (unless you cast all you numbers to float which is a pain to do).
- anyfoo 8y agoWhen dealing with integer quantities and together with modulo, it's pretty natural. n/d gives the integer result, n%d gives the rest. n/2 for example always gives you the "middle" element. And in e.g. a loop where you e.g. transmit elements in blocks, you handle full blocks n/blocksize times, followed by a partial block with n%blocksize elements. It's not always what you want, but it's pretty reasonable. The alternative would be if it always rounds up, which seems weirder to me. I don't think rounding to the nearest integer makes sense with integer quantities at all, that's strictly a real number operation.
- btilly 8y agoIn Ruby there is % which gives you remainders. When we first teach children about division we teach them that, "17/5 = 3 with a remainder of 2". It is only later that they learn about decimal points and learn to say that 3.4 is the answer.
- dragonwriter 8y ago> It's like in Ruby, when you divide 3/2 you get 1 (integer division) which is almost never the behavior you want I dunno, if I'm dividing a quantized space, which is a fairly common thing to do, its exactly what I want. > (unless you cast all you numbers to float which is a pain to do). Using floats just gets you different wrong answers than using integers, unless you happened to somehow have ints to start and want a floating point approximation, which I find is less likely than wanting integer division. What you really want for exact division where there are integers involved is to have at least one operand specified as a rational; this also takes less keystrokes, if its a literal, than changing it to a float to get an inexact answer. Related to this, prior to Ruby 2.5, the standard library include "mathn", a library which monkey-patched all the numeric types so that operations on them acted pretty much like Ruby had a Scheme-style numeric tower, but since that has process-wide effects not restricted to where the library is required, that was deprecated in 2.2 and removed in 2.5.