add functions for sin() and cos() with increased precision when the return value is supposed to be zero

Sun, 13 Jul 2025 13:35:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 13 Jul 2025 13:35:51 +0200
changeset 201
030fa72d516a
parent 200
cf0579d3bbc4
child 202
d6fd0eabe568

add functions for sin() and cos() with increased precision when the return value is supposed to be zero

src/ascension/datatypes.h file | annotate | diff | comparison | revisions
--- a/src/ascension/datatypes.h	Sat Jul 12 23:05:43 2025 +0200
+++ b/src/ascension/datatypes.h	Sun Jul 13 13:35:51 2025 +0200
@@ -214,6 +214,43 @@
 }
 
 /**
+ *
+ * @param x the floating point
+ * @return if @p x is near zero, returns zero, returns @p x otherwise
+ */
+static inline float asc_fround_zero(float x) {
+    if (fabsf(x) < FLT_EPSILON) {
+        return 0.f;
+    } else {
+        return x;
+    }
+}
+
+/**
+ * Returns the cosine of x.
+ *
+ * This function eliminates result values close to zero.
+ *
+ * @param x the angle in radians
+ * @return the cosine of x
+ */
+static inline float asc_cos(float x) {
+    return asc_fround_zero(cosf(x));
+}
+
+/**
+ * Returns the sine of x.
+ *
+ * This function eliminates result values close to zero.
+ *
+ * @param x the angle in radians
+ * @return the sine of x
+ */
+static inline float asc_sin(float x) {
+    return asc_fround_zero(sinf(x));
+}
+
+/**
  * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
  *
  * This operation is quite expensive. When you need to use it often, it's

mercurial