+ 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; }
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.
+ 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.
+ 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";
+ 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
+ 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.
+ 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.
+ 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
+ 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.
+ 2
thank u all guys
+ 1
Aksa P Abraham if u run this program then the output results in yes not no
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
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.
0
why compiler do that....
I think those guys who previous coded the compiler were not good coders
- 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
- 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.
- 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.
- 2
es algo simple 😉,Buen empiezo
- 2
no
- 3
ㄱㄷ
- 3
no