[Issue 8666] std.math.abs(int.min) returns int.min

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 16 10:53:11 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8666



--- Comment #2 from Caligo <iteronvexor at 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: -------


More information about the Digitalmars-d-bugs mailing list