Strange results using and returning doubles [SOLVED]

I’m experiencing some strange results when using doubles…

I’ve got a global double declared:

double x;

Which I clear in setup():

x = 0;

I’ve got the following, which executes (sucessfully) once a second:

void make_x()
{
    int a = 3642;   
    x = (double)a;
}

and the following so that I can retrieve x, from within setup():

Spark.variable("getx", &x, DOUBLE);

Yet when I call ‘getx’, I get the following result:

{
    "cmd": "VarReturn",
    "name": "getx",
    "result": 3.7777603e-317,
    "coreInfo": {
        "last_app": "",
        "last_heard": "2014-02-13T23:33:48.598Z",
        "connected": true,
        "deviceID": "xxxxxxxxxxxxxxxxxxxx"
    }
}

And the value isn’t as I was expecting, unless my maths is seriously wrong somewhere!

How can 3642 be the same as 3.7777603 x 10^-317 ??

I’ve checked that I’m not setting the variable elsewhere.

Can anyone help? Thanks.

Hi @binaryfrost; we’ve got this on our backlog to fix; we think it’s a problem with endianness. I believe that currently every variable type except double should be working, so perhaps you can use another type?

I’ve got a fix for this and it’ll be included in our next production push (hopefully tomorrow), this one has been bugging me!

Thanks,
David

A work-around until this is fixed might be to use strings instead. This came up before in another thread and this code worked fine.

#include <math.h>

double result = 0;
char resultstr[64];

void setup()
{
  Spark.variable("result", &resultstr, STRING);
}

void loop()
{
  result = log10(10000);
  sprintf(resultstr, "%f",result);
  delay(5000);
}

Glad it’s not me going mad. Many thanks guys!

In the interim I’ll try the String work around.

Hey @binaryfrost , Thanks for sharing. @Dave just fixed this issue yesterday, but it’s not deployed to production. Will post here when doubles are working expected.

Hah! @Dave just deployed this to production and verified that it works. Strange double variable behavior should be a thing of the past–if anyone experiences something otherwise, please post here.

2 Likes

@jgoggins awesome - many thanks.

Thinking about my previous post…

…which bug was this logged against? I cant find a closed bug corresponding to this on the github channel…

Thanks :slight_smile:

No prob @binaryfrost. It was logged against our internal project management tool, not logged against anything public. As @Dave mentioned in that post, let’s try logging known bugs via GitHub issues that arise on this community site and see if that helps folks know when things are fixed/etc.

Some bugs won’t make sense on Github at the moment, e.g. stuff with the build IDE, etc. So posting those on the forums might make more sense.