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