banker's rounding

This commit is contained in:
rasbt 2016-07-02 02:41:01 -04:00
parent 4e92886d30
commit e1033b709e

View File

@ -6,7 +6,7 @@
"source": [
"[Sebastian Raschka](http://sebastianraschka.com) \n",
"\n",
"last updated 05/27/2014\n",
"last updated 07/02/2016\n",
"\n",
"- [Open in IPython nbviewer](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb?create=1) \n",
"\n",
@ -87,6 +87,8 @@
"\n",
"- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n",
"\n",
"- [Banker's Rounding](#Banker's-Rounding)\n",
"\n",
"- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)"
]
},
@ -2038,6 +2040,177 @@
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Banker's Rounding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to the section-overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 3 adopted the now standard way of rounding decimals when it results in a tie (.5) at the last significant digits. Now, in Python 3, decimals are rounded to the next even number. Although it's an inconvenience for code portability, it's supposedly a better way of rounding compared to rounding up as it avoids the bias towards large numbers. For more information, see the excellent Wikipedia articles and paragraphs:\n",
"- [https://en.wikipedia.org/wiki/Rounding#Round_half_to_even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)\n",
"- [https://en.wikipedia.org/wiki/IEEE_floating_point#Roundings_to_nearest](https://en.wikipedia.org/wiki/IEEE_floating_point#Roundings_to_nearest)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Python 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 2.7.12\n"
]
}
],
"source": [
"print 'Python', python_version()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"16.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(15.5)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"17.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(16.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Python 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.5.1\n"
]
}
],
"source": [
"print('Python', python_version())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(15.5)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(16.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -2096,21 +2269,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,