▲ 911 ▼ I wish (i.imgur.com) submitted 2 years ago by nave@lemmy.zip to c/lemmyshitpost@lemmy.world 187 comments fedilink hide all child comments
[–] DarkMessiah@lemmy.world 27 points 2 years ago* (last edited 2 years ago) (6 children) Just in case anyone was looking for a decent way to do it... if (((number/2) - round(number/2)) == 0) return true; return false; Or whatever the rounding function is in your language of choice. EDIT: removed unnecessary else. permalink fedilink source hideshow 12 child comments replies: [+] AnxiousOtter@lemmy.world 20 points 2 years ago [deleted] permalink fedilink source parent [–] Acters@lemmy.world 16 points 2 years ago* Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed. use bitwise & // n&1 is true, then odd, or !n&1 is true for even return (!(n & 1)); permalink fedilink source parent [–] nopt@lemmy.world 11 points 2 years ago (1 child) Or modulo % permalink fedilink source parent hideshow 2 child comments replies: [–] oatscoop@midwest.social 5 points 2 years ago private bool IsEven(int number){ return number % 2 ? false : true; } permalink fedilink source parent [–] happyhippo@feddit.it 8 points 2 years ago (1 child) Huh? return number % 2 == 0 That's the only sane solution. permalink fedilink source parent hideshow 2 child comments replies: [–] DarkMessiah@lemmy.world 0 points 2 years ago Do note how I said “a decent” way, not “the best” way. Get that huh outta here. permalink fedilink source parent [–] 257m@sh.itjust.works 5 points 2 years ago* (1 child) number % 2 == 0 and (number & 0b1) == 0 Are the only sane ways to do this. No need to floor. Although If its C and you can't modulo floats then (number/2 == floor(number/2)) permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 1 point 2 years ago (1 child) If you are using floats, you really do not want to have an isEven function ... permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago (1 child) Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction. permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 2 points 2 years ago* (last edited 2 years ago) (1 child) No. The alternative is to not use a float. Testing if a float is even simply does not make sense. Even testing two floats for equality rarely makes sense. What is the correct output of isEven((.2 + .4) ×10) Hint: (.2 + .4) x 10 != 6 permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent [–] UNWILLING_PARTICIPANT@sh.itjust.works 4 points 2 years ago (1 child) Take out the else and I'm in permalink fedilink source parent hideshow 2 child comments replies: [–] DarkMessiah@lemmy.world 2 points 2 years ago Valid point. permalink fedilink source parent
[–] Acters@lemmy.world 16 points 2 years ago* Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed. use bitwise & // n&1 is true, then odd, or !n&1 is true for even return (!(n & 1)); permalink fedilink source parent
[–] nopt@lemmy.world 11 points 2 years ago (1 child) Or modulo % permalink fedilink source parent hideshow 2 child comments replies: [–] oatscoop@midwest.social 5 points 2 years ago private bool IsEven(int number){ return number % 2 ? false : true; } permalink fedilink source parent
[–] oatscoop@midwest.social 5 points 2 years ago private bool IsEven(int number){ return number % 2 ? false : true; } permalink fedilink source parent
[–] happyhippo@feddit.it 8 points 2 years ago (1 child) Huh? return number % 2 == 0 That's the only sane solution. permalink fedilink source parent hideshow 2 child comments replies: [–] DarkMessiah@lemmy.world 0 points 2 years ago Do note how I said “a decent” way, not “the best” way. Get that huh outta here. permalink fedilink source parent
[–] DarkMessiah@lemmy.world 0 points 2 years ago Do note how I said “a decent” way, not “the best” way. Get that huh outta here. permalink fedilink source parent
[–] 257m@sh.itjust.works 5 points 2 years ago* (1 child) number % 2 == 0 and (number & 0b1) == 0 Are the only sane ways to do this. No need to floor. Although If its C and you can't modulo floats then (number/2 == floor(number/2)) permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 1 point 2 years ago (1 child) If you are using floats, you really do not want to have an isEven function ... permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago (1 child) Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction. permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 2 points 2 years ago* (last edited 2 years ago) (1 child) No. The alternative is to not use a float. Testing if a float is even simply does not make sense. Even testing two floats for equality rarely makes sense. What is the correct output of isEven((.2 + .4) ×10) Hint: (.2 + .4) x 10 != 6 permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent
[–] homura1650@lemmy.world 1 point 2 years ago (1 child) If you are using floats, you really do not want to have an isEven function ... permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago (1 child) Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction. permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 2 points 2 years ago* (last edited 2 years ago) (1 child) No. The alternative is to not use a float. Testing if a float is even simply does not make sense. Even testing two floats for equality rarely makes sense. What is the correct output of isEven((.2 + .4) ×10) Hint: (.2 + .4) x 10 != 6 permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent
[–] 257m@sh.itjust.works 1 point 2 years ago (1 child) Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction. permalink fedilink source parent hideshow 2 child comments replies: [–] homura1650@lemmy.world 2 points 2 years ago* (last edited 2 years ago) (1 child) No. The alternative is to not use a float. Testing if a float is even simply does not make sense. Even testing two floats for equality rarely makes sense. What is the correct output of isEven((.2 + .4) ×10) Hint: (.2 + .4) x 10 != 6 permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent
[–] homura1650@lemmy.world 2 points 2 years ago* (last edited 2 years ago) (1 child) No. The alternative is to not use a float. Testing if a float is even simply does not make sense. Even testing two floats for equality rarely makes sense. What is the correct output of isEven((.2 + .4) ×10) Hint: (.2 + .4) x 10 != 6 permalink fedilink source parent hideshow 2 child comments replies: [–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent
[–] 257m@sh.itjust.works 1 point 2 years ago* It does if it dosen't have a decimal. If it has decimal then it automatically isn't and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy. permalink fedilink source parent
[–] UNWILLING_PARTICIPANT@sh.itjust.works 4 points 2 years ago (1 child) Take out the else and I'm in permalink fedilink source parent hideshow 2 child comments replies: [–] DarkMessiah@lemmy.world 2 points 2 years ago Valid point. permalink fedilink source parent