-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan contoh math ceil pada zig (#11)
Signed-off-by: slowy07 <slowy.arfy@gmail.com>
- Loading branch information
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters