assert(false)

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Fri Jun 21 02:56:19 PDT 2013


On 06/21/2013 09:51 AM, monarch_dodra wrote:
> Reading that code, it's not immediately obvious to me: The assert makes it self
> documenting.

I did also have to think about it for a few seconds.  You have a sum of values
from an array; you generate a uniformly-distributed random number point in [0.0,
sum); you sequentially sum values from the same array, and return when that
partial sum ("mass") exceeds the value of point.  Since point < sum you must do
this eventually, but the compiler has no way to recognize that the maximum
possible value of the variable mass will inevitably exceed the value of point.

> In any case, this goes back to what I first said: If you remove that assert, the
> compiler will complain that not all paths return.

Indeed. :-)


More information about the Digitalmars-d-learn mailing list