# square root of large number

I've got 2 int16_t numbers, say 388 and 10288, which are sensor readings from an accelerometer. I want to estimate the angle of the device on that, but seemingly there are some type overflow issues:

My final goal is this:

``````theta =  atan(-ax/sqrt((float)ay*ay+az*az));
``````

The problem occurs in the bracket: ay^2 and az^2 are the 2 power of the given numbers. I recalculated with matlab and for the above example I get `a^2+b^2 = 388^2+10288^2 = 105993488` what I get from the arduino however is `152848`. To be short: I don't know much about fixed point math or thelike and I couldn't find a sufficient answer to this: How do I calculate the square root term?

``````atan(-ax/sqrt((float)ay*ay+(float)az*az));
Though for speed you might want to typecast them as `long` (`int32_t`), as floats calculations on the arduino are really slow. The `sqrt` function will covert this double into a float for you.
• `sqrt` is part of math.h, and it accept a double. A float, in arduino and most cases, is a double. Why it automatically typecasts, I don't know. I would rather expect a compile error, but I don't know C. PS in my question I said `double`, but meant to say `long` (fixed in edit). – Gerben Aug 2 '14 at 19:40