www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: against enforce

reply Kagamin <spam here.lot> writes:
Steven Schveighoffer Wrote:

 For example, if I do:
 
 sqrt(1);
 
 There is never ever a need to test this in production code.  sqrt(1) is  
 always 1, and will always work.
 
 If I do:
 
 sqrt(-1);
 

pure function called on literals is evaluatable at compile time together with checks, so if sqrt run at runtime, this means input couldn't be validated that easily, so checks should be run too. This means that it's unnecessary to ever remove checks from sqrt.
Mar 26 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 26 Mar 2011 08:14:19 -0400, Kagamin <spam here.lot> wrote:

 Steven Schveighoffer Wrote:

 For example, if I do:

 sqrt(1);

 There is never ever a need to test this in production code.  sqrt(1) is
 always 1, and will always work.

 If I do:

 sqrt(-1);

pure function called on literals is evaluatable at compile time together with checks, so if sqrt run at runtime, this means input couldn't be validated that easily, so checks should be run too. This means that it's unnecessary to ever remove checks from sqrt.

It's not that simple. for example: sqrt(uniform(1.0, 2000.0)); This is completely safe, and needs no runtime error checks. It's easier to explain with simple examples, knowing that you can extrapolate more complex ones that the compiler can't "prove". -Steve
Mar 28 2011