+ 1
how integer and double(float) are equal ?
11 Antworten
+ 3
In the code it is only checking the values of the variables, its not checking what type it is.
Both of the variables values are same, it is 2.
To check type u have to use typeof function
if( typeof(varOne) == typeof(varTwo) ) {
printf("both are same type");
}
+ 2
For any integer of a reasonable size you can compare it exactly to a float. Once your integer is greater than 2^22 or 2^23, floats can't represent it accurately, esp. if it is odd (if it is even or a multiple of 2^N, the exponent part of the float can compensate for it.
If you compare a double to int, it has a 53 bit mantissa so an integer less than 53 bits will be equal to its double counterpart. Only when you have larger integers your double <> int comparisons will fail.
(to get 64 but it's you may use int, long or long long, fermenting on your compiler, memory model and cpu architecture)
+ 1
Because the double: 2.000000 is equal to the integer: 2. They have the same value
+ 1
You can try same code but with 0.25 value and you will see than a and b will be same value after comparison because either double and float can represent 0.25 value without losing precision (0.25 is a power of 2 or 2 ^ -2)
+ 1
thanks
+ 1
A similar question.
https://www.sololearn.com/Discuss/2580998/?ref=app
0
Slick , Steve Sajeev ok, if this is case then :
#include <stdio.h>
int main() {
float a = 0.2f;
double b = 0.2;
if(a == b) {
printf("hi");
}
else {
printf("bye");
}
return 0;
}
output : bye
why in the above code it is showing out as "bye" , it should show output as "hi" , because here also values are same ?
0
them are not same representation in memory but compiler add some instruction to convert implicitally the int to double for comparison purpose that make them comparable to memory level
0
KrOW and about float and double comparison ?
0
The problem in your second example is precision... float is less precise than double and after conversion to double 'a' will not has same value than 'b' (in this case)
0
Shrinivas Gadade when you compare an integer with a float, the program creates a temporary float copy of the integer and than execute the comparison.
Read this page for more info:
https://overiq.com/c-programming-101/implicit-type-conversion-in-c/