math/big: add Floor and Ceil methods to Rat
Adds methods for floor and ceil of a rational number r, floor(r) is the
largest integer <= r and ceil(r) is the smallest integer >= r.
diff --git a/src/math/big/rat.go b/src/math/big/rat.go
index c7f79a5..1a8e2a6 100644
--- a/src/math/big/rat.go
+++ b/src/math/big/rat.go
@@ -559,3 +559,18 @@
z.a.neg = a.neg != b.neg
return z.norm()
}
+
+// Floor returns the largest [Int] <= z.
+func (z *Rat) Floor() *Int {
+ // z.b is positive, so Euclidean division == floor division
+ return new(Int).Div(&z.a, &z.b)
+}
+
+// Ceil returns the smallest [Int] >= z.
+func (z *Rat) Ceil() *Int {
+ if z.IsInt() {
+ return new(Int).Set(&z.a)
+ }
+ f := z.Floor()
+ return f.Add(f, intOne)
+}
diff --git a/src/math/big/rat_test.go b/src/math/big/rat_test.go
index d98c89b..0431ab9 100644
--- a/src/math/big/rat_test.go
+++ b/src/math/big/rat_test.go
@@ -744,3 +744,53 @@
<-c
}
}
+
+var ratFloorTests = []struct {
+ rat string
+ out int64
+}{
+ {"123456.78", 123456},
+ {"100.5", 100},
+ {"5645.0222", 5645},
+ {"89898989", 89898989},
+ {"0", 0},
+ {"-123456.78", -123457},
+ {"-100.5", -101},
+ {"-5645.0222", -5646},
+ {"-89898989", -89898989},
+}
+
+func TestRatFloor(t *testing.T) {
+ for i, test := range ratFloorTests {
+ x, _ := new(Rat).SetString(test.rat)
+ out := x.Floor()
+ if out.Cmp(NewInt(test.out)) != 0 {
+ t.Errorf("#%d got out = %v; want %v", i, out, test.out)
+ }
+ }
+}
+
+var ratCeilTests = []struct {
+ rat string
+ out int64
+}{
+ {"123456.78", 123457},
+ {"100.5", 101},
+ {"5645.0222", 5646},
+ {"89898989", 89898989},
+ {"0", 0},
+ {"-123456.78", -123456},
+ {"-100.5", -100},
+ {"-5645.0222", -5645},
+ {"-89898989", -89898989},
+}
+
+func TestRatCeil(t *testing.T) {
+ for i, test := range ratCeilTests {
+ x, _ := new(Rat).SetString(test.rat)
+ out := x.Ceil()
+ if out.Cmp(NewInt(test.out)) != 0 {
+ t.Errorf("#%d got out = %v; want %v", i, out, test.out)
+ }
+ }
+}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?
Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Congratulations on opening your first change. Thank you for your contribution!
Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.
Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.
During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
- [how to update commit messages](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message) for PRs imported into Gerrit.
- the Go project's [conventions for commit messages](https://go.dev/doc/contribute#commit_messages) that you should follow.
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
Done
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
This looks reasonable; it will need to wait for the proposal to be accepted, and for the go1.26 freeze to end (in Feb) before we merge it though.
// Floor returns the largest [Int] <= z.
func (z *Rat) Floor() *Int {
// z.b is positive, so Euclidean division == floor division
return new(Int).Div(&z.a, &z.b)
}
// Ceil returns the smallest [Int] >= z.
func (z *Rat) Ceil() *Int {(Following Num and Denom, it seems fine not to accept an additional destination *Int variable, which is an allocation optimization.)
var ratFloorTests = []struct {This can be a local variable (or literal) of the test. Ditto Ceil.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
This can be a local variable (or literal) of the test. Ditto Ceil.
Done
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
Code LGTM. Could you also add a release note (in GOROOT/doc/next) and an API manifest (in GOROOT/api/next)? The manifest should mention the issue number of the proposal, which is now the blocker.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Code LGTM. Could you also add a release note (in GOROOT/doc/next) and an API manifest (in GOROOT/api/next)? The manifest should mention the issue number of the proposal, which is now the blocker.
Done. (I think?)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Updated according to issue.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |