Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105//// Zig has support for IEEE-754 floating-point numbers in these// specific sizes: f16, f32, f64, f80, and f128. Floating point// literals may be written in the same ways as integers but also// in scientific notation://// const a1: f32 = 1200; // 1,200// const a2: f32 = 1.2e+3; // 1,200// const b1: f32 = -500_000.0; // -500,000// const b2: f32 = -5.0e+5; // -500,000//// Hex floats can't use the letter 'e' because that's a hex// digit, so we use a 'p' instead://// const hex: f16 = 0x2A.F7p+3; // Wow, that's arcane!//// Be sure to use a float type that is large enough to store your// value (both in terms of significant digits and scale).// Rounding may or may not be okay, but numbers which are too// large or too small become inf or -inf (positive or negative// infinity)!//// const pi: f16 = 3.1415926535; // rounds to 3.140625// const av: f16 = 6.02214076e+23; // Avogadro's inf(inity)!//// When performing math operations with numeric literals, ensure// the types match. Zig does not perform unsafe type coercions// behind your back://// var foo: f16 = 5; // NO ERROR//// A runtime value can coerce to a different type,// as long as the value is losslessly representable://// var foo: u16 = 5;// var bar: f16 = foo; // NO ERROR (5 fits in f16)//// var foo: u16 = 49876;// var bar: f16 = foo; // ERROR (49876 not representable in f16)////// Please fix the two float problems with this program and// display the result as a whole number.
const print = @import("std").debug.print;
pub fn main() void { // The approximate weight of the Space Shuttle upon liftoff // (including boosters and fuel tank) was 4,480,000 lb. // // We'll convert this weight from pounds to metric units at a // conversion of 0.453592 kg to the pound. const shuttle_weight: f16 = 0.453592 * 4480e3;
// By default, float values are formatted in standard decimal // notation. Experiment with '{d}' and '{d:.3}' to see how // decimal formatting works, or try '{e}' and '{e:.3}' for // scientific notation. // NOTE: The weight of the shuttle is a huge number, a scientific notation // may be more appropriate. print("Shuttle liftoff weight: {d:.0} metric tons\n", .{shuttle_weight / 1e3});}
// Floating further://// As an example, Zig's f16 is a IEEE 754 "half-precision" binary// floating-point format ("binary16"), which is stored in memory// like so://// 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0// | |-------| |-----------------|// | exponent significand// |// sign//// This example is the decimal number 3.140625, which happens to// be the closest representation of Pi we can make with an f16// due to the way IEEE-754 floating points store digits://// * Sign bit 0 makes the number positive.// * Exponent bits 10000 are a scale of 16.// * Significand bits 1001001000 are the decimal value 584.//// IEEE-754 saves space by modifying these values: the value// 01111 is always subtracted from the exponent bits (in our// case, 10000 - 01111 = 1, so our exponent is 2^1) and our// significand digits become the decimal value _after_ an// implicit 1 (so 1.1001001000 or 1.5703125 in decimal)! This// gives us://// 2^1 * 1.5703125 = 3.140625//// Feel free to forget these implementation details immediately.// The important thing to know is that floating point numbers are// great at storing big and small values (f64 lets you work with// numbers on the scale of the number of atoms in the universe),// but digits may be rounded, leading to results which are less// precise than integers.//// Fun fact: sometimes you'll see the significand labeled as a// "mantissa" but Donald E. Knuth says not to do that.//// C compatibility fact: There is also a Zig floating point type// specifically for working with C ABIs called c_longdouble.