python - 从 blender 导出 float 并读入 c 错误值

标签 python c

我正在使用 Python 从 Blender 写入浮点值,然后在 C 中读取。 对于某些值,我得到 0。 如果我用 2.3 之类的数字或其他随机数替换该值,它就会起作用。 我正在使用 struct.pack("<ffff")写他们。

这是不起作用的示例值:

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

当我在十六进制编辑器中打开文件时,我发现写着正确的数字 -8.88178E-16

如何读取正确的值?

这是 python 示例:

#!/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))

然后这里是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 输出是:

a ='-0.000000'
b ='123.032402'

最佳答案

OP 需要使用指数符号格式打印在 C 中读取的数字,以便清楚地看到有效数字。

// 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);

关于python - 从 blender 导出 float 并读入 c 错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790165/

相关文章:

c++ - C 编译器出错,但 C++ 编译器不出错

python - 带有 Twisted 的交互式 Python 客户端/服务器

javascript - 如何通过 Selenium 的 onclick 属性定位 javascript 元素?

python - open cv2 python中Tensor flow 2.0对象的使用方法是什么,为什么这么迂回?

c - 将数组作为参数从 C 传递给 x86 函数

python - 当需要执行多个线程时如何操作GIL?

python - 从 python 到 aplay

python - 从操作码和参数列表创建 Python 字节码?

c - 在 C 上使用 while 循环和 eof 时出错

c - C编程中的输入重定向?