fork download
  1. #include <stdio.h>
  2. const char digits[] = "0123456789abcdef";
  3.  
  4. union Float64 {
  5. double y;
  6. char bytes[8];
  7. };
  8.  
  9. struct Tdat {
  10. char *txt;
  11. double y;
  12. };
  13.  
  14. static void test(struct Tdat *tst, char *end) {
  15. union Float64 q;
  16. char a, b;
  17.  
  18. q.y = tst->y; // set the double value for the test
  19.  
  20. for (int i = 7; i >= 0; i--) {
  21. a = digits[q.bytes[i] & 0x0f]; // lower nibble
  22. b = digits[(q.bytes[i] & 0xf0) >> 4]; // upper nibble
  23. printf("%c%c ", b, a);
  24. }
  25. printf("%s%c", tst->txt, end);
  26.  
  27. return;
  28. }
  29.  
  30. int main(void) {
  31. // tests only, pure and simple
  32.  
  33. printf("Initial cases:\n");
  34. test(&(struct Tdat){ .y = +1.0, .txt = "(+1.0)" }, '\n');
  35. test(&(struct Tdat){ .y = -1.0, .txt = "(-1.0)" }, '\n');
  36. test(&(struct Tdat){ .y = -0.0, .txt = "(-0.0)" }, '\n');
  37.  
  38. printf("Consecutive and ascending Numbers:\n");
  39. test(&(struct Tdat){ .y = +2.0, .txt = "(+2.0)" }, '\n');
  40. test(&(struct Tdat){ .y = +3.0, .txt = "(+3.0)" }, '\n');
  41. test(&(struct Tdat){ .y = +4.0, .txt = "(+4.0)" }, '\n');
  42. test(&(struct Tdat){ .y = +8.0, .txt = "(+8.0)" }, '\n');
  43. test(&(struct Tdat){ .y = +16.0, .txt = "(+16.0)" }, '\n');
  44.  
  45. printf("Numbers with fractions:\n");
  46. test(&(struct Tdat){ .y = +1.1, .txt = "(+1.1)" }, '\n');
  47. test(&(struct Tdat){ .y = +3.14159, .txt = "(+3.14159)" }, '\n');
  48.  
  49. printf("Fractions ending in 5\n");
  50. test(&(struct Tdat){ .y = +1.5, .txt = "(+1.5)" }, '\n');
  51. test(&(struct Tdat){ .y = +1.25, .txt = "(+1.25)" }, '\n');
  52. test(&(struct Tdat){ .y = +1.125, .txt = "(+1.125)" }, '\n');
  53. test(&(struct Tdat){ .y = +1.0625, .txt = "(+1.0625)" }, '\n');
  54. test(&(struct Tdat){ .y = +1.03125, .txt = "(+1.03125)" }, '\n');
  55.  
  56. printf("Special cases:\n");
  57. test(&(struct Tdat){ .y = +1.0 / +0.0, .txt = "(Inf)" }, '\n');
  58. test(&(struct Tdat){ .y = +0.0 / +0.0, .txt = "(NaN)" }, '\0');
  59.  
  60. return 0;
  61. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Initial cases:
3f f0 00 00 00 00 00 00 (+1.0)
bf f0 00 00 00 00 00 00 (-1.0)
80 00 00 00 00 00 00 00 (-0.0)
Consecutive and ascending Numbers:
40 00 00 00 00 00 00 00 (+2.0)
40 08 00 00 00 00 00 00 (+3.0)
40 10 00 00 00 00 00 00 (+4.0)
40 20 00 00 00 00 00 00 (+8.0)
40 30 00 00 00 00 00 00 (+16.0)
Numbers with fractions:
3f f1 99 99 99 99 99 9a (+1.1)
40 09 21 f9 f0 1b 86 6e (+3.14159)
Fractions ending in 5
3f f8 00 00 00 00 00 00 (+1.5)
3f f4 00 00 00 00 00 00 (+1.25)
3f f2 00 00 00 00 00 00 (+1.125)
3f f1 00 00 00 00 00 00 (+1.0625)
3f f0 80 00 00 00 00 00 (+1.03125)
Special cases:
7f f0 00 00 00 00 00 00 (Inf)
ff f8 00 00 00 00 00 00 (NaN)