Skip to content

json_printf_array with "%f" does not work #52

@jkent

Description

@jkent

Example code:

void test_case(float arr[], size_t len)
{
    for (int i = 0; i < len; i++) {
        printf("%f\n", arr[i]);
    }

    char *json = json_asprintf("{state:{reported:{temps:%M}}}",
                               json_printf_array, arr, len * sizeof(arr[0]),
                               sizeof(arr[0]), "%f");
    if (json) {
        printf("%s\n", json);
        free(json);
    }
}

int main(int argc, char *argv[])
{
    float arr[3] = {
        1.1,
        2.2,
        3.3
    }; 

    test_case(arr, 3);

    return 0;
}

The printf of the loop is correct. The correct number of elements are added to the json, but they are all zero. This is being compiled for the esp32 with xtensa-esp32-elf-gcc (crosstool-NG crosstool-ng-1.22.0-80-g6c4433a) 5.2.0

I've copied the elements to an array of double using "%lf" and this is a workaround for the problem.

Edit: Added a main and tested on amd64 as well, and repro'd there too. I was worried this might be due to the esp32 hardware float lazy context switch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions