Repository navigation
negative zero - #40
danishprakash wants to merge 2 commits into
Conversation
| >>> round(1.5) | ||
| 2 | ||
| >>> round(2.5) | ||
| 2 |
There was a problem hiding this comment.
Interesting! The text under "Explanation" immediately below this says to read explanation.md for clarity, but I just read it and it doesn't explain this behavior. Would you be interested in adding an explanation there for this?
There was a problem hiding this comment.
Sorry for not including the explanation but honestly, I'm not really sure why this happens. The doc mentions nothing about such a case, I might have to ask someone from core-python regarding this, I guess.
There was a problem hiding this comment.
The online docs for python3 round() states:
"if two multiples are equally close, rounding is done toward the even choice. "
There was a problem hiding this comment.
@typhonic thanks for clearing that up, I seem to have misinterpreted that. Although I must admit, it seems like a weird choice. I'd add this to explanations.
There was a problem hiding this comment.
Definitely weird in light of e.g.
$ node -e 'console.log(Math.round(2.5))'
3
$ ruby -e 'puts (2.5).round()'
3
$ perl -e 'use Math::Round; print round(2.5)'
3
$ cat test_round.cpp
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%f\n", round(2.5));
}
$ g++ test_round.cpp && ./a.out
3.000000
$ cat TestRound.java
class TestRound {
public static void main(String[] args) {
System.out.println(Math.round(2.5));
}
}
$ javac TestRound.java && java TestRound
3
$ cat test_round.go
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Round(2.5))
}
$ go build test_round.go && ./test_round
3
$ cat test_round.rs
fn main() {
println!("{}", (2.5_f64).round());
}
$ rustc test_round.rs && ./test_round
3
I'm guessing this must have been discussed already on comp.lang.python, but I didn't immediately see where. Let's find or start a thread to get some more rationale for this and link to it from the explanation.
There was a problem hiding this comment.
Just found http://wiki.c2.com/?BankersRounding (thanks @novalis!)
There was a problem hiding this comment.
Thanks for that, I also found this post with a better explanation. I'm certainly sure this is not a wat anymore especially if you know what Banker's algorithm is and surprisingly not many people know about it.
There was a problem hiding this comment.
It's not a wat in the sense that it is a principled design decision. What I think may very well still be a wat is making this the default behavior of round(2.5) (rather than something like round(2.5, method=HALF_EVEN) in light of how uncommon this is among other popular languages / how relatively little-known banker's rounding is even among experienced programmers who've even studied applied mathematics in college.
There was a problem hiding this comment.
@jab you're right, although I'm not sure if this repo is actively maintained anymore, so it's hard to get the repo owner's take on this. Anyway, I came across this recently - https://realpython.com/python-rounding/
Negative zero is a vexed issue.