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
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mdempsky
Copy link
Member

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 help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Jul 25, 2019
@mdempsky mdempsky added this to the Go1.14 milestone Jul 25, 2019
@gopherbot
Copy link

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

@bkpathak
Copy link

@mdempsky Hi Matthew, I can work on this.

@mdempsky
Copy link
Member Author

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

tomocy pushed 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 pushed 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>
@golang golang locked and limited conversation to collaborators Aug 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants