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

cmd/compile: constant arithmetic with typed complex numbers is too precise #33285

mdempsky opened this issue Jul 25, 2019 · 3 comments


Copy link

commented Jul 25, 2019

When computing constant arithmetic of typed floating-point numbers, we truncate the results based on the floating-point type. That is, if x and y are float32 constants, then x + y will be truncated to fit into a float32 too, even if we could compute it more precisely:

This is currently handled in const.go:setconst:

// Truncate precision for non-ideal float.
if v.Ctype() == CTFLT && n.Type.Etype != TIDEAL {
n.SetVal(Val{truncfltlit(v.U.(*Mpflt), n.Type)})

However, there's no corresponding logic for truncating typed complex constants. E.g., see this sample program that demonstrates "catastrophic cancellation" for arithmetic on const floats, non-const floats, and non-const complexes, but (non-spec-compliantly) computes the precise result for the const complex arithmetic:

gccgo correctly computes 0 for all results. go/types looks like it probably handles this correctly too, but cmd/gotype doesn't easily expose constant values, and I haven't tried writing a test case.

Tagging "help wanted" since this is probably an easy CL if someone wants to grab it. There's already trunccmplxlit, just need to call it.

@mdempsky mdempsky added this to the Go1.14 milestone Jul 25, 2019


This comment has been minimized.

Copy link

commented Jul 26, 2019

Change mentions this issue: cmd/compile: constant arithmetic with typed complex numbers


This comment has been minimized.

Copy link

commented Jul 27, 2019

@mdempsky Hi Matthew, I can work on this.


This comment has been minimized.

Copy link
Member Author

commented Jul 27, 2019

@bkpathak Thanks for the offer! However, @choleraehyq has already started work on this. See CL 187697 linked above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
4 participants
You can’t perform that action at this time.