www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Contract example suggestion

reply Serge Ratke <Serge_member pathlink.com> writes:
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
next sibling parent reply 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
parent 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
prev sibling next sibling parent reply Regan Heath <regan netwin.co.nz> writes:
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
parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
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
prev sibling next sibling parent reply 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
parent Serge Ratke <Serge_member pathlink.com> writes:
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
prev sibling parent reply 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
parent reply "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
parent 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