Problem in C++ program.....
Hey guys i have a doubt in C++....
Here is a program....
#include
#include
void main()
{
float a=0.7;
cout< if(a<0.7)
{
cout<<"hello";
getch();
}
}
and the output is "0.7hello"
and here is another program....
#include
#include
void main()
{
float a=1.7;
cout< if(a<1.7)
{
cout<<"hello";
getch();
}
}
and the output is "1.7" only.....
Can anyone give me the reason.....
Here is a program....
#include
#include
void main()
{
float a=0.7;
cout< if(a<0.7)
{
cout<<"hello";
getch();
}
}
and the output is "0.7hello"
and here is another program....
#include
#include
void main()
{
float a=1.7;
cout< if(a<1.7)
{
cout<<"hello";
getch();
}
}
and the output is "1.7" only.....
Can anyone give me the reason.....
Replies
-
BCA_GIRL@nicpeeyush i have tried both. In frst coding if we take the condition .......if(a==0.7)...........then it produce the output 0.7 only. I think after intialization of a by 0.7 it assigns less then 0.7 to a. I'm confused also.
-
Manish Goyalnice question peeyush
Here the problem is with data types .
compiler does not know whether 0.7 is float type so it consider it as int data type ,assumes its value as 1
for correct output you can use type casting
I mean here,if you change it to
if(a==(float)0.7)
you will get correct output -
PensuGoyal......actually i have tried it and i can explain the first output.....because when the float 0.7 is stored it get stored as 0.69999998807 and its less than 0.7....so it prints "hello".
But the problem is with second output and it can't be explained using the above logic. Its not an error but there should be some logic behind it. -
Manish GoyalI have already told you that you have to tell compiler that 1.7 is of float type otherwise will assume it as int type
if you try this
if(a>1.7)
then it will print hello since 1.7 >1
but if you use type casting and tell the compiler that 1.7 is of float type like below
float a=1.7;
if(a==(float)1.7)
You will get correct output
It will not print hello if you try either if(a>(float)1.7) or if(a<(float)1.7) -
PensuGot it...thanks....!!
-
PensuGoyal......here is the twist in the story.....this problem is only with 1.7, 2.7 and 3.7 and after these its working fine i.e. its printing "hello". So i don't think its a problem of type casting.
Change the program and put 4.7 or something bigger than that......you will be as surprised as i am.....!!
And one more thing the exactly opposite behavior is noted for .3 i.e (0.3, 1.3, 2.3.....etc).
I think this is bigger than just type casting. -
Manish GoyalI don't know what is the reason behind this that why it is giving different outputs?
All i know is you have to tell compiler that variable is of what type and for that you have to use type casting -
PensuExactly my point one more thing i got is when we directly write 1.7 it doesn't take it as integer it takes it as double (type casting again..!!). So if i write 1.7f or as you said float(1.7) then the problem can be solved. But again the question is if its double then why it is treating 1.7, 2.7 and 3.7.....differently!!
-
BCA_GIRLHi Peeyush,
Try this by taking a of double datatype. Then you will get the right solution. The reasion behind this problem is that, every in CPU decimal artitecture is different. That is why in every compiler we can see such type of problems while performing operations on float variables. So, to prevent such kind of things we should use double variables in place of float. -
Whats In NameFloating Type has some limitations.
It can take 0.7 as 0.6999999998 or as 0.7000000001
So 1.7 can be taken as 1.7000000000001 or 1.69999999998 -
PensuYou are right but the question is how is it possible that the value which is stored in a variable is greater the value i assigned to it. So 0.699999999 is acceptable but 1.7000000004 is not because if its true than it can seriously cause a big damage.....!!
-
Whats In NameIts all about how the conversion is taking place into binary..and thus you should always tell the compiler that it is a float value to avoid complications.
-If I am wrong,please correct me(still learning 😀). -
PensuExactly, its all about the conversion i have tried the conversion and the result for 0.7 is 0.6999999999 which is acceptable because it can never be exact 0.7 but(again!!) the problem is why 1.7, 2.7 and 3.7 are getting stored as .70000000004.......
-
Whats In NameDecimal to Binary conversion.Does it help?
-
PensuYa.....it should as numbers are stored in system as binary only.
-
Whats In NameI will try to search more about it which will clear our confusions.
-
Pensusure.....am waiting....!!
-
anandkumarjha
but it's for sure that if u don't declare type casting before any constant then it will behave like an integer by defaultWhats In NameFloating Type has some limitations.
It can take 0.7 as 0.6999999998 or as 0.7000000001
So 1.7 can be taken as 1.7000000000001 or 1.69999999998
You are reading an archived discussion.
Related Posts
Entrepreneurship Cell, IIT Kharagpur has launched its Business Plan Competitions. The different competitions are as follows:
Concipio: Concipio is the annual in-house BPlan competition, open only to students and alumni...
Hello friends,
I have to add no's of buttons in applet.
But i did not able to add this.Check this code.
import java.awt.*;
import javax.swing.*;
/**/
public...
This is what we received few hours ago. -
I wish to work with CrazyEngineers Team because:
to create a revolution in technology
Article:
PETE Process Promises Successful Technology Fusion...
Hello CEans,
Many of the CEans might be the great testing professionals,
I want advice from such experienced testing professionals,, I want to know how to wirte the best test...
Is there any Short way to reach particular path in regedit??Rather than expanding each and every path......