Skip to content

Commit 7ea7fe2

Browse files
authored
math: add Round (#59)
1 parent 4b331ac commit 7ea7fe2

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

math/round.jule

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn Round(n: f64): f64 {
2+
if n >= 0 {
3+
return f64(int(n + 0.5))
4+
}
5+
return f64(int(n - 0.5))
6+
}

math/round_test.jule

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#build test
2+
3+
use "std/testing"
4+
5+
#test
6+
fn testRound(t: &testing::T) {
7+
t.Assert(Round(12.2) == 12, "12.2 should round to 12")
8+
t.Assert(Round(12.5) == 13, "12.5 should round to 13")
9+
t.Assert(Round(12.8) == 13, "12.8 should round to 13")
10+
t.Assert(Round(12.0) == 12, "12.0 should round to 12")
11+
t.Assert(Round(-12.2) == -12, "-12.2 should round to -12")
12+
t.Assert(Round(-12.5) == -13, "-12.5 should round to -13")
13+
t.Assert(Round(-12.8) == -13, "-12.8 should round to -13")
14+
t.Assert(Round(0.49) == 0, "0.49 should round to 0")
15+
t.Assert(Round(0.5) == 1, "0.5 should round to 1")
16+
t.Assert(Round(-0.49) == 0, "-0.49 should round to 0")
17+
t.Assert(Round(-0.5) == -1, "-0.5 should round to -1")
18+
}

0 commit comments

Comments
 (0)