You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The cause is clear from examining the start of the binaryGCD method in int.go:
682func (z*Int) binaryGCD(a, b*Int) *Int {
683u:=z684v:=new(Int)
685686// use one Euclidean iteration to ensure that u and v are approx. the same size687switch {
688caselen(a.abs) >len(b.abs):
689u.Set(b)
690v.Rem(a, b)
691caselen(a.abs) <len(b.abs):
692u.Set(a)
693v.Rem(b, a)
694default:
695u.Set(a)
696v.Set(b)
697 }
In the misbehaving call, z and b point to the same object, so the calls to u.Set() will modify the value of b. Since the rest of the function does not use a and b, it should be possible to fix this by simply setting v before u in each of the three instances above.
Lastly, go version returns go version go1.4.2 linux/amd64
The text was updated successfully, but these errors were encountered:
mikioh
changed the title
math/big.Int.GCD misbehaves when argument reused for result
math/big: Int.GCD misbehaves when argument reused for result
Jun 19, 2015
Code of the form x.GCD(nil, nil, y, x) is likely to return an incorrect result. For example, this code:
should print
111111
. Instead, it prints122111
.The cause is clear from examining the start of the binaryGCD method in int.go:
In the misbehaving call, z and b point to the same object, so the calls to u.Set() will modify the value of b. Since the rest of the function does not use a and b, it should be possible to fix this by simply setting v before u in each of the three instances above.
Lastly, go version returns
go version go1.4.2 linux/amd64
The text was updated successfully, but these errors were encountered: