Home:ALL Converter>Is there a way for Python to read __float128 from a binary file?

Is there a way for Python to read __float128 from a binary file?

Ask Time:2019-07-27T01:48:35         Author:Newton Tang

Json Formatter

I am trying to read a binary file that stores some __float128 variables from other C project using Python.

I've been looking in the numpy.float128 but it seems that it is implemented in a different way to the __float128 in C.

Using the code below to read the same binary file gives me different number.

Python

import sys
import numpy as np

inFile = open(sys.argv[1], 'rb')
a1 = np.fromfile(inFile, dtype=np.float128)
print(a1)
inFile.close()

C

#include <quadmath.h>
#include <stdio.h>

int main(int argc, char *argv[]){
    FILE *infile = fopen(argv[1], "r");
    __float128 in_data;
    fread(&in_data, sizeof(__float128), 1, infile);
    printf("%g", in_data);
    printf("\n");
    return 0;
}

Author:Newton Tang,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57224872/is-there-a-way-for-python-to-read-float128-from-a-binary-file
yy