C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Find Output of Program (Q.No. 1)
1.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float a=0.7;
    if(a < 0.7)
        printf("C\n");
    else
        printf("C++\n");
    return 0;
}
C
C++
Compiler error
Non of above
Answer: Option
Explanation:

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

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

Output:
0.7000000000 0.6999999881

Discussion:
21 comments Page 1 of 3.

Ichidan said:   9 years ago
The answer is, it depends on the compiler and target architecture.

You see float & double precision are encoded as 32 & 64 bit values. The encoding structure can be found on IEEE standards (Google float IEEE). The value 0.7 cannot be represented as either a float or a double. It can only be approximated. This approximation is more accurate with double precision than single (float) precision.

Now some compiler-architectures are 32-bit. Especially the systems of the 90s. So what happens is:

float x = 0.7;

If (x == 0.7) printf ("32-bit x is the same value as 32-bit 0.7. This compiler defaults to 32-bit precision, perhaps this is 32-bit only hardware\n");

Else printf ("32-bit x is not the same value as 64-bit 0.7. This compiler defaults to 64-bit precision perhaps this is 64-bit hardware\n");

Aneesh said:   7 years ago
Hey Guys!

After looking for solutions here i found some gave declaration as odd value in float prints "c" and even value prints "c++".

I want to clarify that this is not true,

Float value from 0.2-0.6 gives odd & even both gives output as "c++" but 0.7 gives output as "c" and again 0.8 is printed as "c++" but its different with 0.9 it prints "c".

Looking for a perfect solution please to share it will help a lot.

Thank you.

Thangavel.V said:   1 decade ago
First what you should know float range value and double range value before to find out solution:

The range of double is -1.7e-308 to 1.7e+308.
The range of float is -3.4e-38 to 3.4e+38.

They are given float a=0.7---->(double constant,0.7)
(float value,a)
printf("%.10f %.10f\n",0.7, a);

0.7000000000 0.6999999881

Just you consider range value and solve it...whatever i know that only put.

Ankita s said:   1 decade ago
Guys. I found that when I was compiling with 0.7 value. I was getting "c" as output same with 0.9 or 0.1. As I think while entering odd values after decimal. Its output is "c"

And while entering even values its output is "C++"

Odd.
0.9
It is checking,
if a<0.9
i.e 0.899999762<0.9

Even...
0.8
It is checking
if a<0.8
i.e 0.80000000<0.800000119

Prasad said:   1 decade ago
Guys if the value in 0.70000 (the range after decimal don't include 7 from next is between (0 to 5) is considered as double and (6 to 9) is considered as float) so here in 0.6999 we have 9 so it is considered as float and 0.7000 is considered as double because of 0 in it.

Shiwam said:   1 decade ago
Hey gys!
it prints c++ for a=0.1 to 0.6
and from 0.7 and afterwards it prints c.
again from 0.8 it prints c++.
it implies 0.7 is a special case that may be related to range of float and double.
so i m searching and pls you also.
Good luck.

Nive said:   1 decade ago
#include<stdio.h>
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}

why it prints the c++ as output? pls xplain..........

Ramya said:   1 decade ago
Hey I am also searching for that. But couldn't get ya. From 0.1 to 0.6 it prints c++ when it is 0.7 it prints c. How it is possible. If anything special for 0.7 as floating point ? if anyone knows please help me!

Shak said:   1 decade ago
#include<stdio.h>
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}

Why it prints the c++ as output? please explain.

Shafi said:   10 years ago
@Ankitha.S.

You try with 0.5 it gives you c++ why?

All the odd values after decimal will give the same as you assume, but 0.5 is not giving answer C it is giving C++.


Post your comments here:

Your comments will be displayed after verification.