小编dev*_*van的帖子

为什么round(x)和round(np.float64(x))之间有区别?

据我了解,2.675和numpy.float64(2.675)都是相同的数字。但是,round(2.675,2)得到2.67,而round(np.float64(2.675),2)得到2.68。为什么会这样?

import numpy as np
from decimal import Decimal

x = 2.675
np_x = np.float64(x)
type(x) # float
Decimal(x)    # Decimal('2.67499999999999982236431605997495353221893310546875')
Decimal(np_x) # Decimal('2.67499999999999982236431605997495353221893310546875')
x == np_x # True

# This is the bit that bothers me
round(x, 2) # 2.67
round(np_x, 2) # 2.68

# Using numpy's round gives 2.68 for both the numpy float as well as the Python built-in float...
np.round(x, 2) # 2.68
np.round(np_x, 2) # 2.68

# ... but this is because it might be …
Run Code Online (Sandbox Code Playgroud)

python numpy

8
推荐指数
1
解决办法
122
查看次数

标签 统计

numpy ×1

python ×1