GeezerVison is showing github.com — banker's rounding · splerman/python_reference@e1033b7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tutorials/key_differences_between_python_2_and_3.ipynb +179-6Lines changed: 179 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | "source": [ | 7 | 7 | "[Sebastian Raschka](http://sebastianraschka.com) \n", | 8 | 8 | "\n", | 9 | | - "last updated 05/27/2014\n", | | 9 | + "last updated 07/02/2016\n", | 10 | 10 | "\n", | 11 | 11 | "- [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", | 12 | 12 | "\n", | | 87 | 87 | "\n", | 88 | 88 | "- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n", | 89 | 89 | "\n", | | 90 | + "- [Banker's Rounding](#Banker's-Rounding)\n", | | 91 | + "\n", | 90 | 92 | "- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)" | 91 | 93 | ] | 92 | 94 | }, | | 2038 | 2040 | "<br>" | 2039 | 2041 | ] | 2040 | 2042 | }, | | 2043 | + { | | 2044 | + "cell_type": "markdown", | | 2045 | + "metadata": {}, | | 2046 | + "source": [ | | 2047 | + "## Banker's Rounding" | | 2048 | + ] | | 2049 | + }, | | 2050 | + { | | 2051 | + "cell_type": "markdown", | | 2052 | + "metadata": {}, | | 2053 | + "source": [ | | 2054 | + "[[back to the section-overview](#Sections)]" | | 2055 | + ] | | 2056 | + }, | | 2057 | + { | | 2058 | + "cell_type": "markdown", | | 2059 | + "metadata": {}, | | 2060 | + "source": [ | | 2061 | + "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", | | 2062 | + "- [https://en.wikipedia.org/wiki/Rounding#Round_half_to_even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)\n", | | 2063 | + "- [https://en.wikipedia.org/wiki/IEEE_floating_point#Roundings_to_nearest](https://en.wikipedia.org/wiki/IEEE_floating_point#Roundings_to_nearest)" | | 2064 | + ] | | 2065 | + }, | | 2066 | + { | | 2067 | + "cell_type": "markdown", | | 2068 | + "metadata": {}, | | 2069 | + "source": [ | | 2070 | + "#### Python 2" | | 2071 | + ] | | 2072 | + }, | | 2073 | + { | | 2074 | + "cell_type": "code", | | 2075 | + "execution_count": 5, | | 2076 | + "metadata": { | | 2077 | + "collapsed": false | | 2078 | + }, | | 2079 | + "outputs": [ | | 2080 | + { | | 2081 | + "name": "stdout", | | 2082 | + "output_type": "stream", | | 2083 | + "text": [ | | 2084 | + "Python 2.7.12\n" | | 2085 | + ] | | 2086 | + } | | 2087 | + ], | | 2088 | + "source": [ | | 2089 | + "print 'Python', python_version()" | | 2090 | + ] | | 2091 | + }, | | 2092 | + { | | 2093 | + "cell_type": "code", | | 2094 | + "execution_count": 6, | | 2095 | + "metadata": { | | 2096 | + "collapsed": false | | 2097 | + }, | | 2098 | + "outputs": [ | | 2099 | + { | | 2100 | + "data": { | | 2101 | + "text/plain": [ | | 2102 | + "16.0" | | 2103 | + ] | | 2104 | + }, | | 2105 | + "execution_count": 6, | | 2106 | + "metadata": {}, | | 2107 | + "output_type": "execute_result" | | 2108 | + } | | 2109 | + ], | | 2110 | + "source": [ | | 2111 | + "round(15.5)" | | 2112 | + ] | | 2113 | + }, | | 2114 | + { | | 2115 | + "cell_type": "code", | | 2116 | + "execution_count": 7, | | 2117 | + "metadata": { | | 2118 | + "collapsed": false | | 2119 | + }, | | 2120 | + "outputs": [ | | 2121 | + { | | 2122 | + "data": { | | 2123 | + "text/plain": [ | | 2124 | + "17.0" | | 2125 | + ] | | 2126 | + }, | | 2127 | + "execution_count": 7, | | 2128 | + "metadata": {}, | | 2129 | + "output_type": "execute_result" | | 2130 | + } | | 2131 | + ], | | 2132 | + "source": [ | | 2133 | + "round(16.5)" | | 2134 | + ] | | 2135 | + }, | | 2136 | + { | | 2137 | + "cell_type": "markdown", | | 2138 | + "metadata": {}, | | 2139 | + "source": [ | | 2140 | + "#### Python 3" | | 2141 | + ] | | 2142 | + }, | | 2143 | + { | | 2144 | + "cell_type": "code", | | 2145 | + "execution_count": 4, | | 2146 | + "metadata": { | | 2147 | + "collapsed": false | | 2148 | + }, | | 2149 | + "outputs": [ | | 2150 | + { | | 2151 | + "name": "stdout", | | 2152 | + "output_type": "stream", | | 2153 | + "text": [ | | 2154 | + "Python 3.5.1\n" | | 2155 | + ] | | 2156 | + } | | 2157 | + ], | | 2158 | + "source": [ | | 2159 | + "print('Python', python_version())" | | 2160 | + ] | | 2161 | + }, | | 2162 | + { | | 2163 | + "cell_type": "code", | | 2164 | + "execution_count": 5, | | 2165 | + "metadata": { | | 2166 | + "collapsed": false | | 2167 | + }, | | 2168 | + "outputs": [ | | 2169 | + { | | 2170 | + "data": { | | 2171 | + "text/plain": [ | | 2172 | + "16" | | 2173 | + ] | | 2174 | + }, | | 2175 | + "execution_count": 5, | | 2176 | + "metadata": {}, | | 2177 | + "output_type": "execute_result" | | 2178 | + } | | 2179 | + ], | | 2180 | + "source": [ | | 2181 | + "round(15.5)" | | 2182 | + ] | | 2183 | + }, | | 2184 | + { | | 2185 | + "cell_type": "code", | | 2186 | + "execution_count": 6, | | 2187 | + "metadata": { | | 2188 | + "collapsed": false | | 2189 | + }, | | 2190 | + "outputs": [ | | 2191 | + { | | 2192 | + "data": { | | 2193 | + "text/plain": [ | | 2194 | + "16" | | 2195 | + ] | | 2196 | + }, | | 2197 | + "execution_count": 6, | | 2198 | + "metadata": {}, | | 2199 | + "output_type": "execute_result" | | 2200 | + } | | 2201 | + ], | | 2202 | + "source": [ | | 2203 | + "round(16.5)" | | 2204 | + ] | | 2205 | + }, | | 2206 | + { | | 2207 | + "cell_type": "markdown", | | 2208 | + "metadata": {}, | | 2209 | + "source": [ | | 2210 | + "<br>\n", | | 2211 | + "<br>" | | 2212 | + ] | | 2213 | + }, | 2041 | 2214 | { | 2042 | 2215 | "cell_type": "markdown", | 2043 | 2216 | "metadata": {}, | | 2096 | 2269 | ], | 2097 | 2270 | "metadata": { | 2098 | 2271 | "kernelspec": { | 2099 | | - "display_name": "Python 3", | | 2272 | + "display_name": "Python 2", | 2100 | 2273 | "language": "python", | 2101 | | - "name": "python3" | | 2274 | + "name": "python2" | 2102 | 2275 | }, | 2103 | 2276 | "language_info": { | 2104 | 2277 | "codemirror_mode": { | 2105 | 2278 | "name": "ipython", | 2106 | | - "version": 3 | | 2279 | + "version": 2 | 2107 | 2280 | }, | 2108 | 2281 | "file_extension": ".py", | 2109 | 2282 | "mimetype": "text/x-python", | 2110 | 2283 | "name": "python", | 2111 | 2284 | "nbconvert_exporter": "python", | 2112 | | - "pygments_lexer": "ipython3", | 2113 | | - "version": "3.5.0" | | 2285 | + "pygments_lexer": "ipython2", | | 2286 | + "version": "2.7.12" | 2114 | 2287 | } | 2115 | 2288 | }, | 2116 | 2289 | "nbformat": 4, | |
|