diff options
author | Chris Boesch <chrboesch@noreply.codeberg.org> | 2023-02-18 23:52:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-18 23:52:08 +0100 |
commit | f6a2356bea3ae61971c5f8f69308b3a80e9f78ee (patch) | |
tree | 26305b3bae8ccdabfb23bddc0ca3b0038f20f0f0 /exercises/094_c_math.zig | |
parent | 6353299cd622141333fcb0e2ae11118465fee9ea (diff) | |
parent | 23f2cc88d268ecc1ed0cd5f70a315b02f94ff21b (diff) |
Merge pull request #186 from chrboesch/c_math
added C math exercise
Diffstat (limited to 'exercises/094_c_math.zig')
-rw-r--r-- | exercises/094_c_math.zig | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/exercises/094_c_math.zig b/exercises/094_c_math.zig new file mode 100644 index 0000000..a8596b7 --- /dev/null +++ b/exercises/094_c_math.zig @@ -0,0 +1,33 @@ +// +// Often C functions are used where no equivalent Zig function exists +// yet. Since the integration of a C function is very simple as already +// seen in the last exercise, it naturally offers itself to use the +// very large variety of C functions for the own programs. +// In addition immediately an example: +// +// 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 +// to get the correct angle. How could we do that? A good method is +// to use the modulo function. But if we write "765.2 % 360", it won't +// work, because the standard modulo function works only with integer +// values. In the C 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 angel. Let's go. + +const std = @import("std"); + +const c = @cImport({ + // What do wee need here? + ??? +}); + +pub fn main() !void { + const angel = 765.2; + const circle = 360; + + // Here we call the C function 'fmod' to get our normalized angel. + const result = c.fmod(angel, circle); + + std.debug.print("The normalized angle of {d: >3.1} degrees is {d: >3.1} degrees.\n", .{ angel, result }); +} |