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

Closed
mdempsky opened this issue Jul 25, 2019 · 3 comments
Closed

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

mdempsky opened this issue Jul 25, 2019 · 3 comments
Milestone

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky 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: https://play.golang.org/p/UpXhGv8pLjQ

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
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jul 26, 2019

Change https://golang.org/cl/187697 mentions this issue: cmd/compile: constant arithmetic with typed complex numbers

@bkpathak

This comment has been minimized.

Copy link

@bkpathak bkpathak commented Jul 27, 2019

@mdempsky Hi Matthew, I can work on this.

@mdempsky

This comment has been minimized.

Copy link
Member Author

@mdempsky mdempsky commented Jul 27, 2019

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

@gopherbot gopherbot closed this in 52cff70 Aug 28, 2019
tomocy added a commit to tomocy/go that referenced this issue Sep 1, 2019
…umbers

Fixes golang#33285

Change-Id: Idd125e3342058051216be3f105330aef987320c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/187697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
t4n6a1ka added a commit to t4n6a1ka/go that referenced this issue Sep 5, 2019
…umbers

Fixes golang#33285

Change-Id: Idd125e3342058051216be3f105330aef987320c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/187697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.