-4

So, I have these 2 questions?

#include <stdio.h>
int main(void) 
{
    float a = 0.7;
    if (0.7 > a)
       printf("Hi\n");
    else
       printf("Hello\n");

return 0;
}

What will be the output of this? According to me, it should be "Hello".

Secondly, I have,

#include <stdio.h>
int main(void)
{
    int a=500,b=100,c;
    if (!a >= 400)
       b=300;
    c=200;
    printf("%d %d\n",b,c);
    return 0;
}

As much as I understand, output should be,

100 200

Because !a means, not a , which means, a value which is not 500 is compared with 400 and it can be either greater than or less than 400, so why will it be 300?

user3797829
  • 383
  • 3
  • 15
  • `(!a >= 400)` is the same as `(!(a >= 400))` as well as `(a < 400)` – Eun Jul 02 '14 at 15:40
  • 3
    Regarding your first question, you should first read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). Then you should remember that floating point literals in C are of type `double` by default, so you're comparing a `double` with a `float`, and due to the nature of floating point values on computers then 0.7 might indeed be larger than 0.7. – Some programmer dude Jul 02 '14 at 15:40
  • 4
    @Eun: No, `!` has higher precedence than `>=`; `!` yields either `1` or `0` and both are smaller than 400, so the test always fails. – mafso Jul 02 '14 at 15:43
  • Umm, @mafso, if the test fails, then what should the answer be? A – user3797829 Jul 02 '14 at 15:45
  • @Joachim, can you elaborate on it a little more? Please, thanks. :) – user3797829 Jul 02 '14 at 15:46
  • @mafso your right, `!a` is `0` and `0 >= 400` will always be `false` – Eun Jul 02 '14 at 15:47
  • 1
    Most floating point values can not be stored exactly on a computer, so your `float` variable my actually be `0.697` or something. Then you compare it with a floating point literal value, which is of type `double` and so has higher precision (and so is more exact), and it might be `0.7001`. – Some programmer dude Jul 02 '14 at 15:49
  • 2
    If I compile and run your second code I get the output you expect. Your reasoning seems to indicate some misconception about operators in C, though. – mafso Jul 02 '14 at 15:49
  • @Joachim, is it necessary that it will be always less or it can be more as well? Also, the floating point literal value will always be stored as a double? – user3797829 Jul 02 '14 at 15:53
  • It could have been the other way as well, with the literal really being `0.6999` and the `float` variable really being `0.701`. The only way to know is to print them out. And floating point literals are always of type `double`, as per the C specification. – Some programmer dude Jul 02 '14 at 15:55
  • @Joachim, this question was in one of the resources which I was studying, and we had to tell the output. So, this means that this can have both the answers as we don't know which one will be larger, and which one be smaller. – user3797829 Jul 02 '14 at 16:09

3 Answers3

5

You are not comparing a float variable with a float value. You think that's would you are doing, but you aren't. You are comparing a float variable with a double literal.

0.7 in C is the exact mathematical number 0.7, rounded to the nearest double precision floating point number.

When you initialise float a = 0.7, that double precision floating point number is rounded to single precision and the result stored in a. The rounding will most likely change the value, so the contents of the variable a is a little bit more or a little bit less than 0.7.

Then you compare the variable 0.7. One is rounded to single precision, the other is rounded to double precision, so both are different.

As a rule, you should NEVER, EVER use float instead of double unless you have a good reason to do so, which you could justify when asked about it.

On the other hand, how likely is it that you came up with exactly the same question as multiple other posters here (with exactly the same numbers, 0.7 and not 0.8 or 0.9 or 3.14 or something like that), so I suppose you are trying to get us to do your homework here, right?

gnasher729
  • 51,477
  • 5
  • 75
  • 98
3

For the first example (floating point values), note that the nature of a Float will cause it to contain some garbage out at the end of the number, somewhere around the 15th significant digit or so. Long story short, a float declared at 0.7 will never exactly equal a literal 0.7.

There's a previous question on Stack Overflow that has links to a lot of good resources explaining further: How is floating point stored? When does it matter?

In your second example, you are using the 'Not' operator in a way that will produce an unexpected result. 'Not' is a boolean operator - it turns a 'False' value into a 'True' value, and a 'True' value into a 'False' value.

Recall that in most programming languages, False is assigned the value of 0, meaning any non-0 value is treated as 'True'.

So what your program is actually doing is evaluating !500, then comparing !500 to the value 400. 500 is not 0, so it's treated as a 'True' value for the sake of the boolean operator 'Not'. Not True = False, and False = 0, so !500 == 0. (Try it: print !500 directly and see what you get!). Then, since 0 is not equal-to-or-larger than 400, so the evaluation in your 'if' statement is also false.

Community
  • 1
  • 1
Ironskink
  • 29
  • 3
  • Note: Typical `float` is [IEEE single precision](http://en.wikipedia.org/wiki/Binary32) with about 6 decimal digits of accuracy. C floating point may use a format like http://en.wikipedia.org/wiki/Decimal32_floating-point_format which can exactly equal a literal `0.7`. This is not meant to discount this answer's cautionary remarks about FP, just a refinement. – chux - Reinstate Monica Jul 02 '14 at 17:50
0

Explanation Code 1:

if (0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi'
Example:

#include<stdio.h>
int main()
{
    float a=0.7;
    printf("%.10f %.10f\n",0.7, a);
    return 0;
}


Output:

0.7000000000 0.6999999881

Consider this code:

int main(void)
{
    int a = 300, b,c;
    if (a>=400)
      b=300;
    c=200;
    printf("%d %d %d\n",a,b,c);
 }

Explanation:

Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.

Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.

Step 3: c = 200; here variable c is initialized to '200'.

Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85