fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. void dispf(float f)
  5. {
  6. char str[32];
  7. char* pc;
  8. unsigned char* pf;
  9. int i, b;
  10.  
  11. pf = (unsigned char*)&f;
  12. pc = str;
  13. for (i = 0; i < 4; i++) {
  14. for (b = 0; b < 8; b++) {
  15. *pc++ = '0'+((pf[3-i]>>(7-b))&1);
  16. }
  17. }
  18. printf("%.1s %.8s %.23s\n", str, str + 1, str + 9);
  19. printf("%E\n", f);
  20. printf("%f\n", f);
  21. }
  22.  
  23. int main()
  24. {
  25. char s[32];
  26. float a, b, c;
  27.  
  28. printf("a = 1970.199707\n");
  29. a = (float)1970.199707;
  30. dispf(a);
  31. sprintf(s, "%E", a);
  32. printf("b = %s\n", s);
  33. sscanf(s, "%E", &b);
  34. dispf(b);
  35. printf("c = fabsf(a - b)\n");
  36. c = fabsf(a - b);
  37. dispf(c);
  38. printf("%s\n", (c<=0.001)?"true":"false");
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
a = 1970.199707
0 10001001 11101100100011001100100
1.970200E+03
1970.199707
b = 1.970200E+03
0 10001001 11101100100011001100110
1.970200E+03
1970.199951
c = fabsf(a - b)
0 01110011 00000000000000000000000
2.441406E-04
0.000244
true