Fix an issue on FP calculation diff of osx and linux
sugar on mac
result_type=1 expected=<1093> actual=<1047>
result_type=4 expected=<730> actual=<698>
result_type=5 expected=<177> actual=<173>
sugar on linux
result_type=1 expected=<1093> actual=<1047>
result_type=4 expected=<730> actual=<698>
result_type=5 expected=<177> actual=<173>
Change-Id: I4cd39becb0de4076dd37d870adda6463a96de424
diff --git a/native/jni/src/geometry_utils.h b/native/jni/src/geometry_utils.h
index f30e9fc..bad5eda 100644
--- a/native/jni/src/geometry_utils.h
+++ b/native/jni/src/geometry_utils.h
@@ -25,14 +25,17 @@
#define M_PI_F 3.14159265f
+#define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \
+ ? (floorf((f) * 10000.0f) / 10000.0f) : (f)
+
+#define SQUARE_FLOAT(x) ((x) * (x))
+
namespace latinime {
-static inline float squareFloat(float x) {
- return x * x;
-}
-
static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
- return squareFloat(x1 - x2) + squareFloat(y1 - y2);
+ const float deltaX = x1 - x2;
+ const float deltaY = y1 - y2;
+ return SQUARE_FLOAT(deltaX) + SQUARE_FLOAT(deltaY);
}
static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
@@ -52,9 +55,11 @@
}
static inline float getAngleDiff(float a1, float a2) {
- const float diff = fabsf(a1 - a2);
+ const float deltaA = fabsf(a1 - a2);
+ const float diff = ROUND_FLOAT_10000(deltaA);
if (diff > M_PI_F) {
- return 2.0f * M_PI_F - diff;
+ const float normalizedDiff = 2.0f * M_PI_F - diff;
+ return ROUND_FLOAT_10000(normalizedDiff);
}
return diff;
}
@@ -76,7 +81,7 @@
const float ray2y = y2 - y1;
const float dotProduct = ray1x * ray2x + ray1y * ray2y;
- const float lineLengthSqr = squareFloat(ray2x) + squareFloat(ray2y);
+ const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
const float projectionLengthSqr = dotProduct / lineLengthSqr;
float projectionX;