Origins of the D Programming Language

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Dec 4 00:02:30 UTC 2018


On Mon, Dec 03, 2018 at 11:42:40PM +0000, Q. Schroll via Digitalmars-d wrote:
[...]
> ulong factorial(ulong n) {
>   ulong result = 1;
>   foreach (i; 0 .. n) result *= i; // off by one.
>   return result;
> }
> 
> Even the best make mistake ;)

Optimizer output:

	ulong factorial(n) { return 0; }

:-D

Where are the unittests that would have caught this?? ;-)

Furthermore, it's not just off by 1, since i=1 is a redundant case. The
loop really ought to start with 2.


T

-- 
In theory, software is implemented according to the design that has been carefully worked out beforehand. In practice, design documents are written after the fact to describe the sorry mess that has gone on before.


More information about the Digitalmars-d mailing list