Jump to page: 1 2
Thread overview
[Issue 20205] std.math: Wrong result for abs(int.min)
Sep 12, 2019
Berni
Sep 12, 2019
Berni
Sep 12, 2019
Dlang Bot
Sep 13, 2019
Dlang Bot
Oct 04, 2019
Dlang Bot
Oct 06, 2019
Dlang Bot
Oct 06, 2019
Dlang Bot
September 12, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

Dominikus Dittes Scherkl <dominikus@scherkl.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dominikus@scherkl.de

--- Comment #1 from Dominikus Dittes Scherkl <dominikus@scherkl.de> ---
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;
}

--
September 12, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #2 from Dominikus Dittes Scherkl <dominikus@scherkl.de> ---
unittest
{
   byte a = -128;
   auto b = abs(a);
   assert(is(typeof(b) == ubyte));
   assert(b == 128);
}

--
September 12, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #3 from Berni <dlang@croco-puzzle.com> ---
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)

--
September 12, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #4 from Berni <dlang@croco-puzzle.com> ---
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

--
September 12, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@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

--
September 13, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
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

--
October 04, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
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

--
October 04, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

Ajieskola@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |Ajieskola@gmail.com
         Resolution|FIXED                       |---

--- Comment #8 from Ajieskola@gmail.com ---
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.

--
October 04, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #9 from Dominikus Dittes Scherkl <dominikus@scherkl.de> ---
(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.

--
October 06, 2019
https://issues.dlang.org/show_bug.cgi?id=20205

--- Comment #10 from Dlang Bot <dlang-bot@dlang.rocks> ---
@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

--
« First   ‹ Prev
1 2