Skip to content

Commit ceabdd3

Browse files
committed
pbio/math: Document and test square root.
1 parent 4e63612 commit ceabdd3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/pbio/src/math.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ int32_t pbio_math_clamp(int32_t value, int32_t abs_max) {
116116
return pbio_math_bind(value, -abs_max, abs_max);
117117
}
118118

119+
/**
120+
* Gets the square root.
121+
*
122+
* If @p n <= 0, it returns 0.
123+
*
124+
* @param [in] n The input value
125+
* @return The square root.
126+
*/
119127
int32_t pbio_math_sqrt(int32_t n) {
120128
if (n <= 0) {
121129
return 0;

lib/pbio/test/src/math.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ static void test_clamp(void *env) {
1919
}
2020

2121
static void test_sqrt(void *env) {
22-
tt_want(pbio_math_sqrt(0) == 0);
23-
tt_want(pbio_math_sqrt(1) == 1);
24-
tt_want(pbio_math_sqrt(4) == 2);
25-
tt_want(pbio_math_sqrt(400) == 20);
26-
tt_want(pbio_math_sqrt(40000) == 200);
27-
tt_want(pbio_math_sqrt(400000000) == 20000);
22+
tt_want_int_op(pbio_math_sqrt(0), ==, 0);
23+
tt_want_int_op(pbio_math_sqrt(1), ==, 1);
24+
tt_want_int_op(pbio_math_sqrt(4), ==, 2);
25+
tt_want_int_op(pbio_math_sqrt(400), ==, 20);
26+
tt_want_int_op(pbio_math_sqrt(40000), ==, 200);
27+
tt_want_int_op(pbio_math_sqrt(400000000), ==, 20000);
28+
29+
// Negative square roots do not exist but are expected to return 0.
30+
tt_want_int_op(pbio_math_sqrt(-36), ==, 0);
31+
32+
for (int32_t s = 0; s < INT32_MAX - 255; s += 256) {
33+
tt_want_int_op(pbio_math_sqrt(s), ==, sqrt(s));
34+
}
2835
}
2936

3037
static void test_atan2(void *env) {

0 commit comments

Comments
 (0)