-
@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.
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
Pensu
Member •
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.
Are you sure? This action cannot be undone.
-
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)
Are you sure? This action cannot be undone.
-
Pensu
Member •
Sep 4, 2010
Got it...thanks....!!
Are you sure? This action cannot be undone.
-
Pensu
Member •
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.
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
Pensu
Member •
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!!
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
Pensu
Member •
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.....!!
Are you sure? This action cannot be undone.
-
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 😀).
Are you sure? This action cannot be undone.
-
Pensu
Member •
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.......
Are you sure? This action cannot be undone.
-
Decimal to Binary conversion.Does it help?
Are you sure? This action cannot be undone.
-
Pensu
Member •
Sep 8, 2010
Ya.....it should as numbers are stored in system as binary only.
Are you sure? This action cannot be undone.
-
I will try to search more about it which will clear our confusions.
Are you sure? This action cannot be undone.
-
Pensu
Member •
Sep 8, 2010
sure.....am waiting....!!
Are you sure? This action cannot be undone.
-
Whats In Name
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
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
Are you sure? This action cannot be undone.