asin, acos, atan, Worng Values

I wrote a sketch the other day but something didnt work. The values that my sketch returned differed from what my calculator said.

I set to figure this out and ended up at the asin(), acos() and atan() terms that is use in my sketch.

I created the most minimal program anyone could think of to test this. Simple example of asin(0,5). The correct return value would be 0.52.

void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(asin(1/2)); } void loop() { // put your main code here, to run repeatedly:

}

It returns 0.

But if i write asin(0.5) instead of asin(1/2) the correct value is returned.

void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(asin(0.5)); } void loop() { // put your main code here, to run repeatedly:

}

It returns 0.52.

Another example atan(34/20) returns 0.79. Thats wrong. 34/2 equals 1.7. atan(1.7) returns 1.04. The correct value

Similar things happen with acos and atan.

Whats going on here?

Thanks Kevin

In Serial.println(asin(1/2)); both 1 and 2 are treated as integers and 1/2 is evaluated as an integer result which should be zero. You need to use Serial.println(asin(1.0/2)); or Serial.println(asin(1/2.0)); to get floating point division.

You are using integer arithmetic constants, you need to say 34.0/20.0 and 1.0/2.0

Understood and confirmed.

But what would I do if I where to use variables instead of numbers and many of them.

That´s the actual equation Im using in my sketch.

omega=pi-atan(yp/(a-xp))-acos((s2*s2+(a-xp)(a-xp)+yp*yp-l2*l2)/(2*s2*sqrt((a-xp)(a-xp)+yp*yp)));

All the variables in it are Byte´s execpt pi and omega which are floats. I did that to simplify things and to save memory.

Do I have to turn all the variables in the equation into floats in order to get a correct result?

Thanks Kevin

You don't have to turn them into floats, just cast them to floats, and the compiler will do the hard work for you.