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

Enhancement: math.RoundOut #33417

Closed
mcandre opened this issue Aug 1, 2019 · 5 comments
Closed

Enhancement: math.RoundOut #33417

mcandre opened this issue Aug 1, 2019 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@mcandre
Copy link

mcandre commented Aug 1, 2019

Ceil and Floor don't quite do what most people expect them to, for negative numbers for example. It would be helpful to provide a convenience method RoundOut, which rounds all positive numbers up, and all negative numbers down.

This can be implemented in JavaScript as math.Sign(x) * math.Ceil(math.Abs(x)), but Go doesn't quite have the same Sign() behavior.

@agnivade
Copy link
Contributor

agnivade commented Aug 2, 2019

There are Round and RoundToEven which do exactly what you say unless I am missing something.

Please update your proposal clarifying what is missing in those functions that you require otherwise. Thanks.

@agnivade agnivade added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 2, 2019
@smasher164
Copy link
Member

smasher164 commented Aug 2, 2019

Would the following exhibit the behavior you want?

func RoundOut(x float64) float64 {
	return math.Copysign(math.Ceil(math.Abs(x)), x)
}

Note that your proposed behavior is "round away from zero", which is non-standard. I'm not exactly sure why one would want to round -1.3 down to -2.0 as well as 1.3 to 2.0. Can you please explain your use case?

@mpx
Copy link
Contributor

mpx commented Aug 12, 2019

Go implements the IEEE rounding modes (API: Ceil, Floor, Trunc, Round, RoundToEven - #21748). Other discussions in #20100, #4594.

I'm not sure there would be much demand for esoteric non-IEEE modes in the standard library.

@agnivade
Copy link
Contributor

Sorry I missed that this is about rounding also 1.3 to 2 apart from 1.7 to 2. In any case, the question still stands to explain the use case for this and why does this need to be in the standard library.

@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Sep 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants