Proposal: Make gcd total

wren ng thornton wren at freegeek.org
Sun May 29 02:21:28 CEST 2011


On 5/28/11 8:38 AM, Daniel Fischer wrote:
> On Saturday 28 May 2011 14:26:45, Ross Paterson wrote:
>> On 28 May 2011 11:59, Daniel Fischer wrote:
>>> I would like to propose the elimination of the special error case
>>>
>>> gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
>>
>> Just deleting that line sounds good.  For documentation, I'd suggest:
>>
>>    gcd x y is the non-negative factor of both x and y of which every
>>    common factor of x and y is also a factor; for example gcd 4 6 = 2,
>>    gcd (-4) 6 = 2, gcd (-4) (-6) = 2, gcd 0 4 = 4, gcd 0 0 = 0.
>>    (That is, the common divisor that is "greatest" in the divisibility
>>    ordering.)
>>
>
> Thanks, that's pretty nice.
>
> (By the way, non-negative, like the current positive, is not entirely true,
> for bounded signed integer types [with twos' complement representation],
> gcd minBound minBound = gcd minBound 0 = minBound<  0,
> should that special case be mentioned or should we ignore it like we
> currently do?)

That's a sticky issue, but one that needs to be documented. Given that 
the result is always positive with the exception of gcd 0 0, gcd 
minBound minBound, gcd minBound 0, and gcd 0 minBound, I think these 
corner cases should be specifically enumerated.

-- 
Live well,
~wren



More information about the Libraries mailing list