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

Comments

@mcandre
Copy link

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

This comment has been minimized.

Copy link
Member

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.

@smasher164

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Aug 12, 2019

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

This comment has been minimized.

Copy link

commented Sep 2, 2019

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.)

@gopherbot gopherbot closed this Sep 2, 2019

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