Both algorithms immediately return 0 in the limit == 0 case because their slow path guards check for something less than the limit, which is never the case for unsigned comparison with 0 :-)
I was recently reminded of this for the nearly-divisionless algorithm when I was using it to replace some code that uses two divisions per random number. The two-division code had an early check for limit < 2, which made me double-check the nearly-divisionless code, and I was pleased that the limit < 2 check could be completely deleted. (And I added a comment noting what happens in the limit == 0 case.)
no subject
Date: 2022-04-21 10:33 (UTC)Both algorithms immediately return 0 in the
limit == 0
case because their slow path guards check for something less than thelimit
, which is never the case for unsigned comparison with 0 :-)I was recently reminded of this for the nearly-divisionless algorithm when I was using it to replace some code that uses two divisions per random number. The two-division code had an early check for
limit < 2
, which made me double-check the nearly-divisionless code, and I was pleased that thelimit < 2
check could be completely deleted. (And I added a comment noting what happens in thelimit == 0
case.)