rust-lang / rust-lang/libs-team
Constant and/or function for the implicit digit of the mantissa for floats
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 178
- Forks
- 28
- Avg merge
- 15m
- Merged PRs (30d)
- 1
Description
Proposal
Adding a constant or function that represents where the implicit digit of the mantissa of normal floating point numbers would be if it was explicit. (This is equivalent to the lowest bit of the exponent), and/or a method that would return constant if it was called on a normal value, zero otherwise.
Problem statement
Rust provides constants1 and functions for performing bitwise operations on floats, but doesn't include a constant/function for getting the implicit bit.
1The only one on stable is MANTISSA_DIGITS, but BITS, MANTISSA_MASK, EXPONENT_MASK, and SIGN_MASK are in the process of stabilization and constants for the exponent bias, signal bit and payload, have been proposed. See related work for links to the tracking issues/ACPs.
Motivating examples or use cases
The implementation of many if not most, that are written to compute a mathematic function and perform bitwise ops require introducing the implied bit on normal values in order to compute their result. Both the function and the constant provide methods of generating this value.
I personally would use this to implement arithmetic where the rounding mode can be specified.
Solution sketch
impl f64 {
/// <docs>
pub const IMPLICIT_BIT: u64 = 0x0010_0000_0000_0000;
/// <docs>
pub fn get_implied_bit(self) -> u64 {
f64::IMPLICIT_BIT * x.is_normal() as u64
}
}
Alternatives
Function vs constant vs both vs neither.
I generally think the constant would work better as bits will likely have already been moved off of float registers to moving it back to the function (and then possibly moving it back to the regular registers so you can actually perform is_normal) would be a waste, as well as the fact that it is ill-defined on infinities and NaNs.
Links and related work
- bit masks for the parts of non-nan floats: https://github.com/rust-lang/rust/issues/154064
- bits constant for floats: https://github.com/rust-lang/rust/issues/151073
- exp bias constant: #784
- bit mask for the parts of nans: #795
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the proposal and its related issues #154064, #151073, #784, and #795 to understand the surrounding float-constant work. The issue does not name implementation files or tests; done requires resolving whether a constant, function, both, or neither belongs in the API and defining the accepted behavior for normal values and non-normal values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100