Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Adafruit_AHRS_Madgwick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,17 @@ void Adafruit_Madgwick::updateIMU(float gx, float gy, float gz, float ax,
// Fast inverse square-root
// See: http://en.wikipedia.org/wiki/Fast_inverse_square_root

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;
float Adafruit_Madgwick::invSqrt(float x)
{
float halfx = 0.5f * x;
union {
float f;
uint32_t i;
} conv = { .f = 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 y;
}

//-------------------------------------------------------------------------------------------
Expand Down
20 changes: 11 additions & 9 deletions src/Adafruit_AHRS_Mahony.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,17 @@ void Adafruit_Mahony::updateIMU(float gx, float gy, float gz, float ax,
// Fast inverse square-root
// See: http://en.wikipedia.org/wiki/Fast_inverse_square_root

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;
float Adafruit_Mahony::invSqrt(float x)
{
float halfx = 0.5f * x;
union {
float f;
uint32_t i;
} conv = { .f = 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 y;
}

//-------------------------------------------------------------------------------------------
Expand Down