We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4e63612 commit ceabdd3Copy full SHA for ceabdd3
lib/pbio/src/math.c
@@ -116,6 +116,14 @@ int32_t pbio_math_clamp(int32_t value, int32_t abs_max) {
116
return pbio_math_bind(value, -abs_max, abs_max);
117
}
118
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
+ */
127
int32_t pbio_math_sqrt(int32_t n) {
128
if (n <= 0) {
129
return 0;
lib/pbio/test/src/math.c
@@ -19,12 +19,19 @@ static void test_clamp(void *env) {
19
20
21
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);
+ tt_want_int_op(pbio_math_sqrt(0), ==, 0);
+ tt_want_int_op(pbio_math_sqrt(1), ==, 1);
+ tt_want_int_op(pbio_math_sqrt(4), ==, 2);
+ tt_want_int_op(pbio_math_sqrt(400), ==, 20);
+ tt_want_int_op(pbio_math_sqrt(40000), ==, 200);
+ 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
+ }
35
36
37
static void test_atan2(void *env) {
0 commit comments