What are the under-the-hood differences between round() and numpy.round()?

主宰稳场 提交于 2021-02-04 17:38:05

问题


Let's look at the ever-shocking round statement:

>>> round(2.675, 2)
2.67

I know why round "fails"; it's because of the binary representation of 2.675:

>>> import decimal
>>> decimal.Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')

What I do not understand is: why does NumPy not fail?

>>> import numpy
>>> numpy.round(2.675, 2)
2.6800000000000002

Thinking

Do not mind the extra zeros; it's an artefact from Python's printing internal rounding. If we look at the "exact" values, they're still different:

>>> decimal.Decimal(round(2.675, 2))
Decimal('2.6699999999999999289457264239899814128875732421875')

>>> decimal.Decimal(numpy.round(2.675, 2))
Decimal('2.680000000000000159872115546022541821002960205078125')

Why oh why does NumPy behave?

I thought at first that NumPy had to handle floats using extra bits, but:

>>> decimal.Decimal(numpy.float(2.675))
Decimal('2.67499999999999982236431605997495353221893310546875')
>>> decimal.Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')
# Twins!

What is happening behind the curtains? I looked a bit at NumPy's round implementation, but I'm a Python newbie and I don't see anything overly fishy.


回答1:


One on top of the hood difference is documented:

In cases where you're halfway between numbers, np.round rounds to the nearest "even" number (after multiplying by 10**n where n is the second argument to the respective round function) whereas the builtin round rounds away from 0.

>>> np.round(2.685, 2)
2.6800000000000002
>>> round(2.685, 2)
2.69

Under the hood, you can get differences when using the scaling parameter. Consider the differences between round(2.675 * 10.**2) and round(2.675, 2). This is certainly a result of the floating point math which as always has some rounding error associated with it. To go any further we would really need to look at the real implementation.



来源:https://stackoverflow.com/questions/20388071/what-are-the-under-the-hood-differences-between-round-and-numpy-round

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!