Modulus equals zero anyone?
I wish
Submitted 2 years ago by nave@lemmy.zip to [deleted]
https://i.imgur.com/31qxtlZ.png
Comments
pinkdrunkenelephants@lemmy.cafe 2 years ago
ramenshaman@lemmy.world 2 years ago
That’s the joke
nogrub@lemmy.world 2 years ago
but that operation is expensive
pinkdrunkenelephants@lemmy.cafe 2 years 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 2 years ago
Nah, it compiles down to an AND because of the constant 2
postmateDumbass@lemmy.world 2 years ago
They really should mod the language to support this.
Cosmicomical@lemmy.world 2 years ago
There is a simpler way: all the "else if"s can be replaced with simple "if"s
Valmond@lemmy.mindoki.com 2 years ago
You mean make it even worse.(performance wise)? Interesting.
mryessir@lemmy.sdf.org 2 years ago
Please elaborate. When and why would his suggestion be worse?
superpants@lemm.ee 2 years ago
I did something like this in high school
Clbull@lemmy.world 2 years 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 2 years ago
Rip switch statements
wtry@lemm.ee 2 years ago
bool iseven(int number){ if (number % 2 == 0){ return true; } else { return false; } }TopRamenBinLaden@sh.itjust.works 2 years 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 2 years 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 2 years 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 2 years ago
Readability over obscure hacks
infreq@lemmy.world 2 years ago
Whooosh
Kingofthevoid@lemmy.world 2 years ago
Whoosh