181

How to check if a number is negative.

Comments
  • 9
    In a function like that I suggest you go all out and compare with the octal number instead of a char ;)
  • 50
    ...aaaaaand memory leak
  • 6
    @-eth I don't think that would make a big difference for such a codebase anyway
  • 10
    This hurts me on so many levels as a TA for a class that teaches C
  • 7
    kill it! kill it with fire!
  • 4
    There are so so so many problems with this.
    It's painful 🙁
  • 3
    Don't let that breed or lay eggs
  • 10
    Nuke it.
  • 0
    Is there no managed if in C++?
  • 2
    I have been doing it wrong all this time...
  • 2
    return arg < 0;
  • 0
    LOL YES! 😂😂😂
  • 2
    Using the heap instead of the stack to create a fixed size array and not even free it afterwards, all that to sprintf the sign. I'm having a hard time thinking how to make it any worse. You are a genius! :)
    Such efficient, much wow.
  • 0
    In the internal representation, there is 1 bit for the sign (S), 8 bits for the exponent (E), and 23 bits for the fraction (F). The bits are mapped with the fraction in bit 0 to bit 22, the exponent in bit 23 to bit 30, and the sign in bit 31.

    so code:

    static bool IsNagative (float numb)
    {
    if(numb >> 31)
    {
    return true;
    }
    return false;
    }

    no memory allocation needed and if you don't want a copy of the float variable then use references.
  • 1
    @xwing7 so you could basically do:
    return num >> 31
Add Comment