Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math/big: (*Rat).SetString("1234") not the same as big.NewRat(1234, 1) #28884

Closed
damastes opened this issue Nov 20, 2018 · 1 comment
Closed

Comments

@damastes
Copy link

What version of Go are you using (go version)?

1.11.2

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOHOSTARCH="amd64"
GOHOSTOS="linux"

What did you do?

func main() {
	var x, y big.Rat
	
	x = *big.NewRat(123456789, 1)
	y.SetString("123456789")
	
	fmt.Printf("%#v\n", x)
	fmt.Printf("%#v\n", y)
}

https://play.golang.org/p/yFKM2k6djNA

What did you expect to see?

I would expect to see both structures be identical.

big.Rat{a:big.Int{neg:false, abs:big.nat{0x75bcd15}}, b:big.Int{neg:false, abs:big.nat{}}}
big.Rat{a:big.Int{neg:false, abs:big.nat{0x75bcd15}}, b:big.Int{neg:false, abs:big.nat{}}}

What did you see instead?

While the same numerical value, they are different for the purposes of test assertion.

big.Rat{a:big.Int{neg:false, abs:big.nat{0x75bcd15}}, b:big.Int{neg:false, abs:big.nat{}}}
big.Rat{a:big.Int{neg:false, abs:big.nat{0x75bcd15}}, b:big.Int{neg:false, abs:big.nat(nil)}}
@ianlancetaylor
Copy link
Contributor

As a side note, you should not write x := *big.NewRat(...). Shallow copies of *big.Rat values are not supported. This will be more clearly documented in the upcoming 1.12 release (https://tip.golang.org/pkg/math/big/#Rat).

To address the main issue, you should compare *big.Rat values using the Cmp method. The package makes no promise that values that compare as equal use the same internal representation.

@golang golang locked and limited conversation to collaborators Nov 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants