commit e0b1b8b3cfc62bedfa09e9143bf1292ad0f6bcf7
parent 619f8395744f7a84f90ed9e68f3f3cd0bb812a84
Author: vaplv <vaplv@free.fr>
Date: Fri, 3 Apr 2015 11:12:42 +0200
Add and test the sin2cos and cos2sin functions
Diffstat:
2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/src/math.h b/src/math.h
@@ -84,4 +84,16 @@ eq_epsf(const float a, const float b, const float eps)
return absf(a - b) <= eps;
}
+static FINLINE double
+sin2cos(const double d)
+{
+ return sqrt(MMAX(0.0, 1.0 - d*d));
+}
+
+static FINLINE double
+cos2sin(const double d)
+{
+ return sin2cos(d);
+}
+
#endif /* MATH_H */
diff --git a/src/test_math.c b/src/test_math.c
@@ -19,6 +19,7 @@ int
main(int argc, char** argv)
{
float f, g;
+ int i;
(void)argc, (void)argv;
f = -3.14159f; CHECK(absf(f),-f);
@@ -86,6 +87,17 @@ main(int argc, char** argv)
CHECK(eq_eps(MRAD2DEG(PI*0.75), 135.0, 1.e-8), 1);
CHECK(eq_eps(MRAD2DEG(MDEG2RAD(70.0)), 70.0, 1.e-8), 1);
+ FOR_EACH(i, 0, 16) {
+ double d, c, s, tmp;
+ d = ((float)rand() / (float)RAND_MAX) * PI;
+ c = cos(d);
+ s = sin(d);
+ tmp = cos2sin(c);
+ CHECK(eq_eps(s, tmp, 1.0e-8) || eq_eps(s, -tmp, 1.0e-8), 1);
+ tmp = sin2cos(s);
+ CHECK(eq_eps(c, tmp, 1.0e-8) || eq_eps(c, -tmp, 1.0e-8), 1);
+ }
+
CHECK(signf(-3.14159f), -1.f);
CHECK(signf(1.23f), 1.f);
CHECK(signf(0.f), 1.f);