Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<conio.h>
#include<stdio.h>         
int main()
{  float x=1.1;
	while(x==1.1)
	{
		printf("%f\n",x);
	    x=x-0.1;
    }   
	getch();
    return 0;
}


What I have tried:

I my opinion the output should be 1.1 but their is NO OUTPUT why????????
when i executed this code on DEV C++ ......
Posted
Updated 17-Mar-18 8:10am
v2

That is because floating point values are not exact due to their internal representation. See these links:
The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic[^]
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]

In your case this is hidden because you are comparing the single precision floating point variable x with the double precision constant 1.1. The compiler will convert your float variable x to double and then compare it. Because 1.1 is one of those values that can't be represented exactly, the comparison fails.

In your case it might work when comparing with a single precision constant (note the f postfix):
while(x==1.1f)

But you should not rely on that. Instead you should use "epsilon comparison" as described at Comparing Floating Point Numbers, 2012 Edition | Random ASCII[^].
 
Share this answer
 
v2
Comments
Jon McKee 17-Mar-18 7:14am    
Nicely explained +1
That's complicated, but in this case it revolves around what numbers actually are.
When you type "1.1" as a number, what type of number is that? We know it's not an integer - because it has a fractional component - so it's a floating point number of some form, but what "size" of floating point number is that? Does it matter?
In C there are two ways to store floating point numbers: float and double (well ... technically there are three: long double is also available) and the difference is no much space they take up. float values generally occupy 4 bytes, and double take 8 becuase they can contain more precise values.

But what is 1.1 as far as the compiler is concerned? Answer: it's a double because that's "the most accurate" and it gets an implicit cast down to float when you do the assignment.
Does it matter? Yes. Because floating point numbers aren't stored as "one one and a point one as well", they are stored in a much more complicated way: Single-precision floating-point format - Wikipedia[^] goes into the details, but becuase it's all bionary instead of base ten, 1.1 doesn't store as an "accurate" number - it's different. And the double version isn't the same as the float version because it can store more binary digits.

The simple solution is to stick to one format:
C++
int main()
{
    float x = 1.1;
    while(x == 1.1F)
    {
    printf("%f\n",x);
    x = x - 0.1F;
    } 
    return 0;
}
Or
C++
int main()
{
    double x = 1.1;
    while(x == 1.1)
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}
And it'll do what you expect.
 
Share this answer
 
Comments
Member 13593396 17-Mar-18 11:38am    
Sir here in statement while(x == 1.1F) why u have used 1.1F why can't we use 1.1 only
OriginalGriff 17-Mar-18 11:54am    
:sigh:
Read what I said above, and have a look at the wiki link.
1.1 is a double, not a float. 1.1F is a float, not a double.
My pre-writers are correct but and the better solution is to write such
a) code that avoids such floating point problems or
b) use some other comparison or work with constants like:
C++
//global constant
const double MY_CONSTANT = 1.1;
int main()
{
    double x = 1.1;
    while(x == MY_CONSTANT)
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}
c) use some defined difference for the check
C++
int main()
{
    double x = 1.1;
    // some diff is accepted
    while( abs(x - MY_CONSTANT) < 0.0000001 )
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}
But always use the higher precision of the data type double
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900