Positive

Bruce Adams tortoise_74 at yeah.who.co.uk
Sun Oct 5 04:08:48 PDT 2008


On Sun, 05 Oct 2008 09:04:37 +0100, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2008-10-05 01:55:43 -0400, Andrei Alexandrescu  
> <SeeWebsiteForEmail at erdani.org> said:
>
>> That will do a runtime check and throw if x is negative. That's  
>> unprecedented for an implicit cast, so I was thinking of defining a
>> universal "unsigned" template function that does the check:
>>  auto y = sqrt(unsigned(x));
>
> Well, at this point I would prefer if it was a contract. Having to  
> explicitly convert to unsigned just makes it harder than necessary.
>
> Throwing may be unprecedented for implict casts, but it is standard for  
> contracts using assertions. So I think it would be okay to implicit cast  
> and assert the value is non-negative (which would be equivalent to  
> enforcing a contract).
>
> But then, shouldn't sqrt(-1) give you NaN, or i?
>

Depends on the type of argument.

For normal integers it should balk at compile time if possible and  
assert/throw if necessary
For complex types i would make sense.
For IEEE real numbers the specification probably mandates some behaviour  
such as NaN. Anyone read it recently?



More information about the Digitalmars-d mailing list