digitalmars.D - Contract example suggestion
- Serge Ratke <Serge_member pathlink.com> Nov 15 2004
- Sjoerd van Leent <svanleent wanadoo.nl> Nov 15 2004
- Regan Heath <regan netwin.co.nz> Nov 15 2004
- Sjoerd van Leent <svanleent wanadoo.nl> Nov 16 2004
- Ben Hinkle <bhinkle4 juno.com> Nov 15 2004
- Serge Ratke <Serge_member pathlink.com> Nov 16 2004
- Russ Lewis <spamhole-2001-07-16 deming-os.org> Nov 16 2004
- "Simon Buchan" <currently no.where> Nov 17 2004
- Russ Lewis <spamhole-2001-07-16 deming-os.org> Nov 17 2004
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
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
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
On Mon, 15 Nov 2004 21:43:41 +0000 (UTC), Serge Ratke <Serge_member pathlink.com> 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); }
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
Regan Heath wrote:On Mon, 15 Nov 2004 21:43:41 +0000 (UTC), Serge Ratke <Serge_member pathlink.com> 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); }
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
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
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
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
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









Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> 