gcd() and lcm() may return negative values
Reported by Samuel GOUGEON (@sgougeon)
BUG DESCRIPTION:
----------------
When gcd() is fed with some negative input, the result is sometimes negative.
Further trials show that the rule setting the sign of the returned GCD is quite obscure.
Example:
--> gcd([70 -245])
ans =
-35
--> gcd([-70 245])
ans =
35
Some investigations about the GCD computed with some other languages lead to the following results:
* Octave: gcd is always > 0
>> gcd(245,-70)
ans = 35
>> gcd(-245,70)
ans = 35
>> gcd(-245,-70)
ans = 35
* Python: https://docs.python.org/dev/library/math.html#math.gcd
gcd(a, b)
Return the greatest common divisor of the integers a and b.
The value of gcd(a, b) is the largest positive integer that divides both a and b.
* Julia: http://docs.julialang.org/en/stable/stdlib/math/?highlight=gcdx#Base.gcdx
gcd(x, y)
Greatest common (positive) divisor (or zero if x and y are both zero).
gcdx(x,y)
../.. Furthermore, the signs of u and v are chosen so that [the gc]d is positive.
julia> gcdx(70,245)
(35,-3,1)
julia> gcdx(-70,245)
(35,3,1)
julia> gcdx(70,-245)
(35,-3,-1)
So, we propose here to return a GCD always > 0 instead of a somewhat arbitrary sign,
while keeping the property P*U = [0 0 .. 0 GCD] when the argout#2 U is queried and used.
ERROR LOG:
----------
none. unexpected obscure result
HOW TO REPRODUCE THE BUG:
-------------------------
gcd([70 -245])>0
gcd([70 -245])==gcd([-70 245])
OTHER INFORMATION:
------------------
Strange behavior found when fixing the bug 15017