···5959// | exponent significand
6060// |
6161// sign
6262-//
6262+//
6363// This example is the decimal number 3.140625, which happens to
6464// be the closest representation of Pi we can make with an f16
6565// due to the way IEEE-754 floating points store digits:
···8686//
8787// Fun fact: sometimes you'll see the significand labeled as a
8888// "mantissa" but Donald E. Knuth says not to do that.
8989-//
8989+//
9090// C compatibility fact: There is also a Zig floating point type
9191// specifically for working with C ABIs called c_longdouble.
+1-1
exercises/061_coercions.zig
···2222// const arr: [3]u8 = [3]u8{5, 6, 7};
2323// const s: []const u8 = &arr; // to slice
2424// const p: [*]const u8 = &arr; // to many-item pointer
2525-//
2525+//
2626// 4. Single-item mutable pointers can coerce to single-item
2727// pointers pointing to an array of length 1. (Interesting!)
2828//
+2-2
exercises/064_builtins.zig
···11//
22// The Zig compiler provides "builtin" functions. You've already
33// gotten used to seeing an @import() at the top of every
44-// Ziglings exercise.
44+// Ziglings exercise.
55//
66// We've also seen @intCast() in "016_for2.zig", "058_quiz7.zig";
77// and @enumToInt() in "036_enums2.zig".
···5151 // 1111 + 1 = 0000 Yes! (Real answer is 10000)
5252 // 0000 + 1 = 0001 Yes!
5353 // 0001 + 1 = 0010 Yes!
5454- //
5454+ //
5555 // Also, check out our fancy formatting! b:0>4 means, "print
5656 // as a binary number, zero-pad right-aligned four digits."
5757 print("{b:0>4} + {b:0>4} = {b:0>4} ({})", .{a, b, my_result, overflowed});
+6-6
exercises/065_builtins2.zig
···11//
22// Zig has builtins for mathematical operations such as...
33//
44-// @sqrt @sin @cos
44+// @sqrt @sin @cos
55// @exp @log @floor
66//
77// ...and lots of type casting operations such as...
···2020// by exploring just THREE of Zig's MANY introspection abilities:
2121//
2222// 1. @This() type
2323-//
2323+//
2424// Returns the innermost struct, enum, or union that a function
2525// call is inside.
2626//
2727// 2. @typeInfo(comptime T: type) @import("std").builtin.TypeInfo
2828-//
2828+//
2929// Returns information about any type in a TypeInfo union which
3030// will contain different information depending on which type
3131// you're examining.
3232-//
3232+//
3333// 3. @TypeOf(...) type
3434-//
3434+//
3535// Returns the type common to all input parameters (each of which
3636// may be any expression). The type is resolved using the same
3737// "peer type resolution" process the compiler itself uses when
···4646 me: *Narcissus = undefined,
4747 myself: *Narcissus = undefined,
4848 echo: void = undefined,
4949-4949+5050 fn fetchTheMostBeautifulType() type {
5151 return @This();
5252 }
+1-1
exercises/066_comptime.zig
···2727//
2828// Zig takes these concepts further by making these optimizations
2929// an integral part of the language itself!
3030-//
3030+//
3131const print = @import("std").debug.print;
32323333pub fn main() void {
+1-1
exercises/067_comptime2.zig
···88// --o-- comptime * | .. .
99// * | * . . . . --*-- . * .
1010// . . . . . . . . . | . . .
1111-//
1111+//
1212// When placed before a variable declaration, 'comptime'
1313// guarantees that every usage of that variable will be performed
1414// at compile time.
+1-1
exercises/069_comptime4.zig
···4949 while (i < size) : (i += 1) {
5050 sequence[i] = @intCast(T, i) + 1;
5151 }
5252-5252+5353 return sequence;
5454}
···4949 '*' => value *= digit,
5050 else => unreachable,
5151 }
5252- // ...But it's quite a bit more exciting than it first appears.
5252+ // ...But it's quite a bit more exciting than it first appears.
5353 // The 'inline while' no longer exists at runtime and neither
5454 // does anything else not touched directly by runtime
5555 // code. The 'instructions' string, for example, does not
···6161 // code at compile time. Guess we're compiler writers
6262 // now. See? The wizard hat was justified after all.
6363 }
6464-6464+6565 print("{}\n", .{value});
6666}
+1-1
exercises/073_comptime8.zig
···5656}
57575858// Fun fact: this assert() function is identical to
5959-// std.debug.assert() from the Zig Standard Library.
5959+// std.debug.assert() from the Zig Standard Library.
6060fn assert(ok: bool) void {
6161 if (!ok) unreachable;
6262}
+1-1
exercises/075_quiz8.zig
···6969//
7070// For example, we could create our own "path language" and
7171// create Paths from that. Something like this, perhaps:
7272-//
7272+//
7373// a -> (b[2])
7474// b -> (a[2] d[1])
7575// c -> (d[3] e[2])
+1-1
exercises/077_sentinels2.zig
···2222// Versatility! Zig strings are compatible with C strings (which
2323// are null-terminated) AND can be coerced to a variety of other
2424// Zig types:
2525-//
2525+//
2626// const a: [5]u8 = "array".*;
2727// const b: *const [16]u8 = "pointer to array";
2828// const c: []const u8 = "slice";
+1-1
exercises/079_quoted_identifiers.zig
···1313// past the authorities: the @"" identifier quoting syntax.
1414//
1515// @"foo"
1616-//
1616+//
1717// Please help us safely smuggle these fugitive identifiers into
1818// our program:
1919//
+1-1
exercises/082_anonymous_structs3.zig
···2929// If a .{} thing is what the print function wants, do we need to
3030// break our "tuple" apart and put it in another one? No! It's
3131// redundant! This will print the same thing:
3232-//
3232+//
3333// print("{} {}\n", foo);
3434//
3535// Aha! So now we know that print() takes a "tuple". Things are
+1-1
exercises/084_async.zig
···3737// fn bar() void {
3838// fooThatSuspends();
3939// }
4040-//
4040+//
4141// 6. The main() function cannot be async!
4242//
4343// Given facts 3 and 4, how do we fix this program (broken by facts
···1010// fn bar() void {
1111// fooThatMightSuspend(true); // Now bar() is async!
1212// }
1313-//
1313+//
1414// But if you KNOW the function won't suspend, you can make a
1515// promise to the compiler with the 'nosuspend' keyword:
1616//