Why the answer of the question below is yes but not no? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Why the answer of the question below is yes but not no?

#include <iostream> using namespace std; int main() { float a=0.7; if(a<0.7) cout<<"yes"; else cout<<"no"; return 0; }

21st Aug 2017, 5:28 AM
Kashish Gupta
Kashish Gupta - avatar
21 Answers
+ 42
because a is 0.69999999998 in it's floating representation. When you assign a=0.7 in memory 0.7 cannot be represented in double precision as you would have thought. So the comparison between float and double leads to type promotion and in that case a is less than 0.7 which is double.
21st Aug 2017, 6:04 AM
Dev
Dev - avatar
+ 10
Interesting, I want to know too, that why it says yes, and when we declare it as double, it says no So searched and found that floating point numbers are saved like this: 0.7 -> 0.699999 and that's why.
21st Aug 2017, 6:05 AM
Ali Rashidi
Ali Rashidi - avatar
+ 8
The only difference between the float and double precision is the number of 9's at the right hand of floating point. 0.6999999999999 vs. 0.699999999999999999999998 float a = 0.7f; // 0.6999999999999 double b = 0.7; // 0.699999999999999999999998 if (a < b) cout << "Yup";
21st Aug 2017, 6:38 AM
Babak
Babak - avatar
+ 6
Hello all, I went through the answers, but none of them is truly right... @~Dev was the nearest, and the answer for this question is really yes but as @Lakshay pointed out, in general cases, if you compare the same number in double with float the answer can be both yes or no, it's upredictable! The reason behind it is the way floats and doubles are stored binary in the memory. Double has 2* precision of float and that's all. But it's important to understand, that you can't say such a thing that the float one is 6,999 and the other is 0.7, because it's not necessary true! Either the double and the float value is just nearly 0.7 if the precision is not enough to store the actual number fully. This code will help to understand, how the computer stores binary, and the values: https://code.sololearn.com/cdBZWq94tz8y/?ref=app
22nd Aug 2017, 11:26 PM
Hajnal Máté
Hajnal Máté - avatar
+ 5
When i tried this code with 0.7, the answer was yes but when i replaced it with 0.3, 0.8 and some others the answer was no. If the reason given in most of the answers is correct it should apply on other numbers too.
22nd Aug 2017, 2:01 PM
Lakshay
Lakshay - avatar
+ 3
Actually because 0.7 is stored in a as 0.69999999999999 The problem is that there's no exact way to represent most decimal numbers like 0.1, 0.2, 0.3, etc.
21st Aug 2017, 6:03 AM
Babak
Babak - avatar
+ 3
When i tried this code with 0.7, the answer was yes but when i replaced it with 0.3, 0.8 and some others the answer was no. If the reason given in most of the answers is correct it should apply on other numbers too. https://code.sololearn.com/cN4FvOeRHZ8R/?ref=app
22nd Aug 2017, 2:08 PM
Lakshay
Lakshay - avatar
+ 2
because a=0.7 and yes is applied for less than 0.7 so it's not correct.if yes is not correct than obviously not is correct.
22nd Aug 2017, 8:03 AM
M Choudhary
M Choudhary - avatar
+ 2
thank u all guys
24th Aug 2017, 2:08 AM
Darwin
Darwin - avatar
+ 1
Aksa P Abraham if u run this program then the output results in yes not no
22nd Aug 2017, 4:39 AM
Kashish Gupta
Kashish Gupta - avatar
0
Because the value of A is 0 .7 and we want to check condition if(a<0.7) the value already defined Ais 0.7 for checking condition it will come like if(0.7<0.7) so it is not true so that if condition skip and go to else condition. The answer is No
22nd Aug 2017, 2:32 AM
Aksa P Abraham
Aksa P Abraham - avatar
0
The answer is yes because compiler will take 0.7 as double value so on comparing float with double the answer will come as false even though they are of same numeric value.
22nd Aug 2017, 9:57 AM
Shamayita Dutta
Shamayita Dutta - avatar
0
why compiler do that.... I think those guys who previous coded the compiler were not good coders
22nd Aug 2017, 1:09 PM
Patrick Augustino
Patrick Augustino - avatar
- 1
This program gives ans Yes not only for 0.7 but also 0.0 & 0.3 why gives ans Yes for only these numbers and No ans gives to others 🤔🤔 any one give ans
22nd Aug 2017, 12:48 AM
Damodar Joshi
Damodar Joshi - avatar
- 1
Yeah, I try to code it and the answer is "yes". But if you change the type of var a from float to double, the answer will be "no". But I dont know why it can be like that.
22nd Aug 2017, 1:36 PM
Aprijal Ghiyas
Aprijal Ghiyas - avatar
- 1
To avoid type conversions, use {} member initializers. Because float a{0.7} will throw an error because 0.7 is a double and a float is expected. Then you would find out that 0.7F is expected for floats.
22nd Aug 2017, 9:20 PM
Timon Paßlick
- 2
es algo simple 😉,Buen empiezo
22nd Aug 2017, 5:36 PM
luis daza
- 2
no
22nd Aug 2017, 6:37 PM
Heena
- 3
ㄱㄷ
21st Aug 2017, 1:05 PM
Steve Shin
Steve Shin - avatar
- 3
no
22nd Aug 2017, 1:30 AM
Pretheve Bieber
Pretheve Bieber - avatar