MadgwickAHRS
MadgwickAHRS copied to clipboard
`strict-aliasing` warnings
In the desktop IDE I get a couple of "warnings" but in the ONLINE editor CREATE I get a compilation failure with the following output.
/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp: In static member function ‘static float Madgwick::invSqrt(float)’:
/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp:234:20: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
long i = *(long*)&y;
^
/home/admin/builder/arduino-builder/latest/Madgwick-1.2.0/src/MadgwickAHRS.cpp:236:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
y = *(float*)&i;
^
C++ doesn't like Type Punning. You would have to deactive checking for strict-aliasing in your compiler, which I wouldn't recommend. Simply replace fastInvSquare() by math.h 1/sqrt(). The speedloss is insignificant.
@facchinm can you explain how to rectify this error(approach)
Simply replace fastInvSquare() by math.h 1/sqrt()
I am with thekunalsaini as I get the same error and don't understand the fix. Do I have to search and replace fastInvSquare() by math.h 1/sqrt()? In what file? Sorry if its a stupid question but I just don't understand it.
In what file?
https://github.com/arduino-libraries/MadgwickAHRS/blob/0a909d25d0878f0c24525aeb4553cdd90473f50b/src/MadgwickAHRS.cpp#L231
Sorry but if I replace float Madgwick::invSqrt(float x) {
with float math.h 1/sqrt(float x) {
it (understandably) throws an error.
math.h is the header containing sqrt()
you can replace
float 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; }
by
float Madgwick::invSqrt(float x) { return 1/sqrt(x); }