python调用dll出现精度问题解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python调用dll出现精度问题解决

嘿,不许笑   2023-03-20 我要评论

问题:python 在调用dll 的时候出现了精度问题

总结:使用decimal库进行转换就可以正常传递。

遇到的问题具体情况

dll 生成函数代码声明如下

extern __declspec(dllexport) void LinearCompute(GoFloat64 currentX, GoFloat64 currentY, GoFloat64 targetX, GoFloat64 targetY, GoFloat64* resultX, GoFloat64* resultY);

使用python调用代码

from ctypes import *

# c_double 声明c 双精度小数变量
result_x = c_double(0)
result_y = c_double(0)

x_c = 1400.
y_c = 1450.
x_t = 1500.
y_t = 5600.

# byref 调用指针
dll.LinearCompute(c_double(x_c), c_double(y_c), c_double(x_t), c_double(y_t), byref(result_x), byref(result_y))

print(result_x.value, '  ', result_y.value)

但是输出的内容显示,输入到函数中的 x_c ,y_c 等数据对不上。

这里需要使用 decimal 库进行精度方面转换

所以以上代码改为如下:

from ctypes import *
from decimal import *

result_x = c_double(0)
result_y = c_double(0)

x_c = Decimal(1400)
y_c = Decimal(1450)

x_t = Decimal(1500)
y_t = Decimal(5600)

dll.LinearCompute(c_double(x_c), c_double(y_c), c_double(x_t), c_double(y_t), byref(result_x), byref(result_y))

print(result_x.value, '  ', result_y.value)

然后运行的结果如下

可以看见 传入的数值变得正常了。

附:https:

Decimal类型的优点

Decimal类型是在浮点类型的基础上设计的,但是它在几个地方上要优于floating point:

  • Decimal类型可以非常精确地在计算机中存储,而学过c++的都知道,浮点型在计算机中是无法精确存储的,比如1.1和2.2在计算机中存储后,运算(1.1+2.2)表达式的值结果会是3.3000000000000003;Decimal类型则不会出现这种情况。同样,由于无法精确存储,浮点型也就无法精确计算(相对于Decimal类型),可以再测试(0.1+0.1+0.1-0.3)两种类型的计算结果。
  • Decimal类型会自动保留小数点后面不需要的0,以与输入的精度相匹配,比如下面小程序中的例子:浮点型的1.20+1.30结果是2.5;而Decimal类型结果是2.50,这样貌似比较人性化。
  • Decimal类型可以根据需要自己设置小数点后精度。通过getcontext().prec = x (x为你想要的精度来设置,getcontext()函数下面再详细介绍)。
  • Decimal类型有很强的管理功能,它能够根据需要设置,来控制输出的格式,得到或者忽略某类错误(如除0,可以设置忽略它,而得到一个Infinity的Decimal值)。

比较重要的一点,如果使用 decimal 转换小数时,需要使用 单引号 引起来。

from decimal import *

print(Decimal(1.1) + Decimal(3.3))
print(Decimal(1.1) - Decimal(3.3))
print(Decimal(1.1) * Decimal(3.3))
print(Decimal(1.1) / Decimal(3.3))

#输出结果
'''
4.399999999999999911182158030
-2.199999999999999733546474090
3.630000000000000097699626167
0.3333333333333333781908292778
'''

但是如果使用字符串,就可以得到正常的结果了。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们