25

I assume this just returns an int. Is there anything else going on I should be aware of? C/C++ differences?

float a = 2.5;
!a; // What does this return? Int? Float?
Nathan Fig
  • 14,970
  • 9
  • 43
  • 57
  • 2
    Possible answer [here](http://stackoverflow.com/questions/1969620/c-float-to-bool-conversion) – cctan Mar 23 '12 at 03:32

3 Answers3

39

Regarding C++, quoting C++11 §5.3.1/9:

The operand of the logical negation operator ! is contextually converted to bool; its value is true if the converted operand is false and false otherwise. The type of the result is bool.

So what's really relevant here is the behavior of static_cast<bool>(some_float) – quoting §4.12/1:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

Putting those together, 2.5f is a non-zero value and will consequently evaluate to true, which when negated will evaluate to false. I.e., !a == false.


Regarding C, quoting C99 §6.5.3.3/5:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

I.e. the net result is the same as with C++, excepting the type.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Thanks! I assume this means in C I'd get an int? – Nathan Fig Mar 23 '12 at 03:36
  • 3
    @NathanFig: Right, the result of `!` in C is always an `int`. – caf Mar 23 '12 at 03:40
  • @ildjarn I have seen you use `` in F# edits to force syntax highlighting but there is also `` so why not use that? – Cetin Sert Sep 25 '12 at 01:22
  • 1
    @CetinSert : When I first started using SO only the former worked, I simply continue to use it out of habit. :-] – ildjarn Sep 25 '12 at 01:25
  • @ildjarn I am glad I could still reach you with this primitive mode of socializing, despite StackOverflow not giving its users anything better. You are doing a great job with your edits though! Keep them up. It is nice to notice your touches on many F# questions. – Cetin Sert Sep 25 '12 at 01:32
  • 1
    @CetinSert : Thanks. :-] And the one other option for contacting someone is to go to [Chat.SO](http://chat.stackoverflow.com/) and create a new room, then go to the person's chat profile and send them an invitation to that room (it will show them an inbox notification). – ildjarn Sep 25 '12 at 01:40
  • I wonder if 0.0 can be represent precisely, if not, then 0.0 == 0.0 will be false. (as we know, we shouldn not compare two float point value diretly) – vacing Oct 09 '19 at 13:11
9

From here

A float will be converted to false if its exactly 0.0f,
It will be also true if its not exacly 0.0f!
Inifinity will also be converted to true.

Community
  • 1
  • 1
cctan
  • 2,015
  • 3
  • 19
  • 29
-1

See for yourself:

#include <iostream>

int main()
{
   float a = 2.5;

   if ( !a )
       std::cout << !a << "\n";

   else
       std::cout << !a << "\n";
}
01100110
  • 2,294
  • 3
  • 23
  • 32
  • 10
    The problem with "seeing for yourself" is that you can observe undefined or implementation-defined behavior, which only tells you what happens on your particular platform/compiler, which is not useful when knowing what to expect in terms of _well-defined_ behavior. – ildjarn Mar 23 '12 at 03:35
  • You can't "expect" anything from "implementation-defined" behavior. So what's "well-defined" mean to you? – 01100110 Mar 23 '12 at 03:37
  • 9
    Exactly, which is why "see for yourself" is a useless answer if the behavior is implementation-defined. Thanks for agreeing with my point. :-P – ildjarn Mar 23 '12 at 03:41
  • 3
    Both branches of your `if`/`else` block do exactly the same thing, may as well not have the `if`/`else`. – dreamlax May 12 '12 at 23:27