c - casting signed to double different result than casting to float then double -
Then, as part of an assignment, I am working on an expression: (double) (float) x == (Double) x 1 or not. (X is a signed integer)
It works for every value except INT_MAX. I was wondering why this is so? If I print the values, then they both show the same value for INT_MAX.
x = INT_MAX; Printf ("signed X:% d \ n", x); Float fx1 = (float) x; Double dx 1 = (double) x; Double dfx = (double) (float) x; Printf ("(double) x:% g \ n", dx1); Printf ("(float) x:% f \ n", fx1); Printf ("(double) (float) x:% g \ n", dfx); If ((double) (float) x == (double) x) {printf ("RESULT:% d \ n", ((double) (float x == (double) x)); } Edit: The entire program:
contains # lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; #to & lt include, limits.h & gt; Int main (int argc, char * argv []) {// create random values int x = INT_MAX; Printf ("signed X:% d \ n", x); Float fx1 = (float) x; Double dx 1 = (double) x; Double dfx = (double) (float) x; Printf ("(double) x:% g \ n", dx1); Printf ("(float) x:% f \ n", fx1); Printf ("(double) (float) x:% g \ n", dfx); If ((double) (float) x == (double) x) {printf ("RESULT:% d \ n", ((double) (float x == (double) x); } Return 0; } The end of the main task
Int < / Code> and float in their representation is likely to have the same number of bits, i.e. 32. In float there is a mantissa, an exponent and a signal bit, so i Must be at least bitta, which is required for large int values like INT_MAX . Therefore, lack of accuracy while storing in float
Comments
Post a Comment