Python numpy.round(0.5) 的一個問題

  在完成第三週的作業時候,並沒有像第二週一樣直接暴力的使用if else實現最後的預測結果部分的判斷,而是使用了np.round(),於是測試時候出現了一個問題。

輸入:

import numpy as np

print(np.round(0.5))
print(np.round(1.5))

輸出:

0.0
2.0

  很奇怪啊,明明是四捨五入,但是輸出了0,在文檔中給出了相關的回答:

python3.5中:

values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice.

  也就是說如果四捨五入時候到兩邊的距離一樣,結果會是偶數部分的值,所以結果是0.0和2.0。

python2.7中:

values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0

  可以看到,在2.7版本中,如果兩邊距離相等,那麼會等於遠離0的值,所以如果是2.7版本,輸出應該是:

1.0
2.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章