Add: PI upper/lower bound f16 constants to ScalarValue#19497
Merged
Jefffrey merged 2 commits intoapache:mainfrom Dec 27, 2025
Merged
Add: PI upper/lower bound f16 constants to ScalarValue#19497Jefffrey merged 2 commits intoapache:mainfrom
Jefffrey merged 2 commits intoapache:mainfrom
Conversation
Jefffrey
reviewed
Dec 26, 2025
| // Constants defined for scalar construction. | ||
|
|
||
| // Next F16 value above π (upper bound) | ||
| pub(super) const PI_UPPER_F16: half::f16 = half::f16::from_bits(0x4249); |
Contributor
There was a problem hiding this comment.
How/where are these constants sourced from?
Contributor
Author
There was a problem hiding this comment.
These are the IEEE 754 binary16 half-precision bit representations closest to pi.
Contributor
Author
There was a problem hiding this comment.
I converted std::f64::consts::PI to f16 which results in 0x4248 (approx 3.1406). Since this value is slightly lower than the actual Pi. I used the next representable bit pattern (0x4249) to ensure a strict upper bound.
Jefffrey
approved these changes
Dec 26, 2025
Contributor
Jefffrey
left a comment
There was a problem hiding this comment.
Did some quick sanity checks on Rust playground with following code:
use half::f16;
fn main() {
println!("f16 PI + next_up");
println!("0x{:X}", (f16::PI).to_bits());
println!("0x{:X}\n", next_up(f16::PI).to_bits());
println!("f16 -PI + next_down");
println!("0x{:X}", (-f16::PI).to_bits());
println!("0x{:X}\n", next_down(-f16::PI).to_bits());
println!("f16 FRAC_PI_2 + next_up");
println!("0x{:X}", (f16::FRAC_PI_2).to_bits());
println!("0x{:X}\n", next_up(f16::FRAC_PI_2).to_bits());
println!("f16 -FRAC_PI_2 + next_down");
println!("0x{:X}", (-f16::FRAC_PI_2).to_bits());
println!("0x{:X}\n", next_down(-f16::FRAC_PI_2).to_bits());
}
pub fn next_up(v: f16) -> f16 {
let bits = v.to_bits();
let sign_mask = 0x8000u16;
let abs = bits & !sign_mask;
let next_bits = if abs == 0 {
let tiny_bits = 0x1u16;
tiny_bits
} else if bits == abs {
bits + 1
} else {
bits - 1
};
f16::from_bits(next_bits)
}
pub fn next_down(v: f16) -> f16 {
let bits = v.to_bits();
let sign_mask = 0x8000u16;
let abs = bits & !sign_mask;
let next_bits = if abs == 0 {
let tiny_bits = 0x1u16 | sign_mask;
tiny_bits
} else if bits == abs {
bits - 1
} else {
bits + 1
};
f16::from_bits(next_bits)
}Adapted next_up and next_down from f32/f64 to f16, see references:
- https://docs.rs/half/latest/src/half/binary16.rs.html#764
- https://doc.rust-lang.org/src/core/num/f32.rs.html#754
Constants look correct
Contributor
Author
|
Thanks for taking the time to write this . Looks great! |
Contributor
|
Thanks @xonx4l |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
Rationale for this change
To add Support scalarvalue APIs for f16 regarding Pi values
What changes are included in this PR?
Added half::f16 support to ScalarValue mathematical APIs.
Are these changes tested?
Yes
Are there any user-facing changes?
Yes