• 257m@sh.itjust.works
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    1 year ago
    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))

      • 257m@sh.itjust.works
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        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.

        • homura1650@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          1 year ago

          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