Repository navigation
Floor and clamp the exponent of a root of zero #153
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
Open
youdie006
wants to merge
1
commit into
cockroachdb:master
Choose a base branch
from
youdie006:root-zero-exponent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package apd | ||
|
|
||
| import "testing" | ||
|
|
||
| // TestRootSpecialsZeroExponent checks the exponent and conditions of a | ||
| // square root or cube root of zero. TestGDA compares finite results with | ||
| // Cmp, which considers every zero equal regardless of exponent, so it | ||
| // cannot check these cases even though testdata carries them. | ||
| func TestRootSpecialsZeroExponent(t *testing.T) { | ||
| tests := []struct { | ||
| id string | ||
| prec uint32 | ||
| emax int32 | ||
| emin int32 | ||
| cbrt bool | ||
| x string | ||
| exp int32 | ||
| neg bool | ||
| cond Condition | ||
| }{ | ||
| // testdata/squareroot.decTest | ||
| {id: "sqtx006", prec: 9, emax: 384, emin: -383, x: "00.0", exp: -1}, | ||
| {id: "sqtx009", prec: 9, emax: 384, emin: -383, x: "00.000", exp: -2}, | ||
| {id: "sqtx017", prec: 9, emax: 384, emin: -383, x: "-0.0", exp: -1, neg: true}, | ||
| {id: "sqtx019", prec: 9, emax: 384, emin: -383, x: "-00.000", exp: -2, neg: true}, | ||
| {id: "sqtx9010", prec: 4, emax: 9, emin: -9, x: "0E-9", exp: -5}, | ||
| {id: "sqtx9012", prec: 4, emax: 9, emin: -9, x: "0E-11", exp: -6}, | ||
| {id: "sqtx9014", prec: 4, emax: 9, emin: -9, x: "0E-13", exp: -7}, | ||
| {id: "sqtx9023", prec: 4, emax: 9, emin: -9, x: "0E-24", exp: -12}, | ||
| {id: "sqtx9024", prec: 4, emax: 9, emin: -9, x: "0E-25", exp: -12, cond: Clamped}, | ||
| {id: "sqtx9027", prec: 4, emax: 9, emin: -9, x: "0E-28", exp: -12, cond: Clamped}, | ||
| {id: "sqtx9037", prec: 4, emax: 9, emin: -9, x: "0E+19", exp: 9}, | ||
| {id: "sqtx9038", prec: 4, emax: 9, emin: -9, x: "0E+20", exp: 9, cond: Clamped}, | ||
| {id: "sqtx9040", prec: 4, emax: 9, emin: -9, x: "0E+22", exp: 9, cond: Clamped}, | ||
| // testdata/cuberoot-apd.decTest | ||
| {id: "cbtx015", prec: 9, emax: 384, emin: -383, cbrt: true, x: "-0.00", exp: -1, neg: true}, | ||
| {id: "cbtx016", prec: 9, emax: 384, emin: -383, cbrt: true, x: "-00.0", exp: -1, neg: true}, | ||
| {id: "cbtx017", prec: 9, emax: 384, emin: -383, cbrt: true, x: "-0E+9", exp: 3, neg: true}, | ||
| {id: "cbtx020", prec: 9, emax: 384, emin: -383, cbrt: true, x: "-0E+12", exp: 4, neg: true}, | ||
| } | ||
| for _, tc := range tests { | ||
| // Parse the operand exactly, as TestGDA does; the context under test | ||
| // applies to the operation, not to the literal. | ||
| x, _, err := NewFromString(tc.x) | ||
| if err != nil { | ||
| t.Fatalf("%s: %v", tc.id, err) | ||
| } | ||
| c := &Context{Precision: tc.prec, MaxExponent: tc.emax, MinExponent: tc.emin, Rounding: RoundHalfUp} | ||
| d := new(Decimal) | ||
| var res Condition | ||
| if tc.cbrt { | ||
| res, err = c.Cbrt(d, x) | ||
| } else { | ||
| res, err = c.Sqrt(d, x) | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("%s: %v", tc.id, err) | ||
| } | ||
| if !d.IsZero() || d.Exponent != tc.exp || d.Negative != tc.neg { | ||
| t.Errorf("%s: got %#v, expected zero with exponent %d, negative %t", tc.id, d, tc.exp, tc.neg) | ||
| } | ||
| if res != tc.cond { | ||
| t.Errorf("%s: got conditions %q, expected %q", tc.id, res, tc.cond) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
rootSpecialsis also called byCbrt, flooring here changes positive cube-root results with negative, non-divisible exponents: for example,Cbrt("00.0")now returns0.0with exponent -1, whiletestdata/cuberoot-apd.decTestcasescbtx006–cbtx008require exponent 0.TestGDAdoes not expose this regression because it compares zeros numerically, and the new targeted test only selects the contradictory negative-zero expectations. Restrict the flooring change to square root, or first define and consistently update and test the cube-root contract.Useful? React with 👍 / 👎.