# Getting no result of a equition

I have writteng the following test sketch.

``````#include "Arduino.h"
#include <Streaming.h>
// Used in the xyConvert routine
long _XX;
long _YY;
long _newX_axis;
long _newY_axis;
static const double DegreeToRadian = PI/180 ;  // PI/180
static const double RadianToDegree = 1/DegreeToRadian;    // 1 / ( PI / 180 )

void setup() {
// put your setup code here, to run once:
// initialize serial ports and LCD
Serial.begin(9600);                 // Communication with the world
while (!Serial) ;                          // while the serial stream is not open, do nothing:
_xyConvert(9800,3400);
}

void loop() {
// put your main code here, to run repeatedly:

}
void _xyConvert(float az, float el)  {

double azFloat = az / 100*DegreeToRadian;
if(el < 0.1) {
el = 0.1;
}
double elFloat = el / 100*DegreeToRadian;
Serial << "Azimuth before " << az / 100 << _BYTE(176) << endl;
Serial << "Elevation before " << el / 100 << _BYTE(176) << endl;

_XX =  (asin(sin(-azFloat) * cos(-elFloat) )*RadianToDegree) + 90.0;
_YY = (atan(cos(-azFloat) / tan(-elFloat) )*RadianToDegree) + 90.0;

_newX_axis = _XX;
_newY_axis = _YY;
Serial << "Conversion (David) from  " << az/100 << _BYTE(176) << "/" << el/100 << _BYTE(176);
Serial << " to " << _XX << _BYTE(176) << "/" << _YY << _BYTE(176) << endl;
Serial << "X axis after " << _newX_axis / 100 << _BYTE(176) << endl;
Serial << "Y axis after " << _newY_axis / 100 << _BYTE(176) << endl;
}
``````

The strange thing is that i get this result:

``````Azimuth before 98.00°
Elevation before 34.00°
Conversion (David) from  98.00°/34.00° to 34°/101°
X axis after 0°
Y axis after 1°
``````

Actuely the last two lines should have the same result as the one line above them.
Anny sugesstions

What is the output you are expecting, it doesn't look to me like that output is unexpected.

X axis after 34° Y axis after 101°

Mixing int and float arithmetic is a bad idea. Change all your int constants to float constants (100 -> 100.0) and see if that affects your results.

XX and YY are integers (please don't use leading underscores!) if you divide integer 34 by 100, what do you get? If you divide 101 by 100 what do you get?

PaulS: Mixing int and float arithmetic is a bad idea. Change all your int constants to float constants (100 -> 100.0) and see if that affects your results.

OK GOT IT. I have to multiply the result by 100.

``````  _newX_axis = _XX * 100;
_newY_axis = _YY * 100;
``````

But how do i round it, i.o.w. get a normal number

But how do i round it, i.o.w. get a normal number

What does "a normal number" mean?

gharryh:

PaulS: Mixing int and float arithmetic is a bad idea. Change all your int constants to float constants (100 -> 100.0) and see if that affects your results.

OK GOT IT. I have to multiply the result by 100.

``````  _newX_axis = _XX * 100;
_newY_axis = _YY * 100;
``````

But how do i round it, i.o.w. get a normal number

Actually, what he said is to use the data type float for all your numeric values. For constant numbers (i.e. numbers written in a formula, could also be called literal values) write a decimal point and a trailing zero to convince the compiler that you want a floating point number instead of an integer. Then all the arithmetic would be floating point. Slow, but accurate.

But, what you seem to have interpreted might also work as well. Especially since floating point math is time consuming on most general purpose microprocessors (like what is inside these microcontrollers). Move the decimal point to the right 1 more times than the precision you need (multiply by some power of 10). Do all your math as integers (or maybe longs, I haven't looked at your numbers and counted decimal points to make sure the values would still fit in an integer). Then for output divide by the same power of 10 into a floating point variable and output the precision you need. Even if the output method just strips the right-most digit, all the calculations you already did had it there and should reduce rounding errors. This is essentially doing fixed point arithmetic.

Also, the request to not use the leading underscore for your variable names is valid. While it works, a leading underscore is used only in specific applications by the generally used programming styles. (I think for local variables inside classes to help differentiate them from regular variables, but I could be wrong. Someone with more experience should probably give the exact reason if for nothing else to educate me.) Just like putting all your code on one line will work, it flies in the face of easy (for humans) to understand programming styles.

The main reason not to use leading underscores is that the implementation uses those. From Harbison and Steele:
“Standard C further reserves all indentifiers beginning with an underscore and followed by either an uppercase letter or another underscore; programmers should avoid using such identifiers. C implentations sometimes use these identifiers for extensions to stnadard C or other internal purposes.”