060_floats.zig 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Zig has support for IEEE-754 floating-point numbers in these
  3. // specific sizes: f16, f32, f64, f80, and f128. Floating point
  4. // literals may be written in the same ways as integers but also
  5. // in scientific notation:
  6. //
  7. // const a1: f32 = 1200; // 1,200
  8. // const a2: f32 = 1.2e+3; // 1,200
  9. // const b1: f32 = -500_000.0; // -500,000
  10. // const b2: f32 = -5.0e+5; // -500,000
  11. //
  12. // Hex floats can't use the letter 'e' because that's a hex
  13. // digit, so we use a 'p' instead:
  14. //
  15. // const hex: f16 = 0x2A.F7p+3; // Wow, that's arcane!
  16. //
  17. // Be sure to use a float type that is large enough to store your
  18. // value (both in terms of significant digits and scale).
  19. // Rounding may or may not be okay, but numbers which are too
  20. // large or too small become inf or -inf (positive or negative
  21. // infinity)!
  22. //
  23. // const pi: f16 = 3.1415926535; // rounds to 3.140625
  24. // const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)!
  25. //
  26. // When performing math operations with numeric literals, ensure
  27. // the types match. Zig does not perform unsafe type coercions
  28. // behind your back:
  29. //
  30. // var foo: f16 = 5; // NO ERROR
  31. //
  32. // var foo: u16 = 5; // A literal of a different type
  33. // var bar: f16 = foo; // ERROR
  34. //
  35. // Please fix the two float problems with this program and
  36. // display the result as a whole number.
  37. const print = @import("std").debug.print;
  38. pub fn main() void {
  39. // The approximate weight of the Space Shuttle upon liftoff
  40. // (including boosters and fuel tank) was 4,480,000 lb.
  41. //
  42. // We'll convert this weight from pounds to metric units at a
  43. // conversion of 0.453592 kg to the pound.
  44. const shuttle_weight: f16 = 0.453592 * 4480e3;
  45. // By default, float values are formatted in scientific
  46. // notation. Try experimenting with '{d}' and '{d:.3}' to see
  47. // how decimal formatting works.
  48. print("Shuttle liftoff weight: {d:.0} metric tons\n", .{shuttle_weight});
  49. }
  50. // Floating further:
  51. //
  52. // As an example, Zig's f16 is a IEEE 754 "half-precision" binary
  53. // floating-point format ("binary16"), which is stored in memory
  54. // like so:
  55. //
  56. // 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0
  57. // | |-------| |-----------------|
  58. // | exponent significand
  59. // |
  60. // sign
  61. //
  62. // This example is the decimal number 3.140625, which happens to
  63. // be the closest representation of Pi we can make with an f16
  64. // due to the way IEEE-754 floating points store digits:
  65. //
  66. // * Sign bit 0 makes the number positive.
  67. // * Exponent bits 10000 are a scale of 16.
  68. // * Significand bits 1001001000 are the decimal value 584.
  69. //
  70. // IEEE-754 saves space by modifying these values: the value
  71. // 01111 is always subtracted from the exponent bits (in our
  72. // case, 10000 - 01111 = 1, so our exponent is 2^1) and our
  73. // significand digits become the decimal value _after_ an
  74. // implicit 1 (so 1.1001001000 or 1.5703125 in decimal)! This
  75. // gives us:
  76. //
  77. // 2^1 * 1.5703125 = 3.140625
  78. //
  79. // Feel free to forget these implementation details immediately.
  80. // The important thing to know is that floating point numbers are
  81. // great at storing big and small values (f64 lets you work with
  82. // numbers on the scale of the number of atoms in the universe),
  83. // but digits may be rounded, leading to results which are less
  84. // precise than integers.
  85. //
  86. // Fun fact: sometimes you'll see the significand labeled as a
  87. // "mantissa" but Donald E. Knuth says not to do that.
  88. //
  89. // C compatibility fact: There is also a Zig floating point type
  90. // specifically for working with C ABIs called c_longdouble.