060_floats.zig 3.4 KB

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