Positive
Bruce Adams
tortoise_74 at yeah.who.co.uk
Sun Oct 5 04:23:55 PDT 2008
On Sun, 05 Oct 2008 11:18:24 +0100, KennyTM~ <kennytm at gmail.com> wrote:
>
> Probably they'd used (0, +∞) and [0, +∞) instead.
>
> ---
>
> BTW, negative real numbers can also be indicated as ℝ⁻ (R^- if Unicode
> is not supported). Is it now special enough to deserve a Negative!()
> template? :p
Nice use of unicode.
>
> I think these sign checking should be done through contracts or
> "conditional template" (? whatever it's called; I haven't used one of
> these yet) instead. Unless you can runtime check that
>
> Positive!(double) x = 6;
> Positive!(double) y = 12;
Compile time check
> Positive!(double) z = void;
This must be an syntax error surely?
> z = x - y; // raises error.
The return type of subtraction goes outside the postive domain:
i.e. if it needed to exist (which it probably doesn't) the function spec
would be:
double opMinus(Positive!(double) y, Positive!(double) y);
So this is actually:
z = cast(Positive!(double))(x - y)
Hence there is the necessary check which could be either run-time or
compile time.
> Positive!(double) w;
> din.readf("%g", &w); // raises error if user enters negative number.
>
> But I don't think uint, etc now even do these checks.
This would ideally work as above but I see your problem.
The contract of readf is not tight enough. You should rewrite as:
double wtemp;
din.read("%g", &wtemp);
Positive!(double) w = wtemp; // type check applied here
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list