Bug 9717 – `std.math.round` rounds away from zero instead of to the nearest even integer

Status
RESOLVED
Resolution
WONTFIX
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-14T05:16:00Z
Last change time
2016-01-03T14:15:08Z
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2013-03-14T05:16:12Z
Rounding to the nearest even integer is a default rounding mode and is an expected, useful (e.g. for mesurements rounding), and documented behaviour for `std.math.round`. But instead it currently rounds away from zero (it even uses C's `roundl`).
Comment #1 by hsteoh — 2013-03-15T10:36:32Z
Do you have an example code for this bug? I did this: void main() { writeln(std.math.round(1.4)); } and the output is 1, as expected.
Comment #2 by monarchdodra — 2013-03-15T10:46:11Z
(In reply to comment #1) > Do you have an example code for this bug? I did this: > > void main() { writeln(std.math.round(1.4)); } > > and the output is 1, as expected. I believe the "nearest even" is for the .5 tie-breakers: EG: round(0.5) == 0 round(1.5) == 2 round(2.5) == 2 It sounds strange at first, but it keeps things "sign agnostic" and nor shifts the average. http://en.wikipedia.org/wiki/Rounding#Round_half_to_even
Comment #3 by monarchdodra — 2013-03-15T10:46:50Z
That said, "nearest half" is one scheme among many other. I'm not sure about "default".
Comment #4 by verylonglogin.reg — 2013-03-16T00:42:37Z
(In reply to comment #3) > That said, "nearest half" is one scheme among many other. I'm not sure about > "default". I mean the function is documented to behave this way: http://dlang.org/phobos/std_math.html#round Also I haven't seen examples where you have to use another theme but I saw ones where you have to use "half to even" theme.
Comment #5 by lt.infiltrator — 2015-12-02T16:56:26Z
Both the C function[0] and the CRuntime_Microsoft version [1] actually explicitly do rounding away from zero rather than to even. There are other functions in std.math which round using the current rounding mode, which defaults to even, so this is most likely just a documentation error. [0]: www.codecogs.com/library/computing/c/math.h/round.php#sec1 [1]: in std/math.d
Comment #6 by lt.infiltrator — 2015-12-03T14:05:08Z
Comment #7 by github-bugzilla — 2015-12-05T17:56:53Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/91c38b4bfda60278924140e7bded63f56903a15e fix issue 9717 - `std.math.round` rounds away from zero instead of to the nearest even integer https://github.com/D-Programming-Language/phobos/commit/0c39d2d341eac3f3c81f6608dedd25ee2689d576 Merge pull request #3849 from Infiltrator/patch-1 fix issue 9717 - `std.math.round` rounds away from zero instead of to the nearest even integer
Comment #8 by verylonglogin.reg — 2015-12-06T11:08:44Z
This issue isn't fixed. It's state is WONTFIX. I'm strictly against documenting current useless specification as people either don't care about rounding or prefer bank rounding method as it is less biased.
Comment #9 by ibuclaw — 2015-12-06T15:58:17Z
(In reply to Denis Shelomovskij from comment #8) > This issue isn't fixed. It's state is WONTFIX. > I'm strictly against documenting current useless specification as people > either don't care about rounding or prefer bank rounding method as it is > less biased. There's rint/lrint?
Comment #10 by verylonglogin.reg — 2015-12-06T17:05:23Z
(In reply to Iain Buclaw from comment #9) > (In reply to Denis Shelomovskij from comment #8) > > This issue isn't fixed. It's state is WONTFIX. > > I'm strictly against documenting current useless specification as people > > either don't care about rounding or prefer bank rounding method as it is > > less biased. > > There's rint/lrint? Yes, there is a way to do it: ensure default rounding mode is set and call `rint`. But there is still two function calls (i.e. one `with(FloatingPointControl) assert(rounding == roundToNearest)`) if you control rounding mode and even more calls if you don't. My point is: why are we using C behavior for `round` when it is not a generally wanted behaviour? It's like keeping C issues in D and I don't see any point of it.
Comment #11 by github-bugzilla — 2016-01-03T14:15:08Z
Commits pushed to stable at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/91c38b4bfda60278924140e7bded63f56903a15e fix issue 9717 - `std.math.round` rounds away from zero instead of to the nearest even integer https://github.com/D-Programming-Language/phobos/commit/0c39d2d341eac3f3c81f6608dedd25ee2689d576 Merge pull request #3849 from Infiltrator/patch-1