Atan( a bit of math ) , why IS NOT WORKING=!

Hello

Help me i'm stupid!

This works ( 2 * atan(0.4444444) ) * 57.2958

This does not work ( 2 * atan( (120/2) / 135 ) ) * 57.2958

And even if i move this (120/2) / 135 into a variable and use that, it does not work.

But if i solve it as 0.44444 and just put in there, then it works. Makes no sense for me!

And by not working, i mean i get zero as output. While it should be 47.92degrees.

Try

( 2 * atan( (120.0/2) / 135 ) ) * 57.2958

And if you want to know why the proposed version works, it’s because adding the .0 forces the maths to be done using floating point arithmetics. The way you wrote it was using integral math 120/2 was 60, that was fine but then 60/135 was just 0

Thanks a lot, and it does work.

But I tested it with float variable, so i'm still a bit confused but at least moving. Thanks for both of you!

Do i need to force math into floatin points even if i declare them as floats?

Like if i did that

float anglE = (120/2) / 135;

So i still need to force it to calculate it in floating points?

Using integer math, which is the default, 60/135 = 0

60.0/135.0 = 0.444...

It does not matter what type you declare for the left hand side variable.

Great stuff, thanks. Something learned.

I used and INT for the 120, by float(int). That seemed to work. So all good.

int var=120;
Serial.print( ( 2 * atan( (float(var)/2) / 135 ) ) * 57.2958 );

Outputting factual data :slight_smile:

This works too:
Serial.print( ( 2 * atan( (var/2.) / 135 ) ) * 57.2958 );

Goddam you, i was so fancy.

try

float var = 120;

There are many paths to enlightenment, but the end result is said to be the same: eternal bliss.

The point is that you want to force the math inside the atan() call to be conducted using floating point arithmetics. For that you need to understand the evaluation order and how the compiler will then promote to float the rest of the maths.

See Order of evaluation - cppreference.com