本文簡要介紹 python 語言中  numpy.around  的用法。
- 用法:- numpy.around(a, decimals=0, out=None)
- 均勻四舍五入到給定的小數位數。 - a: array_like
- 輸入數據。 
- decimals: 整數,可選
- 要四舍五入的小數位數(默認值:0)。如果小數為負數,它指定小數點左側的位數。 
- out: ndarray,可選
- 用於放置結果的替代輸出數組。它必須具有與預期輸出相同的形狀,但如果需要,輸出值的類型將被強製轉換。有關更多詳細信息,請參閱輸出類型確定。 
 
- rounded_array: ndarray
- 與 a 類型相同的數組,包含四舍五入的值。除非指定了 out,否則將創建一個新數組。返回對結果的引用。 - 複數的實部和虛部分別四舍五入。將浮點數舍入的結果是浮點數。 
 
 - 參數:- 返回:- 注意:- 對於恰好介於四舍五入的十進製值之間的值,NumPy 會四舍五入到最接近的偶數值。因此 1.5 和 2.5 舍入為 2.0,-0.5 和 0.5 舍入為 0.0,等等。 - np.around使用快速但有時不精確的算法來舍入浮點數據類型。對於正小數點它相當於- np.true_divide(np.rint(a * 10**decimals), 10**decimals), 由於 IEEE 浮點標準中小數的不精確表示而有錯誤[1]以及按 10 次方縮放時引入的錯誤。例如,請注意以下額外的“1”:- >>> np.round(56294995342131.5, 3) 56294995342131.51- 如果您的目標是打印具有固定小數位數的此類值,則最好使用 numpy 的浮點打印例程來限製打印的小數位數: - >>> np.format_float_positional(56294995342131.5, precision=3) '56294995342131.5'- 浮點打印例程使用精確但計算要求更高的算法來計算小數點後的位數。 - 或者,Python 的內置 - round函數對 64 位浮點值使用更準確但更慢的算法:- >>> round(56294995342131.5, 3) 56294995342131.5 >>> np.round(16.055, 2), round(16.055, 2) # equals 16.0549999999999997 (16.06, 16.05)- 參考:- “關於 IEEE 754 狀態的講義”,William Kahan,https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF 
 - 1:- 例子:- >>> np.around([0.37, 1.64]) array([0., 2.]) >>> np.around([0.37, 1.64], decimals=1) array([0.4, 1.6]) >>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value array([0., 2., 2., 4., 4.]) >>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned array([ 1, 2, 3, 11]) >>> np.around([1,2,3,11], decimals=-1) array([ 0, 0, 0, 10])
相關用法
- Python numpy argpartition用法及代碼示例
- Python numpy arctan用法及代碼示例
- Python numpy array用法及代碼示例
- Python numpy array_repr用法及代碼示例
- Python numpy arccos用法及代碼示例
- Python numpy array2string用法及代碼示例
- Python numpy arctan2用法及代碼示例
- Python numpy arctanh用法及代碼示例
- Python numpy arccosh用法及代碼示例
- Python numpy arange用法及代碼示例
- Python numpy argsort用法及代碼示例
- Python numpy array_equiv用法及代碼示例
- Python numpy array_str用法及代碼示例
- Python numpy array_equal用法及代碼示例
- Python numpy arcsinh用法及代碼示例
- Python numpy argmin用法及代碼示例
- Python numpy argmax用法及代碼示例
- Python numpy argwhere用法及代碼示例
- Python numpy arcsin用法及代碼示例
- Python numpy array_split用法及代碼示例
- Python numpy asscalar用法及代碼示例
- Python numpy any用法及代碼示例
- Python numpy ascontiguousarray用法及代碼示例
- Python numpy asarray_chkfinite用法及代碼示例
- Python numpy all用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.around。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
