1

I am writing float values from Blender using python, then read in C. For some values I get 0. If I replace that values with numbers like 2.3 or other random numbers It works. I am using struct.pack("<ffff") to write them.

Here is example value which doesn't work:

-8.881784197001252e-16, -1.700029006457271e-15, 6.106226635438361e-16, 1.0

When I opened file in hex editor I found correct number written -8.88178E-16

How can I read correct value?

Here is python sample:

#!/usr/bin/python

import struct

f = open("test.f", "wb")

f.write(struct.pack("<f", -8.881784197001252e-16))
f.write(struct.pack("<f", 123.0324))

Then here is C:

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

int main(int argc, char *argv[])
{
    FILE *file = fopen("test.f", "rb");
    float test = 0;

    fread(&test, sizeof(float), 1, file);
    printf("a ='%f'\n", test);

    fread(&test, sizeof(float), 1, file);
    printf("b ='%f'\n", test);

    return 0;
}

C output that I get is:

a ='-0.000000'
b ='123.032402'
user3256672
  • 33
  • 1
  • 3
  • I don't see the problem here? Unless it's that Python is printing too many digits. – user253751 Mar 01 '15 at 04:43
  • Problem is that most of values read are correct but this last few are not, – user3256672 Mar 01 '15 at 04:45
  • if I write `struct.pack(" – user3256672 Mar 01 '15 at 04:46
  • 2
    How are you reading them in C? – user253751 Mar 01 '15 at 04:47
  • I am using `fread(rotations, sizeof(float), 4, file)`. Also tried one by one and same results. – user3256672 Mar 01 '15 at 04:56
  • 2
    Can you please post the simplest *complete* example of Python code and C code that demonstrates the behavior you're seeing? While you're at it, start writing all code this way - don't go into it writing the full-featured version. Write the most basic version you can to prove that you can do what you want with your data (aka export a few floats to your C program), then move on to the "real" version. – Jonathon Reinhart Mar 01 '15 at 04:57
  • Using `printf("a ='%e'\n", test);` will provide more information. As it stands now everything look OK: `-8.881784197001252e-16` was written and the same number was read back. The C code was instructed to print the number as a x.xxxxxx number of which '-0.000000' is the best representation. – chux - Reinstate Monica Mar 01 '15 at 05:11
  • @chux when I printed it with %e I got `-8.881784e-16`. Is this number correct and only printf not displaying it properly? – user3256672 Mar 01 '15 at 05:15
  • Yes see updated comment. Try `printf("a ='%.16e'\n", test);` to print with 16 places after the decimal point. BTW: The C code _did_ displaying it properly per the format used `"%f"`. – chux - Reinstate Monica Mar 01 '15 at 05:16
  • To see how many decimal places are needed see http://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value/19897395#19897395 – chux - Reinstate Monica Mar 01 '15 at 05:19
  • Thanks chux, that solved problem. Can you add answer or how can I close this question? – user3256672 Mar 01 '15 at 05:22

1 Answers1

0

OP needs to print the numbers read in C using an exponential notation format to clearly see the significant digits.

// printf("a ='%f'\n", test);
printf("a ='%e'\n", test);

// or to print with enough distinguishing significance for all float
#include <float.h>
printf("a ='%.*e'\n", FLT_DECIMAL_DIG - 1, test);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256