diff --git a/library.properties b/library.properties index b19ae7c..e008edd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit AHRS -version=2.3.3 +version=2.3.5 author=Adafruit maintainer=Adafruit sentence=AHRS (Altitude and Heading Reference System) for various Adafruit motion sensors diff --git a/src/Adafruit_AHRS_Madgwick.cpp b/src/Adafruit_AHRS_Madgwick.cpp index d8234ff..6b7daf9 100644 --- a/src/Adafruit_AHRS_Madgwick.cpp +++ b/src/Adafruit_AHRS_Madgwick.cpp @@ -282,13 +282,14 @@ void Adafruit_Madgwick::updateIMU(float gx, float gy, float gz, float ax, float Adafruit_Madgwick::invSqrt(float x) { float halfx = 0.5f * x; - float y = x; - long i = *(long *)&y; - i = 0x5f3759df - (i >> 1); - y = *(float *)&i; - y = y * (1.5f - (halfx * y * y)); - y = y * (1.5f - (halfx * y * y)); - return y; + union { + float f; + long i; + } conv = {x}; + conv.i = 0x5f3759df - (conv.i >> 1); + conv.f *= 1.5f - (halfx * conv.f * conv.f); + conv.f *= 1.5f - (halfx * conv.f * conv.f); + return conv.f; } //------------------------------------------------------------------------------------------- diff --git a/src/Adafruit_AHRS_Mahony.cpp b/src/Adafruit_AHRS_Mahony.cpp index c01b510..bb1dc0a 100644 --- a/src/Adafruit_AHRS_Mahony.cpp +++ b/src/Adafruit_AHRS_Mahony.cpp @@ -261,13 +261,14 @@ void Adafruit_Mahony::updateIMU(float gx, float gy, float gz, float ax, float Adafruit_Mahony::invSqrt(float x) { float halfx = 0.5f * x; - float y = x; - long i = *(long *)&y; - i = 0x5f3759df - (i >> 1); - y = *(float *)&i; - y = y * (1.5f - (halfx * y * y)); - y = y * (1.5f - (halfx * y * y)); - return y; + union { + float f; + long i; + } conv = {x}; + conv.i = 0x5f3759df - (conv.i >> 1); + conv.f *= 1.5f - (halfx * conv.f * conv.f); + conv.f *= 1.5f - (halfx * conv.f * conv.f); + return conv.f; } //-------------------------------------------------------------------------------------------