Skip to content

Commit 28a74ab

Browse files
authored
Merge pull request #12 from markub3327/master
Avoid warnings about `type-punned`
2 parents 91e68c8 + a2cd7bd commit 28a74ab

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/SensorFusion.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ SF::SF()
5757
float SF::invSqrt(float x)
5858
{
5959
float halfx = 0.5f * x;
60-
float y = x;
61-
long i = *(long*)&y;
62-
i = 0x5f3759df - (i>>1);
63-
y = *(float*)&i;
64-
y = y * (1.5f - (halfx * y * y));
65-
y = y * (1.5f - (halfx * y * y));
60+
union {
61+
float f;
62+
uint32_t i;
63+
} conv = { .f = x };
64+
conv.i = 0x5f3759df - (conv.i >> 1);
65+
conv.f *= 1.5f - (halfx * conv.f * conv.f);
66+
conv.f *= 1.5f - (halfx * conv.f * conv.f);
6667
return y;
6768
}
6869

0 commit comments

Comments
 (0)