
- Forum
- Programming Talk
- C and C++
- Problem with float
Problem with float
This is a discussion on Problem with float within the C and C++ forums, part of the Programming Talk category; When I compiled and ran my C program given below: #include <stdio.h> main() { float c=0.9 if(c<0.9) printf("Welcome"); else printf("Bye"); ...
-
06-05-2007, 03:57 AM #1
- Join Date
- Apr 2006
- Answers
- 124
Problem with float
When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}
The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
-
06-06-2007, 04:35 AM #2
- Join Date
- May 2007
- Answers
- 12
you must postfix "f" when assingn constants float values like
float c=0.9f;
and rest of statements
-
The output was welcome because of the reason of precision consideration. You have declared variable c as float and stored value 0.9 in that. But inside the memory it would get stored as a value with a little less value than 0.9 due to precision consideration and so when you compare c with 0.9 as c<0.9 it returns true and prints welcome. If you want to print Bye in the above program you can declare variable c as long double as
long double c;
-
06-11-2007, 10:59 AM #4
- Join Date
- Jun 2007
- Answers
- 4
hey
do u check the result for 1.9,2.9,3.9,4.9,...
for 0.9 n 1.9 the o/p is welcome
for the remaining ....2.9,3.9....the o/p will b bye
check it
-
07-02-2007, 06:09 AM #5
- Join Date
- Jul 2007
- Answers
- 3
Reg Float point comparison
<p>Hello Rahul..<br>
Here is an explanation to your doubt..<br>
in your example you have specified the value of c as 0.9<br>
as we know well floating point variables can hold upto 4 bytes in length.<br>
so the actual value of c will be assigned by the compiler as 0.90000<br>
take a note of the trailing 4 characters after 9.<br>
so, when you try to compare floating point variables / numbers, compare it with<br>
its full precision of completed float (double) format to get your expected
results..<br>
hope you understood.. in case of any suggestions to my above explanation, do <br>
drop a mail to bairavi DOT gita AT gmail DOT com<br>
Regards,<br>
Bairavi</p>
-
09-10-2007, 03:37 AM #6
- Join Date
- Sep 2007
- Answers
- 1
0.89 < 0.90
The ans is as follws
if we convert 5.0 to its binary foramat to sore it in a 4 byte float memory then 5 is equivalent to 101 and it will be stored at mantissa and corerespondingly exponenet will contain 1 as
5.0 = 0.5 x E^1 This is normalized form of floating numbers.
But another form is direct store of integer part as well as fractional part. And this procedure has been explained below to ans your question. Go through it.
Take the following example------------>
5.75
5 % 2 =1
2% 2 = 0
1
So 5 --> 101
and...........
0.75 x 2 = 1.50 ---> 1
0.50 x 2 = 1.00 ---> 1
So 0.75 --> 0.11
So ans 5.75 is 0000000000000101.1100000000000000 in 4 byte float memory
Now consider the case 0.9
0.90 x 2 = 1.80 ---> 1
0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0
0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0
0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0
So this will continue upto infinity
So your actual ans is
A=0000000000000000.1110011001100110 01100110011001100.........
But stored value in memory is
C=0000000000000000.1110011001100110 00000000000000000.........
Thats why C<A.
-
you have written code like c<0.9 in if condition. Insted of write c(float)<0.9 you will get correct output.
-
ooooooooooo
hai ,]When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}
The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
this is amar i am also facing the same problem
-
09-19-2007, 11:33 PM #9
- Join Date
- Nov 2006
- Answers
- 1
It all depends which programming tool do you use. For example, I tried this problem on Dev-C++ 4.9.9.2 and found similar solution as Mr. sunit, only a bit different (may be he used another IDE),and it goes like this:
if (c<(float)0.9)
.
.
.
Because variable c is FLOAT,you must compare it with FLOAT.And yes,we all know from math that 0.9 is FLOAT,but you must explicitly tell compiler that 0.9 is FLOAT,and you do that putting (float) in front of 0.9.
I think this is called OVERRIDING,but I am not sure,because my english is very bad.
-
The answer for ur problem is : In general float precision value is '6'.When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}
The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
that means when u enter a float value of 1.2or 0.9 it will reads it as 0.900000.which is different from 0.9.that is why it is showing wrong out put. if u want to give the exact float value take itas "0.9f"
u will get the right result.
-
Sponsored Ads

Reply With Quote






