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 a method to pow::Target
for returning difficulty as an f64.
#1707
Add a method to pow::Target
for returning difficulty as an f64.
#1707
Conversation
afc4ed3
to
93ac83a
Compare
This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post. https://blog.m-ou.se/floats/ Target::MAX was converted to a f64 and set as a const that is verified in a unit test.
93ac83a
to
2158f88
Compare
I also noticed that |
The method used in Perhaps the call is just moving the It depends on how precise we need it to be (probably not very). |
@junderw I think we'd prefer precision over speed. But |
concept ACK. Will test and provide a real ACK soon. This is a neat approach. I hadn't considered just constructing an IEEE float directly like this. |
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.
ACK 2158f88
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.
ACK 2158f88
FTR I did not read the blog post to understand how the to_f64
function works - just relied on the unit test values being correct. This would be a good function to attack with mutagen
.
/// Computes the popular "difficulty" measure for mining and returns a float value of f64. | ||
/// | ||
/// See [`difficulty`] for details. | ||
/// | ||
/// [`difficulty`]: Target::difficulty |
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.
nit but perhaps this is cleaner
/// Computes the popular "difficulty" measure for mining and returns a float value.
///
/// See [`Target::difficulty`] for details.
/// | ||
/// [`difficulty`]: Target::difficulty | ||
#[cfg_attr(all(test, mutate), mutate)] | ||
pub fn difficulty_float(&self) -> f64 { TARGET_MAX_F64 / self.0.to_f64() } |
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.
Why not just change the type above?
} | ||
|
||
// Target::MAX as a float value. Calculated with U256::to_f64. | ||
// This is validated in the unit tests as well. | ||
const TARGET_MAX_F64: f64 = 2.695953529101131e67; |
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.
With a bit of additional work you could make to_f64
const fn
so the magic constant wouldn't be needed. Maybe something for the future.
4924148 Swap out `Work::log2` implementation for `U256::to_f64` (junderw) Pull request description: See: #1707 (comment) ACKs for top commit: Kixunil: ACK 4924148 apoelstra: ACK 4924148 Tree-SHA512: 39d5d10b65c9597df1103df1639c57d432f04798b4b2be990b59275b8bd1f08560f72db8cda9edafad202ed0e4bf16cef7798279841b2d713ed482855498d1fe
Added to rust-bitcoin in rust-bitcoin/rust-bitcoin#1707.
Made available in rust-bitcoin as of rust-bitcoin/rust-bitcoin#1707
Made available in rust-bitcoin as of rust-bitcoin/rust-bitcoin#1707
Made available in rust-bitcoin as of rust-bitcoin/rust-bitcoin#1707
Closes #1703
This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post.
https://blog.m-ou.se/floats/
Target::MAX was converted to a f64 and set as a const that is verified in a unit test.
The code is rather confusing, so I took a crack at explaining it in my comments as well. Please let me know if you want it cleaned up some more.