Added notes to exercise 94 c_math.

This commit is contained in:
Chris Boesch 2024-02-28 13:01:11 +01:00
parent 48b2032024
commit 9e48c9a339
2 changed files with 18 additions and 11 deletions

View file

@ -1,19 +1,26 @@
// //
// Often, C functions are used where no equivalent Zig function exists // Often, C functions are used where no equivalent Zig function exists
// yet. Since the integration of a C function is very simple, as already // yet. Okay, that's getting less and less. ;-)
//
// Since the integration of a C function is very simple, as already
// seen in the last exercise, it naturally offers itself to use the // seen in the last exercise, it naturally offers itself to use the
// very large variety of C functions for our own programs. // very large variety of C functions for our own programs.
// As an example: // As an example:
// //
// Let's say we have a given angle of 765.2 degrees. If we want to // Let's say we have a given angle of 765.2 degrees. If we want to
// normalize that, it means that we have to subtract X * 360 degrees // normalize that, it means that we have to subtract X * 360 degrees
// to get the correct angle. How could we do that? A good method is // to get the correct angle.
// to use the modulo function. But if we write "765.2 % 360", it won't // How could we do that? A good method is to use the modulo function.
// work, because the standard modulo function works only with integer // But if we write "765.2 % 360", it only works with float values
// values. In the C library "math", there is a function called "fmod"; // that are known at compile time.
// the "f" stands for floating and means that we can solve modulo for // In Zig, we would use %mod(a, b) instead.
// real numbers. With this function, it should be possible to normalize //
// our angle. Let's go. // Let us now assume that we cannot do this in Zig, but only with
// a C function from the standard library. In the library "math",
// there is a function called "fmod"; the "f" stands for floating
// and means that we can solve modulo for real numbers. With this
// function, it should be possible to normalize our angle.
// Let's go.
const std = @import("std"); const std = @import("std");

View file

@ -1,6 +1,6 @@
--- exercises/094_c_math.zig 2023-10-22 14:00:02.909379696 +0200 --- exercises/094_c_math.zig 2024-02-28 12:50:35.789939935 +0100
+++ answers/094_c_math.zig 2023-10-22 14:02:46.709025235 +0200 +++ answers/094_c_math.zig 2024-02-28 12:53:57.910309471 +0100
@@ -19,7 +19,7 @@ @@ -26,7 +26,7 @@
const c = @cImport({ const c = @cImport({
// What do we need here? // What do we need here?