0

I'm trying to produce a code in Python and I feed as an input a variable of a C program. How could I do this?

If the two codes where in Python I could use "pickle'' to store the information of the first one and then read it in the second one. But for what I've been reading there is not a pickle-like way to do it in C (please correct me if I am wrong, because I know almost nothing of C) and even if there exists such a way I'm unsure of the right way to "import'' it to Python.

The data I need to save is an "array of doubles'', that actually is a dynamic allocation of the memory.

BTW: What I'm doing now is get the print of the output, save it to a file and read it in python, but I'm losing decimal precision due the rounding when printing.

esv
  • 41
  • 1
  • Can't you just [print more precise data](https://stackoverflow.com/questions/26441781/loss-of-precision-float-in-python) from python? – Federico klez Culloca Dec 03 '19 at 11:59
  • 1
    Right now it is a bit difficult to tell what exactly your problem is. Unless your C implementation is *really* poor, it should print `double` values at the full precision necessary to uniquely identify the binary representation of said `double`, i.e. a round-turn (reading that `double` back in) should not lose any precision. Could you come up with a small example, observed result, and expected result? Because I have a hunch it's your expectation that is faulty at some point... – DevSolar Dec 03 '19 at 12:00
  • @FedericoklezCulloca: He's *printing* from C, not Python. But yes, that question / answer touches on the same issue -- "false precision", when you think that a longer decimal is "better" when, in the end, the rounded one is just as uniquely identifying the same bit pattern. – DevSolar Dec 03 '19 at 12:02
  • Wouldn't it be easiest just to dump double array as is into binary file with `fwrite` in C, and then decode it in Python with `struct.unpack`? – user694733 Dec 03 '19 at 12:06
  • @DevSolar Right, I got confused. I read "as an input a variable of a C program" as "as an input a variable *to* a C program". – Federico klez Culloca Dec 03 '19 at 12:28

1 Answers1

1

You can serialize the data in python using the struct.pack function, in C you can then unpack this information again using the union data type. I will give short codes in both languages on how to do both parts. The reverse is also possible and should be easy for you to do yourself should you require it. Remember that when you're sending doubles instead of floats you need to read 8 bytes in the C code each time.

In Python:

import struct

doublesArray = [5.0, 1.2, 3.4, 8.6]

file = open("transferdata", "wb")
for i in range(len(doublesArray)):
    file.write(struct.pack('f', doublesArray[i]))

In C. By the way I took this code from this post:

#include <stdio.h>
#include <stdlib.h>

union
{
    char asBytes[4];
    float asFloat;
} converter;

int main(void)
{
    FILE *fileptr;
    char *buffer;
    long filelen;

    fileptr = fopen("transferdata", "rb");
    fseek(fileptr, 0, SEEK_END);
    filelen = ftell(fileptr);
    rewind(fileptr);

    buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
    fread(buffer, filelen, 1, fileptr); // Read in the entire file
    fclose(fileptr);

    int i = 0;
    while(i < filelen)
    {
        converter.asBytes[0] = buffer[i];
        converter.asBytes[1] = buffer[i + 1];
        converter.asBytes[2] = buffer[i + 2];
        converter.asBytes[3] = buffer[i + 3];
        i += 4;

        char msg[32];
        snprintf(msg, 32, "%f ", converter.asFloat);
        printf(msg);
    }

    return 1;
}

[EDIT]: I see I wrote the exact opposite of what you need. I did not do that on purpose. However, I think you have enough information to figure out how to do the reverse on your own now.

Anteino
  • 1,044
  • 7
  • 28