在 python 中更改除法精度 [已关闭]

Changing the precision of division in python [closed]

提问人:Pooyan Rezaeipour 提问时间:11/12/2023 最后编辑:Pooyan Rezaeipour 更新时间:11/12/2023 访问量:118

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

7天前关闭。

这篇文章在 7 天前被编辑并提交审核,未能重新打开帖子:

原始关闭原因未解决

我想在Python中更改除法的精度。

实际上,我正在Matlab中处理“col2im”指令,我想用Python编写它的等效形式。

例如,对于相同的输入,在 Matlab 中输出为

enter image description here

在 python 中我得到了

enter image description here

输出之间略有不同,但在特定应用中,获得完全相同的结果非常重要。

我认为问题是因为我使用的 Python 中的除法形式。我尝试了“ / , // , divide( , ) ”,但事实并非如此。

有没有其他方法可以在 python 中划分 2 个值?

我使用了以下源代码,并进行了轻微的修改,如 https://github.com/Mullahz/Python-programs-for-MATLAB-in-built-functions/blob/main/col2im.py 所示:

import numpy as np
def col2im(blk_mtx, img, blk_size):
    image_size = img.shape
    imgw = img.shape[0]
    imgh = img.shape[1]

    p, q = [blk_size, blk_size]

    result = np.zeros(image_size)
    weight = np.zeros(image_size)

    col = 0
    for i in range(1, imgw, blk_size):
        for j in range(1, imgh, blk_size):
            result[j-1:j + p-1, i-1:i + q-1] += blk_mtx[:,                                                        col].reshape((blk_size, blk_size), order='F')
            weight[j-1:j + p-1, i-1:i + q-1] += np.ones((blk_size, blk_size))
            col += 1

    final_res = np.divide(result, weight)
    return final_res
python numpy matlab 精度 除法

评论


答: 暂无答案