float precision problems

I am trying to get accurate precision with sin and cosine.

eg my angle is:
50.401687 degrees (generated by GPS chip)

If I set this up as a float it seems to truncate it to 50.40 (when I do Serial.print(x):wink:
Is there any way of keeping numbers accurate to 6 decimal places on the Arduino UNO?

I want to perform sin and cosine (and acos atan2 etc) with as much accuracy as possible.
Grateful for any advice.

Andrew

Try Serial.print(x, 6);

Can't say for sure it will maintain 6 places.

You could maybe try fixed point if it is only +/- 180 degrees, but handling could be fiddly

If I set this up as a float it seems to truncate it to 50.40 (when I do Serial.print(x)
Is there any way of keeping numbers accurate to 6 decimal places on the Arduino UNO?

External (printed) accuracy and internal accuracy are not the same thing. Pay attention to the Serial.print() method overloads - specifically the one for floats. You CAN change the number of digits printed after the decimal point.

You can NOT change the stored accuracy, though, so don't bother trying to print more than 6 decimal places and expect them to mean anything.

thanks both - grateful for advice

And what is the precision of your GPS measurement?

If you think it's good to 1/100,000 of a degree, I've got a bridge I'd like to sell you...

-j

andrewashe:
I am trying to get accurate precision with sin and cosine.

eg my angle is:
50.401687 degrees (generated by GPS chip)
Is there any way of keeping numbers accurate to 6 decimal places on the Arduino UNO?

If you mean 8 decimal places in total ( like your sample value) :No, you'll lose some precision.

double is a float32 in reality, with 23 bit mantissa: see this Float Converter to play around.