www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8666] New: std.math.abs(int.min) returns int.min

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8666

           Summary: std.math.abs(int.min) returns int.min
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: iteronvexor gmail.com


--- Comment #0 from Caligo <iteronvexor gmail.com> 2012-09-15 22:02:34 PDT ---
pragma(msg, abs(int.min));

prints:
-2147483648

pragma(msg, abs(cast(long)int.min));

prints:
2147483648L

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 15 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8666


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei metalanguage.com


--- Comment #1 from Andrei Alexandrescu <andrei metalanguage.com> 2012-09-16
03:41:26 PDT ---
This is a known anomaly. I assume you are proposing that returning long is the
right way to go about it, but it really isn't.

One possibility would be to return uint for int and smaller, and ulong for
long. That would be a principled way to go about it, but would break existing
code in rather subtle ways.

We could add uabs to return an unsigned type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 16 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8666



--- Comment #2 from Caligo <iteronvexor gmail.com> 2012-09-16 10:53:57 PDT ---
(In reply to comment #1)
 This is a known anomaly. I assume you are proposing that returning long is the
 right way to go about it, but it really isn't.
 
 One possibility would be to return uint for int and smaller, and ulong for
 long. That would be a principled way to go about it, but would break existing
 code in rather subtle ways.
 
 We could add uabs to return an unsigned type.

No, I wasn't trying to propose that returning a long is the solution. I was simply trying to demonstrate that the bug isn't triggered when the int is converted to a long. When I reported this, the anomaly wasn't obvious to me, and I thought there was a bug in math.abs. In my code I had a template: template Fun(long a) { enum b = abs(a); //... } but I was instantiating it with Fun!(int.min). Things started to go wrong when I changed the template to: template Fun(int a) { enum b = abs(a); //... } In this case the int was being implicitly converted to a long. I don't think the problem has a solution, but D's contracts will really help those who aren't aware of the anomaly: Num abs(Num)(Num x) out(result) { assert(result >= 0, "...and a helpful message."); } body { // ... } What do you guys think? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2012