+ 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.

2nd Mar 2022, 6:37 AM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
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
2nd Mar 2022, 10:15 AM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
+ 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 👌.
2nd Mar 2022, 9:10 AM
Milad Hamidi
Milad Hamidi - avatar
+ 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
2nd Mar 2022, 6:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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.
2nd Mar 2022, 8:35 AM
Lisa
Lisa - avatar
+ 4
Hi dear mathematican, It is one more effect of the incorrect representation of floats.
2nd Mar 2022, 6:45 AM
Oma Falk
Oma Falk - avatar
+ 2
My keyboard has some issues
1st Jun 2022, 6:43 AM
benjamin biegon
benjamin biegon - avatar