## digitalmars.D - Contract example suggestion

• Serge Ratke (16/16) Nov 15 2004 Hi guys,
• Sjoerd van Leent (4/24) Nov 15 2004 Yup, else it does something completely insane.
• Regan Heath (11/28) Nov 15 2004 No, I think it's correct, using an example:
• Sjoerd van Leent (4/39) Nov 16 2004 Ah I did mistake sqrt (A square root) with a power.
• Ben Hinkle (3/21) Nov 15 2004 try plugging in some example values. For example take x = 4. then result...
• Serge Ratke (5/5) Nov 16 2004 In article , Ben Hinkle says...
• Russ Lewis (5/23) Nov 16 2004 As has been noted, the general idea of this contract is correct.
• Simon Buchan (7/30) Nov 17 2004 I'm assuming he meant double/real instead of long. (possibly from C's lo...
• Russ Lewis (4/17) Nov 17 2004 Well, if they were floats, then it would stil be broken, because of
```Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}
```
Nov 15 2004
Sjoerd van Leent <svanleent wanadoo.nl> writes:
```Serge Ratke wrote:
Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

Yup, else it does something completely insane.

Regards,
Sjoerd
```
Nov 15 2004
Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
```Sjoerd van Leent schrieb am 2004-11-15:
i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

Yup, else it does something completely insane.

so you are saying:

1) result := sqrt(x)
2) result == x * x

mhhhh... the only non-complex solution would be 0?

Thomas
```
Nov 15 2004
Regan Heath <regan netwin.co.nz> writes:
```On Mon, 15 Nov 2004 21:43:41 +0000 (UTC), Serge Ratke
Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the
out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

No, I think it's correct, using an example:

square_root(25);

if x == 25;
then result == 5;
therefore 5*5 == 25
or result*result == x

Regan

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
```
Nov 15 2004
Sjoerd van Leent <svanleent wanadoo.nl> writes:
```Regan Heath wrote:
On Mon, 15 Nov 2004 21:43:41 +0000 (UTC), Serge Ratke

Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't
the out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

No, I think it's correct, using an example:

square_root(25);

if x == 25;
then result == 5;
therefore 5*5 == 25
or result*result == x

Regan

Ah I did mistake sqrt (A square root) with a power.

My mistake,
Sjoerd
```
Nov 16 2004
Ben Hinkle <bhinkle4 juno.com> writes:
```Serge Ratke wrote:

Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the
out assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

try plugging in some example values. For example take x = 4. then result = 2
and 2*2 == 4.
```
Nov 15 2004
```In article <cnbeqf\$13bg\$1 digitaldaemon.com>, Ben Hinkle says...

Of course you are right,

sorry for that (should have read the function name closer, missed the *root*
part).

Sorry for that, again
```
Nov 16 2004
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```Serge Ratke wrote:
Hi guys,

i'm pretty new to D (wrote a simple Hello World), but ... shouldn't the out
assert be more like (x * x) == result?

long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

As has been noted, the general idea of this contract is correct.
However, the out contract is incorrect because it assumes that x is a
square number.  This contract will obviously fail if x is, for instance, 2.

WALTER: This needs to be changed on the website...
```
Nov 16 2004
"Simon Buchan" <currently no.where> writes:
```On Tue, 16 Nov 2004 06:53:44 -0700, Russ Lewis
<spamhole-2001-07-16 deming-os.org> wrote:

Serge Ratke wrote:
Hi guys,
i'm pretty new to D (wrote a simple Hello World), but ... shouldn't
the out
assert be more like (x * x) == result?
long square_root(long x)
in
{
assert(x >= 0);
}
out (result)
{
assert((result * result) == x);
}
body
{
return math.sqrt(x);
}

As has been noted, the general idea of this contract is correct.
However, the out contract is incorrect because it assumes that x is a
square number.  This contract will obviously fail if x is, for instance,
2.

WALTER: This needs to be changed on the website...

I'm assuming he meant double/real instead of long. (possibly from C's long
double?)
I don't think i've seen (m)any root functions taking integrals.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
```
Nov 17 2004
Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
```Simon Buchan wrote:
On Tue, 16 Nov 2004 06:53:44 -0700, Russ Lewis
<spamhole-2001-07-16 deming-os.org> wrote:
As has been noted, the general idea of this contract is correct.
However, the out contract is incorrect because it assumes that x is a
square number.  This contract will obviously fail if x is, for
instance,  2.

WALTER: This needs to be changed on the website...

I'm assuming he meant double/real instead of long. (possibly from C's
long  double?)
I don't think i've seen (m)any root functions taking integrals.

Well, if they were floats, then it would stil be broken, because of
float rounding issues.  You'd have to do something like
assert(abs((result*result) - x) < SOME_SMALL_VALUE);
```
Nov 17 2004