r/shittyprogramming • • Jul 23 '18

r/badcode how do I math?

https://imgur.com/0ysm4wG
41 Upvotes

8 comments sorted by

View all comments

Show parent comment

8

u/Athandreyal Jul 23 '18

(X/5)*5 = X

suppose x=6.

(X/5)*5 = X
(6/5)*5 = 6
(1)*5=6
5=6

uh oh.

Integer division can be a nasty surprise sometimes, the above 6/5 won't yield 1.2, like a calculator would, or you might with pen/paper. It does long division with remainders. The answer is quotient=1,remainder=1, and divisions always yield the quotient unless you explicitly ask for the remainder with %, or mod.

So the x/5*x will get the the largest multiple of 5 still smaller than or equal to the size of x, so 19 would get you 15, etc.

x/5 with x being an integer is equivalent to floor(x/5)

Now, had strikerows been a float or double, or had the equation been (X/5.0)*5, then I would agree with you, pointless math.