060_floats.zig 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.0; // 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. // A float literal doesn't need a decimal point. When performing math
  27. // operations with numeric literals, ensure the types match. Zig
  28. // does not perform unsafe type coercions behind your back:
  29. //
  30. // var foo: f16 = 5; // NO ERROR
  31. // var foo: f16 = @as(u16, 5); // ERROR
  32. //
  33. // Please fix the two float problems with this program and
  34. // display the result as a whole number.
  35. const print = @import("std").debug.print;
  36. pub fn main() void {
  37. // The approximate weight of the Space Shuttle upon liftoff
  38. // (including boosters and fuel tank) was 2,200 tons.
  39. //
  40. // We'll convert this weight from tons to kilograms at a
  41. // conversion of 907.18kg to the ton.
  42. var shuttle_weight: f16 = 907.18 * 2200;
  43. // By default, float values are formatted in scientific
  44. // notation. Try experimenting with '{d}' and '{d:.3}' to see
  45. // how decimal formatting works.
  46. print("Shuttle liftoff weight: {d:.0}kg\n", .{shuttle_weight});
  47. }
  48. // Floating further:
  49. //
  50. // As an example, Zig's f16 is a IEEE 754 "half-precision" binary
  51. // floating-point format ("binary16"), which is stored in memory
  52. // like so:
  53. //
  54. // 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0
  55. // | |-------| |-----------------|
  56. // | exponent significand
  57. // |
  58. // sign
  59. //
  60. // This example is the decimal number 3.140625, which happens to
  61. // be the closest representation of Pi we can make with an f16
  62. // due to the way IEEE-754 floating points store digits:
  63. //
  64. // * Sign bit 0 makes the number positive.
  65. // * Exponent bits 10000 are a scale of 16.
  66. // * Significand bits 1001001000 are the decimal value 584.
  67. //
  68. // IEEE-754 saves space by modifying these values: the value
  69. // 01111 is always subtracted from the exponent bits (in our
  70. // case, 10000 - 01111 = 1, so our exponent is 2^1) and our
  71. // significand digits become the decimal value _after_ an
  72. // implicit 1 (so 1.1001001000 or 1.5703125 in decimal)! This
  73. // gives us:
  74. //
  75. // 2^1 * 1.5703125 = 3.140625
  76. //
  77. // Feel free to forget these implementation details immediately.
  78. // The important thing to know is that floating point numbers are
  79. // great at storing big and small values (f64 lets you work with
  80. // numbers on the scale of the number of atoms in the universe),
  81. // but digits may be rounded, leading to results which are less
  82. // precise than integers.
  83. //
  84. // Fun fact: sometimes you'll see the significand labeled as a
  85. // "mantissa" but Donald E. Knuth says not to do that.
  86. //
  87. // C compatibility fact: There is also a Zig floating point type
  88. // specifically for working with C ABIs called c_longdouble.