Kindly explain me the output/concept behind these 2 pieces of code.
float x;
x=(float)3.3==3.3;
printf("%f",x);
The output for above is 0.000000
float x;
x=(float)3.5==3.5;
printf("%f",x);
The output for above is 1.000000
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In C source text, the numeral “3.3” stands for a value of the double type that is very close to the number 3.3. Good compilers convert “3.3” to the value of the double type that is closest to 3.3, which is 3.29999999999999982236431605997495353221893310546875, but the C standard does not require that.
The expression
(float) 3.3converts this double to float, which produces the value of the float type that is closest to the double. That value is 3.2999999523162841796875.Then the expression
(float) 3.3 == 3.3compares the float value to the double value. (This implicitly converts the float to double, but the value does not change during this conversion.) Since the values are inequal, the result of the comparison is 0.Since 3.5 is exactly representable as a double, the numeral “3.5” produces exactly the value 3.5. This is also exactly representable as a float, so converting it to float produces 3.5. Then comparing the two values produces 1.
(This answer assumes IEEE 754 floating point, which is very common.)