user2902: 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?

user2881: You need 2 typecasts. So 
atan(-ax/sqrt((float)ay*ay+(float)az*az));

Otherwise it will use int16_t for the right side of the addition and overflow.
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.

2902: <p>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:</p>

<p>My final goal is this:   </p>

<pre><code>theta =  atan(-ax/sqrt((float)ay*ay+az*az));
</code></pre>

<p>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 <code>a^2+b^2 = 388^2+10288^2 = 105993488</code> what I get from the arduino however is <code>152848</code>.
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?</p>
 2881: <p>You need 2 typecasts. So </p>

<pre><code>atan(-ax/sqrt((float)ay*ay+(float)az*az));
</code></pre>

<p>Otherwise it will use int16_t for the right side of the addition and overflow.</p>

<p>Though for speed you might want to typecast them as <code>long</code> (<code>int32_t</code>), as floats calculations on the arduino are really slow. The <code>sqrt</code> function will covert this double into a float for you.</p>
