How can int and float be equal?(see code) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
27th Dec 2021, 2:04 PM
gaurav kumar
gaurav kumar - avatar
19 Answers
+ 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 🤷🏻‍♂️
27th Dec 2021, 2:17 PM
Saurya
Saurya - avatar
+ 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.
27th Dec 2021, 3:53 PM
Lisa
Lisa - avatar
+ 8
🤦 really u need learn lot of things about programming algorithm 😇 every programing language have some rules 🤷🏻‍♂️
27th Dec 2021, 2:24 PM
Saurya
Saurya - avatar
+ 6
gaurav kumar 😇 it's ohk yrr we are also learner 😎btw great question keep learning 😇
27th Dec 2021, 2:33 PM
Saurya
Saurya - avatar
+ 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
27th Dec 2021, 2:28 PM
マッドキング♣️✨♣️ [MadKing]
マッドキング♣️✨♣️ [MadKing] - avatar
+ 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.
27th Dec 2021, 2:37 PM
Simon Sauter
Simon Sauter - avatar
+ 4
In normal terms both 5.0 and 5 are equal and here in C too we are using == to compare the values and not the data type so it prints they are equal
27th Dec 2021, 2:24 PM
Aysha
Aysha - avatar
+ 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.
27th Dec 2021, 2:25 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Saurya Ok sorry bro for that i am a beginner . May be i am arguing about something i may not know
27th Dec 2021, 2:31 PM
gaurav kumar
gaurav kumar - avatar
+ 1
gaurav kumar he already told you the rule: "==" compares the values, not the types.
27th Dec 2021, 2:27 PM
Simon Sauter
Simon Sauter - avatar
+ 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.
28th Dec 2021, 2:56 PM
Dipti_lyen
+ 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.
28th Dec 2021, 6:30 PM
Simon Sauter
Simon Sauter - avatar
+ 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"); }
29th Dec 2021, 9:44 AM
Neeraj Kumar
Neeraj Kumar - avatar
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.
28th Dec 2021, 4:23 PM
Simon Sauter
Simon Sauter - avatar
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.
28th Dec 2021, 8:50 PM
Simon Sauter
Simon Sauter - avatar
0
Use cast with keyword (int) front a variable of type float
29th Dec 2021, 2:32 AM
Lena Blondel
Lena Blondel - avatar
- 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😉
28th Dec 2021, 3:17 PM
Hallucinatory
Hallucinatory - avatar
- 1
No
28th Dec 2021, 7:50 PM
knox D
- 2
how does indentation works
28th Dec 2021, 4:13 PM
Sheriff Oladimeji
Sheriff  Oladimeji - avatar