Modulus equals zero anyone?
I wish
Submitted 1 year ago by nave@lemmy.zip to [deleted]
https://i.imgur.com/31qxtlZ.png
Comments
pinkdrunkenelephants@lemmy.cafe 1 year ago
ramenshaman@lemmy.world 1 year ago
That’s the joke
nogrub@lemmy.world 1 year ago
but that operation is expensive
pinkdrunkenelephants@lemmy.cafe 1 year ago
So what? It works and it’s better than precompiling a list of all known even and odd numbers, and expecting a computer to iterate through a whole list every time it wants to check the value of something.
The stupid trig tables are just as problematic and it’s why graphics chips use other geometric functions instead. It’s just better to use a modulus.
Rubeer@sh.itjust.works 1 year ago
Nah, it compiles down to an AND because of the constant 2
postmateDumbass@lemmy.world 1 year ago
They really should mod the language to support this.
Cosmicomical@lemmy.world 1 year ago
There is a simpler way: all the "else if"s can be replaced with simple "if"s
Valmond@lemmy.mindoki.com 1 year ago
You mean make it even worse.(performance wise)? Interesting.
mryessir@lemmy.sdf.org 1 year ago
Please elaborate. When and why would his suggestion be worse?
superpants@lemm.ee 1 year ago
I did something like this in high school
Clbull@lemmy.world 1 year ago
I, EvaX, humbly submit a toast to Nicholas Alexander for successfully managing to pirate WarCraft III so that he may play Defense of the Ancients.
Congratulations Nick, enjoy your DotA!
(sips from cum chalice)
Flax_vert@feddit.uk 1 year ago
Rip switch statements
wtry@lemm.ee 1 year ago
bool iseven(int number){ if (number % 2 == 0){ return true; } else { return false; } }
TopRamenBinLaden@sh.itjust.works 1 year ago
I like the example in the post better. It is more clear as to what is going on to an experienced dev like me. What’s this 2 percent nonsense?
EvokerKing@lemmy.world 1 year ago
Explanation: the percent is modulus. Basically it’s just divide the first number by the second and return the remainder. If you do number % 2, it will return 1 if it is odd and 0 if it is even. For example 4/2 has a remainder of 0 and therefore is even. 3/2 has a remainder of 1, and therefore is odd.
Clearwatermo@lemmy.world 1 year ago
I like the example in the comment better. It is more confusing as to what is going on to an experienced dev like me. iSeven is always odd tho right?
filcuk@lemmy.zip 1 year ago
Readability over obscure hacks
infreq@lemmy.world 1 year ago
Whooosh
Kingofthevoid@lemmy.world 1 year ago
Whoosh