+ 4
How can int and float be equal?(see code)
20 Antworten
+ 26
== is comparing the value of that variable .. not comparing data type 😅
and we know very well 5.0 and 5 are equals that's why 🤷🏻♂️
+ 10
I would like to add, that comparing 5 to 5.0 isn't save and the result may depend on the used compiler (and interpreter and programming language)
This is due to "floating point precision".
The thing is that floating point values (float, double, decimal...) cannot be stored with infinite precision. Therefore the value of 5.0 might not necessarily be represented as 5 but could be 5.00000000000000001. Hence, equality comparing with floating point values can result in unexpected outcomes.
+ 8
🤦 really u need learn lot of things about programming algorithm 😇
every programing language have some rules 🤷🏻♂️
+ 6
gaurav kumar 😇 it's ohk yrr we are also learner 😎btw great question keep learning 😇
+ 5
In contrast, the int value beneath the surface will get turned into float value, and as 5 turns into 5.0
The comparison, 5.0==5.0 , is approved
+ 5
Ultimately the point is that types are primarily about how the computer stores values and about how it interprets stored values. But types shouldn't interfere with things like basic mathematical calculations. So under the hood programming languages make sure that 5==5.0 is true. If you couldn't mix int and float the uses for int would be extremely limited because everyone would use float whenever there is even a remote chance that you have to use a value in combination with floats.
+ 2
What would be the purpose of a language where 5==5.0 is not true? It would lead to all kinds of hell for debugging.
+ 2
Saurya Ok sorry bro for that i am a beginner . May be i am arguing about something i may not know
+ 1
gaurav kumar he already told you the rule: "==" compares the values, not the types.
+ 1
while compare integer x with floating value y, the value of x is converted in to float value 5.0 .... hence both will be equal.
so x and y are equal will be printed.
+ 1
Abraham I see your point. But I don't think that makes the answers above wrong. To me this is a difference of levels, not a conflict of claims.
Of course the types play a role in the comparison. But 5==5.0 is true because it is not a statement about types, but about values. Of course the compiler has to take types into account to facilitate this comparison. What I meant when I wrote that the equality operator does not compare the types is that the result says nothing about the types. So the fact that 5==5.0 is true does not mean that 5 and 5.0 have the same type. In other words it doesn't tell you anything about equality of types, just about equality of values.
I would characterise this as a high-level (or -- if you prefer the term -- a superficial) answer whereas I would characterise your explanation as a low-level (or -- if you prefer the term -- deep) answer.
+ 1
Then guys what u all would like to say about this code? Anyone......
main()
{
float a = 5.1;
if ( a == 5.1 )
printf("IN IF");
else
printf("IN ELSE");
}
0
Abraham I don't see how any of the answers above contradict what you're saying. In my opinion they just don't give as much technical detail.
0
I would argue that whether it is an OVERsimplification depends on your goal. If you just want to know why 5==5.0 is evaluated as true it's enough to say that the difference of type is irrelevant for the result. Of course if you want to understand what is going on and why the difference of types doesn't result in 5==5.0 being evaluated as false you need to go into the technical details you provided. In a way it's the difference between why and how.
Just like the answer to the question why the moon doesn't just hurl into outer space can be as simple as saying it's because of gravitation and as complex as an explanation of the general theory of relativity.
Of course you're right when you say that to truly understand it you need to know the details.
0
Use cast with keyword (int) front a variable of type float
- 1
"If" is used to check whether the statement in the braces ( )
is correct.
If (5>6) {
cout<<"yess!" };
This won't print anything, as 5 is not greater than 6.
If (10 >4) {
cout <<"yess"};
This will print yess, as 10 is greater than 4.
In your case int and float have the same value😉
- 1
No
- 2
how does indentation works