Bug 20205 – std.math: Wrong result for abs(int.min)

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-09-12T06:57:43Z
Last change time
2019-10-06T23:22:10Z
Keywords
pull
Assigned to
No Owner
Creator
Berni

Comments

Comment #0 by dlang — 2019-09-12T06:57:43Z
assert(abs(int.min)==int.min); The same is true for the other signed integral types. I think, an Exception should be thrown instead.
Comment #1 by dominikus — 2019-09-12T07:59:47Z
Use better implementation of abs: /// get the absolute value of x as unsigned type. always succeeds, even for T.min Unsigned!T abs(T)(const(T) x) if(isIntegral!T) { static if(isSigned!T) if(x < 0) return -x; return x; }
Comment #2 by dominikus — 2019-09-12T08:05:47Z
unittest { byte a = -128; auto b = abs(a); assert(is(typeof(b) == ubyte)); assert(b == 128); }
Comment #3 by dlang — 2019-09-12T15:38:45Z
Having used Java for years unsigned integers often don't come to my mind... It's a good idea to use them! :-) But: xmin.d(7): Deprecation: integral promotion not done for -x, use '-transition=intpromote' switch or -cast(int)(x)
Comment #4 by dlang — 2019-09-12T16:30:53Z
In the forums I found a workaround: (0-x) instead if -x. When the transition has ended, this workaround can be removed. Unfortunatly I can't find anything on this deprecation on https://dlang.org/deprecate.html
Comment #5 by dlang-bot — 2019-09-12T19:07:39Z
@crocopaw created dlang/phobos pull request #7182 "Fix issue 20205 - std.math: Wrong result for abs(int.min)" fixing this issue: - Fix issue 20205 - std.math: Wrong result for abs(int.min) https://github.com/dlang/phobos/pull/7182
Comment #6 by dlang-bot — 2019-09-13T04:58:11Z
dlang/phobos pull request #7182 "Fix issue 20205 - std.math: Wrong result for abs(int.min)" was merged into master: - e6b5c68b4b76d637c8754643d4d908fc57b41e70 by Berni: Fix issue 20205 - std.math: Wrong result for abs(int.min) https://github.com/dlang/phobos/pull/7182
Comment #7 by dlang-bot — 2019-10-04T11:40:08Z
dlang/phobos pull request #7212 "Revert "Fix issue 20205 - std.math: Wrong result for abs(int.min)"" was merged into master: - 23fe9b85c16ff04b3c0d8540271b1a9d88a37ffa by Sebastian Wilzbach: Revert "Fix issue 20205 - std.math: Wrong result for abs(int.min)" https://github.com/dlang/phobos/pull/7212
Comment #8 by Ajieskola — 2019-10-04T11:53:51Z
The fix had to be reverted, due to confusion and code breakage it would have caused - see Dlang Bot links. So the original issue still persists. I personally think this should be left as-is. It's more a limitation of the signed integral types than the abs function. Throwing an exception wouldn't be good -it would break @nogc and nothrow callers and prevents using with -betterC. A notification in function documentation should suffice.
Comment #9 by dominikus — 2019-10-04T12:17:28Z
(In reply to Ajieskola from comment #8) > The fix had to be reverted, due to confusion and code breakage it would have > caused - see Dlang Bot links. I can't see why it is confusing that the result of abs() is unsigned. Vice versa - the absolute value is especially designed to result in an unsigned type and I find it heavily irritating that it doesn't. > So the original issue still persists. Yeah. Bad design. > I personally think this should be left as-is. So I will continue to use my better approach instead of phobos. Sic.
Comment #10 by dlang-bot — 2019-10-06T14:45:42Z
@crocopaw created dlang/phobos pull request #7214 "Fix issue 20205 - std.math: Wrong result for abs(int.min)" fixing this issue: - Fix issue 20205 - std.math: Wrong result for abs(int.min) https://github.com/dlang/phobos/pull/7214
Comment #11 by dlang-bot — 2019-10-06T23:22:10Z
dlang/phobos pull request #7214 "Fix issue 20205 - std.math: Wrong result for abs(int.min)" was merged into master: - 56c1473f2acdeecde249d8ec731781b0683164b3 by Berni: Fix issue 20205 - std.math: Wrong result for abs(int.min) https://github.com/dlang/phobos/pull/7214