-
Notifications
You must be signed in to change notification settings - Fork 24
Open
Description
The VectorInt16 class overflows in the getMagnitude function and returns useless results. This potentially impacts a lot of calculations. The problem is multiplying an int16 by itself can grow too large. I've had to create my own GetMagnitude function to avoid using this one.
One potential fix is to cast to int32_t before multiplying.
REPRO:
#include "Simple_MPU6050.h"
void setup()
{
Serial.begin(115200);
VectorInt16 aaWorld = VectorInt16(-13000, 1500, 10);
float magnitude = aaWorld.getMagnitude();
Serial.printfloatx(F("x:"), aaWorld.x, 9, 4, F(", "));
Serial.printfloatx(F("y:"), aaWorld.y, 9, 4, F(", "));
Serial.printfloatx(F("z:"), aaWorld.z, 9, 4, F("\n"));
Serial.printfloatx(F("M:"), magnitude, 9, 4, F("\n"));
magnitude = GetMagnitude2(aaWorld.x, aaWorld.y, aaWorld.z);
Serial.printfloatx(F("M2:"), magnitude, 9, 4, F("\n"));
}
static float GetMagnitude2(int32_t x, int32_t y, int32_t z)
{
return sqrt(x*x + y*y + z*z);
}
void loop()
{
}
OUTPUT:
x: -13000.0000, y: 1500.0000, z: 10.0000
M: 67.3201
M2: 13086.2560
It's even worse with y=15000
x: -13000.0000, y: 15000.0000, z: 10.0000
M: NAN
M2: 19849.4360
Metadata
Metadata
Assignees
Labels
No labels