← Back to DAGDA | Support | Source: https://github.com/cosmologicon/pywat/pull/40
Skip to content

negative zero - #40

Closed
danishprakash wants to merge 2 commits into
cosmologicon:masterfrom
danishprakash:master
Closed

danishprakash wants to merge 2 commits into
cosmologicon:masterfrom
danishprakash:master

Conversation

@danishprakash

@danishprakash danishprakash commented Sep 21, 2018 •

Copy link
Copy Markdown

Negative zero is a vexed issue.

Comment thread README.md
>>> round(1.5)
2
>>> round(2.5)
2

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@danishprakash danishprakash Sep 24, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jab jab Sep 24, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great article, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants