diff --git a/math/round.jule b/math/round.jule new file mode 100644 index 0000000..a0c4f2e --- /dev/null +++ b/math/round.jule @@ -0,0 +1,6 @@ +fn Round(n: f64): f64 { + if n >= 0 { + return f64(int(n + 0.5)) + } + return f64(int(n - 0.5)) +} \ No newline at end of file diff --git a/math/round_test.jule b/math/round_test.jule new file mode 100644 index 0000000..53eec0e --- /dev/null +++ b/math/round_test.jule @@ -0,0 +1,18 @@ +#build test + +use "std/testing" + +#test +fn testRound(t: &testing::T) { + t.Assert(Round(12.2) == 12, "12.2 should round to 12") + t.Assert(Round(12.5) == 13, "12.5 should round to 13") + t.Assert(Round(12.8) == 13, "12.8 should round to 13") + t.Assert(Round(12.0) == 12, "12.0 should round to 12") + t.Assert(Round(-12.2) == -12, "-12.2 should round to -12") + t.Assert(Round(-12.5) == -13, "-12.5 should round to -13") + t.Assert(Round(-12.8) == -13, "-12.8 should round to -13") + t.Assert(Round(0.49) == 0, "0.49 should round to 0") + t.Assert(Round(0.5) == 1, "0.5 should round to 1") + t.Assert(Round(-0.49) == 0, "-0.49 should round to 0") + t.Assert(Round(-0.5) == -1, "-0.5 should round to -1") +} \ No newline at end of file