+ 6
[Solved] Round Function as IEEE 754 standard.
I was confused about the behaviour of "round()" function, see this code below: print(round(-2.5)) #prints -2 print(round(-1.5)) #also prints -2 print(round(-0.5)) #prints 0 print(round(0.5)) #also prints 0 print(round(1.5)) #prints 2 print(round(2.5)) #also prints 2 Seems like Python rounds the 0.5 to the nearest even number. At first, I was confused why but thank you everyone to contribute specially ChaoticDawg for your informative links. I understood the issue and is now resolved for me. â I want to share my understandings here in comments. Please let me know if you had the same issue too.
6 Answers
+ 8
đ˘ There are 6 different kind of rounding to nearest number when it comes to dealing with "tie breaking" which means when the number is half way between another two numbers:
1. Round half up (toward +inftyâĄď¸):
Example: 23.5 -> 24
â23.5 -> â23
â it isn't symmetrical and have positive bias
2. Round half down (toward -inftyâŹ
ď¸):
Example: 23.5 -> 23
â23.5 -> â24
â it isn't symmetrical and have negative bias
3. Round half toward zero (away from inftyâĄď¸âŹ
ď¸):
Example: 23.5 -> 23
â23.5 -> â23
â have bias toward zero
4. Round half away from zero (toward inftyâď¸):
Example: 23.5 -> 24
â23.5 -> â24
â have bias away from zero
5. Round half to even (Convergent, Statistician's, Gaussian, banker's rounding):
â
This tie-breaking rule is without positive/negative bias and without bias toward/away from zero and is the default rounding mode used in IEEE 754 operations which Python uses.
Example: 23.5 -> 24
24.5 -> 24
-23.5 -> -24
-24.5 -> -24
6. Round half to odd (this one is similar to previous one but its not much common
+ 12
Just remember this: Python round() function rounds values of .5 toward an even integer. If you want the real rounding structure you should use decimal module. it is on the link ChaoticDawg has posted here. It is very comprehensive đ.
+ 6
Has to do with floating point arithmetic.
https://docs.python.org/3/tutorial/floatingpoint.html
The round() function will round towards the even outcome if the rounding point is equidistant to the nearest integer.
https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior
https://docs.python.org/3/library/functions.html#round
+ 6
In addition to general floating point concerns:
The default round algorithm now implemented in Python is called "round half to even" or "banker's rounding". Unlike other rounding methods, including the one most of us learn at school, this algorithm prevents/ reduces a bias towards or away from zero and a bias for positive of negative numbers.
+ 4
Hi dear mathematican,
It is one more effect of the incorrect representation of floats.
+ 2
My keyboard has some issues