Skip to content

Commit

Permalink
feat: menambahkan contoh math ceil pada zig (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: slowy07 <slowy.arfy@gmail.com>
  • Loading branch information
slowy07 authored Dec 10, 2023
1 parent 351c891 commit abbee11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ pub fn build(b: *std.Build) void {
.category = "math",
});

if (std.mem.eql(u8, op, "math/ceil"))
buat_algo(b, .{
.optimize = optimize,
.target = target,
.name = "ceil.zig",
.category = "math",
});

// algoritma/sorting
// bubble sorting
if (std.mem.eql(u8, op, "algorithm/sorting/bubbleSort"))
Expand Down
23 changes: 23 additions & 0 deletions math/ceil.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const std = @import("std");

// fungsi ceil untuk mengembalikan nilai ceil dari suatu angka
pub fn ceil(comptime T: type, x: T) T {
// simpan nilai x yang dibulatkan ke nol untuk memeriksa kondisi pembulatan
// ke atas
const x_bulat_ke_nol = x;

// jika x kurang dari 0 atau x dusah bulat ke 0, kembalikan x yang sudah bulat ke 0
if (x < 0 or x_bulat_ke_nol == x) {
return x_bulat_ke_nol;
} else {
// jika x positif dan belum bulat ke nol, kembalikan x yang sudah bulat ke nol ditambah 1
return x_bulat_ke_nol + @as(T, 1);
}
}

// testing untuk fungsi ceil berfungsi dengan benar
test "testing ceil" {
try std.testing.expectEqual(@as(u4, 9), ceil(u4, 9));
try std.testing.expectEqual(@as(f80, 3263.56), ceil(f80, 3263.56));
try std.testing.expectEqual(@as(u5, 7), ceil(u5, 7));
}
1 change: 1 addition & 0 deletions runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ argumen='--summary all -freference-trace'
# yang sudah dibuat
$ZIG_TEST_COMMAND -Dalgoritma=math/gcd $argumen
$ZIG_TEST_COMMAND -Dalgoritma=math/faktorial $argumen
$ZIG_TEST_COMMAND -Dalgoritma=math/ceil $argumen
$ZIG_TEST_COMMAND -Dalgoritma=algorithm/sorting/bubbleSort $argumen

0 comments on commit abbee11

Please sign in to comment.