toFloat strangeness


Having problems with numbers with Arduino...

void setup() {

Serial.begin(115200); delay(100);


void loop() { Serial.println("toFloat ?!??!");

String ff="123456789"; Serial.println(ff); float ffs=ff.toFloat(); Serial.println(ffs);

Serial.println("atof ?!??!");

char strVal[10] = "123456789"; Serial.println(strVal); float fVal = atof(strVal); Serial.println(fVal);



Outputs :-

toFloat ?!| 123456789 123456792.00 atof ?!| 123456789 123456792.00

Not terribly useful?

Any ideas?

You have too many significant digits for a float. You only get about 6-7 mantissa digits.

Ok, thanks for the reply...

What variable type can I use then? double says its the same



What variable type can I use then?

Use long. 123456789 is not a floating point value.

But I need to be using


hence working on floats....

Since we don't know what you want to do, we can't answer that.

Can you use fixed point? just assume the decimal point and work with integers.

I need it to calculate distance between 2 points on the Earth using Lat and Long.....

Hence a good precision is needed and a decimal point...

I cant believe it theres no variable that works with decimals properly ! :(

PhilCol: But I need to be using


"float" data type ha 6-7 "significant digits, that is the count of digits from left to right beginning with hte first non-zero digit.

So in float: 123456789.123 123456123.456 123456543.210 are fairly the same values within the accuracy of internal "float" number representation as they are the same in the first 6 digits and the same in decadic logarithm.

If you need high accuracy, DO NOT use float'! The 'long' data type has an accuracy of 9 to 10 significant digits which is much more than the 6 zo 7 significant digits of float.

Let's say your program needs compass degrees in the range 0 to 360 degrees at high accuracy, DO NOT USE float, use unsigned long of perhaps millionth of degrees.

Use 1000000 as "1 degree" (= 1000000 million units of a millionth degrees) to store the number.

Use 360000000 as 360 degrees.

The only thing you will need (is a self-written function for doing formatted output, i.e. if you want to show up 12345678 as "12.235678 degrees" in an LCD display for example

Using 'föoat' is only a good idea if you want 6-7 significant digits ATMOST and are happy with little accuracy!

Thanks, will give it a try :)

jurs: Using 'föoat' is only a good idea if you want 6-7 significant digits ATMOST and are happy with little accuracy!

Hardly. Float is also needed if you are doing a lot of calculation, spanning a wide dynamic range. Try doing a calculation using your millionths of a degree, when you need to perform multiplication or division with other large numbers....

Very few applications require 10 digits of precision, especially when working with real-world quantities, most of which are fundamentally un-knowable to that kind of precision. Understanding HOW to do calculations without excessive loss of precision is at least, if not more, important than what type of variables you use. There is no single magic bullet, as every problem has a different solution.

Regards, Ray L.

The issue was not "working with decimals properly." The issue is that there are tradeoffs. While you only get 1 part in a million or so precision, you can work any where within a 1E37 to 1E-37 to -1E-37 to -1E37, which is a pretty good range.