• @[email protected]
    link
    fedilink
    34
    edit-2
    2 years ago

    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.

    • @[email protected]
      link
      fedilink
      5
      edit-2
      2 years 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))

        • @[email protected]
          link
          fedilink
          12 years 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.

          • @[email protected]
            link
            fedilink
            2
            edit-2
            2 years 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

            • @[email protected]
              link
              fedilink
              1
              edit-2
              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.

    • @[email protected]
      link
      fedilink
      16
      edit-2
      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));  
      
  • Karyoplasma
    link
    fedilink
    312 years ago
    while (true){
        if (number == 0) return true;
        if (number == 1) return false;
        number -= 2
    }
    
      • Karyoplasma
        link
        fedilink
        1
        edit-2
        2 years ago

        You know, shortly after posting this I did think about whether it’s still working if I just pass the underflow that will happen at some point or if I have to fix something in that case (like subtract 1 after the underflow). I deemed it “too complicated” and would just issue a warning that my code is only tested on positive numbers, but I think it will still work.

  • enkers
    link
    fedilink
    42
    edit-2
    2 years ago

    This is your brain on python:

    def is_even (num):
         return num in [x*2 for x in range(sys.maxsize / 2)]
    
    • Goddard Guryon
      link
      fedilink
      102 years ago

      That won’t work tho, you need to make it sys.maxsize//2 to coerce the output into int form

        • Goddard Guryon
          link
          fedilink
          12 years ago

          IIRC it doesn’t; that has caused me pain so many times when trying to generate fractional range

  • @[email protected]
    link
    fedilink
    English
    69
    edit-2
    2 years ago

    Wow. Amateur hour over here. There’s a much easier way to write this.

    A case select:

    select(number){
        case 1:
            return false;
        case 2:
            return true;
    }
    

    And so on.

    • robotica
      link
      fedilink
      13
      edit-2
      2 years ago

      Don’t forget that you can have fall-through cases, so you can simplify it even further:

      switch (number) {
          case 1:
          case 3:
          case 5:
          case 7:
          case 9:
            ...
      
  • @[email protected]
    link
    fedilink
    80
    edit-2
    2 years ago

    Just print True all the time. Half the time it will be correct and the client will be happy, and the other half the time, they will open a ticket that will be marked as duplicate and closed.

    • Rouxibeau
      link
      fedilink
      232 years ago

      Reminds me of the fake thermometers being sold during the peak of COVID that weren’t actually thermometers but just displayed numbers to make people think they were.

  • @[email protected]
    link
    fedilink
    4
    edit-2
    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 milk goblet)

  • Iron Lynx
    link
    fedilink
    132 years ago

    Even the shitposty better version has us:

    • take the absolute value of the input as a variable
    • while that variable is >1, subtract 2. Repeat until this is no longer true
    • if it’s now 1, return true. Otherwise, return false.
    • Iron Lynx
      link
      fedilink
      1
      edit-2
      2 years ago

      On a sidenote, the completely non-shitposty version would be:

      return !(var & 1);

      Notice the single &, which should be bitwise AND. Fun thing: negative numbers in two’s complement also have 1 for their LSB when odd. C definitely supports bitwise operators, and it appears C# does as well.

      • @[email protected]
        link
        fedilink
        22 years ago

        Yeah that should be last bit, not last digit lol.

        For another convoluted impl you could do some kind of string format print into a string. Then do your if/else comparing that string containing the last digit. Maybe create a hash set collection of all single digit even strings, then you’ve got O(1) performance using a contains() method.

  • @[email protected]
    link
    fedilink
    162 years ago

    string taco = variable.ToString()[variable.ToString().Length - 1];

    If (taco == “0” || taco == “2” || taco == “4” || taco == “6” || taco == “8”)

    return true;

    else

    return false;

    Im something of a coding master myself

    • @[email protected]
      link
      fedilink
      32 years ago

      as division is complicated and expensive depending on the size of the numbers you’d usually receive as an input, this could be the most efficient solution. Certainly could have the best worst case if we imagine some 128bit shenanigans.