Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 15, 2004 Contract example suggestion | ||||
---|---|---|---|---|
| ||||
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); } |
November 15, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serge Ratke | 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
|
November 15, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sjoerd van Leent | 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
|
November 15, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serge Ratke | 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/ |
November 15, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serge Ratke | 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.
|
November 16, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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 |
November 16, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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
|
November 16, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serge Ratke | 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...
|
November 17, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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/ |
November 17, 2004 Re: Contract example suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simon Buchan | 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);
|
Copyright © 1999-2021 by the D Language Foundation