Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

negative zero #40

Open
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@danishprakash
Copy link

commented Sep 21, 2018

Negative zero is a vexed issue.

danishprakash added some commits Sep 21, 2018

>>> round(1.5)
2
>>> round(2.5)
2

This comment has been minimized.

Copy link
@jab

jab Sep 22, 2018

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?

This comment has been minimized.

Copy link
@danishprakash

danishprakash Sep 24, 2018

Author

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.

This comment has been minimized.

Copy link
@typhonic

typhonic Sep 24, 2018

The online docs for python3 round() states:
"if two multiples are equally close, rounding is done toward the even choice. "

This comment has been minimized.

Copy link
@danishprakash

danishprakash Sep 24, 2018

Author

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

This comment has been minimized.

Copy link
@jab

jab Sep 24, 2018

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.

This comment has been minimized.

Copy link
@jab

This comment has been minimized.

Copy link
@danishprakash

danishprakash Sep 29, 2018

Author

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.

This comment has been minimized.

Copy link
@jab

jab Oct 1, 2018

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.

This comment has been minimized.

Copy link
@danishprakash

danishprakash Oct 12, 2018

Author

@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/

This comment has been minimized.

Copy link
@jab

jab Oct 15, 2018

Great article, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.