fork(2) download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <assert.h>
  5.  
  6. float make_float(uint32_t random23)
  7. {
  8. uint32_t pattern = 0x3f800000;
  9.  
  10. random23 &= 0x7fffff;
  11. pattern |= random23;
  12.  
  13. assert(sizeof(float) == sizeof(uint32_t));
  14. char buffer[sizeof(float)];
  15. memcpy(buffer, &pattern, sizeof(float));
  16. float f;
  17. memcpy(&f, buffer, sizeof(float));
  18.  
  19. return f - 1.0;
  20. }
  21.  
  22. float make_float2(uint32_t random23)
  23. {
  24. random23 &= 0x7fffff;
  25. return (float)random23 / (1 << 23);
  26. }
  27.  
  28. int main(void) {
  29. for (uint32_t i = 0; (i >> 23) < 1; i++) {
  30. float a = make_float(i), b = make_float2(i);
  31. if (a != b) {
  32. printf("Mismatch: %d -> %.20f != %.20f!\n", i, a, b);
  33. return 1;
  34. }
  35. }
  36. printf("No mismatch found.\n");
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.07s 2112KB
stdin
Standard input is empty
stdout
No mismatch found.