Float Number Calculation

Hi, im trying to calculate something with float numbers but the second result is wrong. This is my code;

lat1_d=(float) lat1_d1*10+lat1_d2;
lat_1_radian=(float)(22/7)*(lat1_d/180);

Serial.println(lat1_d,5);
Serial.println(lat_1_radian,5);

And the output is;

40.00000
0.66667

It must be like 0.698.

If you have any idea about this i will be happy. Thank you

So use fewer digits here: Serial.println(lat_1_radian,5); like 3 Serial.println(lat_1_radian,3);

I mean the result of calculation is wrong it must be 0.698, not 0.667.

globieai: I mean the result of calculation is wrong it must be 0.698, not 0.667.

It's impossible to know whether that's the case unless you know what the input values are. I suggest you write a sketch which demonstrates the problem i.e. just hard-code the input values, do your calculation and print the result, tell us what it prints and what you think it should have printed.

Try adding more ( )s, just in case: lat1_d=(float) (lat1_d1*10)+lat1_d2;

The value of first result is 40, for the second result it uses only this 40 value and calculating, you can check it. I overcame this problem using code like this;

lat_1_radian=(double)(((22*lat1_d)/(180))/7)+((((((22*lat1_m)/180)/60)/7)));

I did not lose any decimal number and result is correct.

That's a bit different than what you started with.

I changed a little bir the calculation but i divided the numbers step by step. That was the key.

If you want accuracy, 22/7 is not equal to pi. Why are you wasting your time like this?

You can used this

lat1_d=(float) lat1_d1*10+lat1_d2;
lat_1_radian=(float)(22.0/7)*(lat1_d/180.0);

Serial.println(lat1_d,5);
Serial.println(lat_1_radian,5);

or this

lat1_d=(float) lat1_d1*10+lat1_d2;
lat_1_radian=(float)(3.14159265)*(lat1_d/180.0);

Serial.println(lat1_d,5);
Serial.println(lat_1_radian,5);