Problem in C++ program.....

Pensu
@pensu-8tNeGU โ€ข Oct 24, 2024

Hey guys i have a doubt in C++....
Here is a program....

#include<iostream.h>
#include<conio.h>
void main()
{
float a=0.7;
cout<<a;
if(a<0.7)
{
cout<<"hello";
getch();
}
}

and the output is "0.7hello"

and here is another program....

#include<iostream.h>
#include<conio.h>
void main()
{
float a=1.7;
cout<<a;
if(a<1.7)
{
cout<<"hello";
getch();
}
}

and the output is "1.7" only.....
Can anyone give me the reason.....

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • BCA_GIRL

    @bca-girl-wzX9cA • Sep 4, 2010

    @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 Goyal

    @manish-r2Hoep • Sep 4, 2010

    nice 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

  • Pensu

    @pensu-8tNeGU • Sep 4, 2010

    Goyal......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 Goyal

    @manish-r2Hoep • Sep 4, 2010

    I 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)

  • Pensu

    @pensu-8tNeGU • Sep 4, 2010

    Got it...thanks....!!

  • Pensu

    @pensu-8tNeGU • Sep 4, 2010

    Goyal......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 Goyal

    @manish-r2Hoep • Sep 4, 2010

    I 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

  • Pensu

    @pensu-8tNeGU • Sep 4, 2010

    Exactly 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_GIRL

    @bca-girl-wzX9cA • Sep 8, 2010

    Hi 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 Name

    @whats-in-name-KdgM7o • Sep 8, 2010

    Floating 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

  • Pensu

    @pensu-8tNeGU • Sep 8, 2010

    You 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 Name

    @whats-in-name-KdgM7o • Sep 8, 2010

    Its 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 ๐Ÿ˜€).

  • Pensu

    @pensu-8tNeGU • Sep 8, 2010

    Exactly, 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 Name

    @whats-in-name-KdgM7o • Sep 8, 2010

    Decimal to Binary conversion.Does it help?

  • Pensu

    @pensu-8tNeGU • Sep 8, 2010

    Ya.....it should as numbers are stored in system as binary only.

  • Whats In Name

    @whats-in-name-KdgM7o • Sep 8, 2010

    I will try to search more about it which will clear our confusions.

  • Pensu

    @pensu-8tNeGU • Sep 8, 2010

    sure.....am waiting....!!

  • anandkumarjha

    @anandkumarjha-jzdDMA • Sep 8, 2010

    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

    but it's for sure that if u don't declare type casting before any constant then it will behave like an integer by default