Oddities with Particle.publish() and float

can someone explain why the String.toFloat() method is behaving oddly.

The string variable is always properly set, but If I have the number variable defined as float, it never gets set in the function, but if it is defined as double it does get set.

Also, I am getting none of the approximation (as described in the documentation) if I pass in 123.123 the double value becomes 123.1220004783746 something. Unless the decimal portion is a proper decimal (1/(power of 2))

double foo  = 100.0;
//float foo - 100.0;
String fooStr = "UNDEF";

void setup(void) {

   Particle.variable("FooStr", fooStr);
   Particle.variable("Foo", foo);
   Particle.function( "AdjMyValue", AdjustMyValue );

void loop(void)

int AdjustMyValue(String command)
    fooStr = command;
    foo = fooStr.toFloat();

Not answering your question, but you should return an int since your function is declared as int AdjustMyValue(String command);

The trouble with floating point types is that you can’t represent every real number due to the way how they are stored.
Although float and double are a bit more complex than this, the basic idea is that binary numeric types only store powers of 2.
e.g. 12.25 = … + 02^4 + 12^3 + 12^2 + 02^1 + 02^0 + 02^-1 + 12^-2 + 02^-3 + …
but not all possible real numbers can be represented like this with a finite number of bits.

I’ve tried with floatand the conversion works as expected (try with Serial.print(foo, 5)). It’s only Particle.variable() or the cloud side of the system seems to have problems with interpreting float as double.
And I’ve already opened an issue about this

1 Like

@ScruffR Thanks for the “not answer, answer” I’ll get that part corrected!

And thanks for opening a ticket on this. Your ticket pretty much sums up what I was experiencing.

If need be, I can change the thread title to something more appropriate.


1 Like