-
Notifications
You must be signed in to change notification settings - Fork 130
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
Add unit tests for trait method float::Float::integer_decode
#327
base: master
Are you sure you want to change the base?
Conversation
float::Float::integer_debug
float::Float::integer_decode
24606e0
to
ff6913b
Compare
8037d4b
to
ecfb76b
Compare
@cuviper this is ready for review. I'm especially interested to hear your thoughts about whether NaN values should be recoverable using the formula sign * mantissa * 2^exponent. |
ecfb76b
to
a00b3ec
Compare
`float::Float::integer_decode`
a00b3ec
to
25eb45f
Compare
It's a good observation -- no, I don't think there's any way to recover a NaN, apart from recognizing the special exponent and taking a different branch. Even then it will be hard to match the exact NaN bits, signaling or not, etc. Subnormals are also problematic, but those might be recoverable if you're more careful about if exponent >= MIN_EXP {
sign * magnitude * exponent.exp2()
} else {
// MIN_POSITIVE == 2^(MIN_EXP - 1)
(sign * magnitude * MIN_POSITIVE) * (exponent - (MIN_EXP - 1)).exp2()
} I'm ignoring type conversions there, and we don't have generic versions of those constants either. Even then, some hardware will flush subnormal arithmetic to zero anyway, so it still may not work. So I think it's probably not worth the effort to document anything more complicated, and we can just say that the simple formula only works for zero/normal/infinite values (per |
Thanks for the feedback!
I just pushed a change to update the doc comment. By the way, I just learned about Rust's documentation testing feature 😅 I'm new to Rust, so thanks for being patient. Should I move the tests I wrote to the method doc strings, or keep them in the |
I just added test cases for subnormals. The recovery code seems to work (within tolerance) for those, but I think they are being treated as zero. |
609effa
to
6309a53
Compare
Doc tests are nice, but we don't want to overwhelm the reader. I think yours are fine as unit tests.
Yes, your |
Motivation
While working on #326, I saw an opportunity to add unit test coverage for the trait method
float::Float::integer_decode
. I hope others find this helpful when troubleshooting this method.Changes
Float::integer_decode
with 32-bit and 64-bit inputs.Float::integer_decode
.