Basic behavior of floating point types

  1. /* floats.c
  2. * --------
  3. * A program to explore the behavior of floating point types for lab.
  4. */
  5. #include <float.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. // Just a little helper to convert bool to string value when printing
  10. static char *boolstr(bool b)
  11. {
  12. return b ? "true" : "false";
  13. }
  14. // helper to display raw float bits
  15. static void print_float_bits(float f, const char *name, const char *comment)
  16. {
  17. unsigned int bits = *(unsigned int *)&f; // trick compiler into giving us access to raw bits
  18. unsigned int mask = 1 << 31;
  19. printf(" %d ", (mask & bits) != 0); // msb is sign bit
  20. for (int i = 0; i < 8; i++) // next 8 bits are exponent
  21. printf("%d", ((mask >>= 1) & bits) != 0);
  22. printf(" ");
  23. for (int i = 0; i < 23; i++) // remaining 23 bits are significand
  24. printf("%d", ((mask >>= 1) & bits) != 0);
  25. printf(" raw bits of %s ", name);
  26. if (!comment) printf("(float value = %g)\n", f);
  27. else printf("(%s)\n", comment);
  28. }
  29. /* Function: float_behavior
  30. * ------------------------
  31. * Tries various floating point operations and prints results so you can observe beahvior.
  32. */
  33. static void float_behavior()
  34. {
  35. printf("sizeof(float) = %zu FLT_MIN = %g FLT_MAX = %g FLT_DIG = %d (num decimal digits of precision)\n",
  36. sizeof(float), FLT_MIN, FLT_MAX, FLT_DIG);
  37. printf("sizeof(double) = %zu DBL_MIN = %g DBL_MAX = %g DBL_DIG = %d\n\n",
  38. sizeof(double), DBL_MIN, DBL_MAX, DBL_DIG);
  39. printf("max * 2 = %g\n", DBL_MAX*2);
  40. printf("1.0/0.0 = %g\n", 2.0/0.0);
  41. printf("0.0/0.4 = %g\n", 0.0/0.4);
  42. double infinity = 1.0/0.0;
  43. printf("inf * 2 = %g\n", infinity * 2);
  44. printf("inf - inf = %g\n", infinity - infinity);
  45. printf("does inf == inf? %s, does inf == -inf? %s\n", boolstr(infinity == infinity), boolstr(infinity == -infinity));
  46. double nan = 0.0/0.0;
  47. printf("nan * 2 = %g\n", nan * 2);
  48. printf("nan - nan = %g\n", nan - nan);
  49. printf("does nan == inf? %s, does nan == nan? %s\n", boolstr(nan == infinity), boolstr(nan == nan));
  50. }
  51. /* Function: float_bits
  52. * --------------------
  53. * Prints bit patterns for various float values.
  54. */
  55. static void float_bits()
  56. {
  57. unsigned int rawbits = 1;
  58. float f = *(float *)&rawbits;
  59. printf("\nFloat bit patterns:\n");
  60. print_float_bits(FLT_MAX, "FLT_MAX", NULL);
  61. print_float_bits(FLT_MIN, "FLT_MIN", NULL);
  62. print_float_bits(f, "smallest non-zero denorm", NULL);
  63. print_float_bits(1.0/0.0, "Infinity", NULL);
  64. print_float_bits(0.0/0.0, "nan", NULL);
  65. print_float_bits(1.0, "1.0", NULL);
  66. print_float_bits(1.5, "1.5", "how did float bits change from 1.0 to 1.5?");
  67. print_float_bits(1.25, "1.25", "how did float bits change from 1.5 to 1.25?");
  68. print_float_bits(-1.25, "-1.25", "how did negation change float bits?");
  69. print_float_bits(127.0, "127.0", "note bit pattern for integer 127 appears within float bits!");
  70. print_float_bits(126.0, "126.0", "how did subtract 1 change float bits?");
  71. print_float_bits(126.5, "126.5", "how did add 0.5 change float bits?");
  72. print_float_bits(253.0, "253.0", "how did multiply by 2 change float bits of norm?");
  73. print_float_bits(f, "smallest non-zero denorm", NULL);
  74. print_float_bits(f*2, "denorm*2", "how did multiply by 2 change float bits of min denorm?");
  75. print_float_bits(.5, ".5", "2 to power -1");
  76. print_float_bits(8, "8", "2 to power 3");
  77. print_float_bits(1024, "1024", "2 to power 10");
  78. print_float_bits(32768, "32768", "what do normalized powers of two have in common?");
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. float_behavior();
  83. float_bits();
  84. return 0;
  85. }

Float bits

image.png
image.png

Practice converting normalized floats to decimal

https://www.h-schmidt.net/FloatConverter/IEEE754.html
image.png

Converting decimal to IEEE floating point representation

http://web.stanford.edu/class/cs107/float/convert.html
image.png

Limits of a finite representation (nearest representable floats)

image.png

Floating point arithmetic

image.png