-
Notifications
You must be signed in to change notification settings - Fork 63
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
Allow 1 ULP of error in exp, ln, log10 and power GDA tests #152
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"os" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ericlagergren/decimal" | ||
|
@@ -277,15 +278,15 @@ got : %v | |
func check(t *testing.T, z, r *decimal.Big, c *Case, flags decimal.Condition) { | ||
helper(t)() | ||
if !equal(z, r) { | ||
str := fmt.Sprintf(`%s | ||
wanted: %q (%s:%d) | ||
got : %q (%s:%d) | ||
`, | ||
s := []string{ | ||
c.ShortString(10000), | ||
r, flags, -r.Scale(), | ||
z, z.Context.Conditions, -z.Scale(), | ||
) | ||
t.Log(str) | ||
fmt.Sprintf("wanted: %q (%s:%d)", r, flags, -r.Scale()), | ||
fmt.Sprintf("got : %q (%s:%d)", z, z.Context.Conditions, -z.Scale()), | ||
} | ||
if isInexact(z) && allow1ULPError[c.Op] && equalWithin1ULP(z, r) { | ||
s = append(s, "passed within 1 ULP") | ||
} | ||
t.Log(strings.Join(s, "\n"), "\n") | ||
} | ||
} | ||
|
||
|
@@ -321,6 +322,35 @@ func equal(x, y *decimal.Big) bool { | |
return cmp && scl && prec | ||
} | ||
|
||
func equalWithin1ULP(x, y *decimal.Big) bool { | ||
if !x.IsFinite() || !y.IsFinite() { | ||
return false | ||
} | ||
formX, negX, coeffX, expX := x.Decompose(make([]byte, 2<<3)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Off hand (I haven’t tested it yet, or had my coffee :-), I’m not sure if this is accurate. [123, -2] and [1234, -3] should compare equal within 1 ULP, but would fail here since their exponents differ. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @rdingwall, apparently I had added this comment (one year ago!) but never actually clicked “submit” on the review. Sorry for that. I only noticed when I got the email that you’d rebased! |
||
formY, negY, coeffY, expY := y.Decompose(make([]byte, 2<<3)) | ||
if formX != formY || negX != negY || expX != expY { | ||
return false | ||
} | ||
l := len(coeffX) | ||
if len(coeffY) != l { | ||
return false | ||
} | ||
for i := 0; i < l-1; i++ { | ||
if coeffX[i] != coeffY[i] { | ||
return false | ||
} | ||
} | ||
d := coeffX[l-1] - coeffY[l-1] | ||
return d == 0x01 || d == 0xff | ||
} | ||
|
||
var allow1ULPError = map[Op]bool{ | ||
Exp: true, | ||
Ln: true, | ||
Log10: true, | ||
Power: true, | ||
} | ||
|
||
// helper returns testing.T.Helper, if it exists. | ||
func helper(v interface{}) func() { | ||
if fn, ok := v.(interface { | ||
|
@@ -345,3 +375,7 @@ func convertConditions(c Condition) (decimal.Condition, bool) { | |
} | ||
return r, true | ||
} | ||
|
||
func isInexact(x *decimal.Big) bool { | ||
return x.Context.Conditions&decimal.Inexact == decimal.Inexact | ||
} |
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.
I refactored this string builder from one fmt.Sprintf() into strings.Join() to preserve test output (but have the ability to insert the optional ULP line in the middle)