据我了解,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)