-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 constants for f16 and f128 #123850
Add constants for f16 and f128 #123850
Conversation
cc @tgross35 |
This comment has been minimized.
This comment has been minimized.
9810c6e
to
e972a7f
Compare
This comment has been minimized.
This comment has been minimized.
Note that for constants gated under |
The test failure seems to be a panic in beta clippy.
|
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.
Hey, thank you for the help here!
I was holding off on these until we had the ability to actually run code that involves these types, which unfortunately we won't until I get compiler_builtins
updated (well, code will run on x86 but nothing else so we can't actually merge anything). I was trying to add as little as possible until we get tests, but don't have any big problem with adding these.
One change needed - the literals we have aren't high enough precision for f128. I have a Julia script to generate longer ones using arbitrary precision math, https://github.com/tgross35/apfloat-consts. You can just copy the consts from output.rs.
This is the other reason I haven't added too much more, clippy and CTFE both have some @rustbot label +F-f16_and_f128 |
e972a7f
to
3f29034
Compare
Oops, I used 36 significant digits for I changed |
For the clippy warning, I think clippy is already fine in nightly, so instead of adding extra clippy allows, I think I might just wait for the next beta to be cut. |
This comment has been minimized.
This comment has been minimized.
3f29034
to
2488138
Compare
This comment has been minimized.
This comment has been minimized.
2488138
to
37bec59
Compare
This comment has been minimized.
This comment has been minimized.
NaN and infinity are not included as they require arithmetic.
NaN and infinity are not included as they require arithmetic.
I rebased (to retrigger the tests) now that the clippy tests in mingw-check are using 2024-04-29 instead of 2024-03-19, and the tests now pass. |
Could you post the code for this so we can reproduce in the future? |
This is my code below. Should I include the code in a comment in f128.rs? use rug::float::Constant;
use rug::{Assign, Float};
fn decimal_string(val: &Float, num_digits: usize) -> String {
let (sign, mut digits, exp) = val.to_sign_string_exp(10, Some(num_digits));
if let Some(exp) = exp {
if exp <= 0 {
let one_minus_exp = usize::try_from(1 - exp).unwrap();
digits.insert_str(0, &"0".repeat(one_minus_exp));
digits.insert(1, '.');
} else {
let exp = usize::try_from(exp).unwrap();
if exp >= num_digits {
digits.push_str(&"0".repeat(exp - num_digits));
} else {
digits.insert(exp, '.');
}
}
}
if sign {
digits.insert(0, '-');
}
digits
}
fn float<T>(t: T) -> Float
where
Float: Assign<T>,
{
Float::with_val(1000, t)
}
fn print(name: &str, f: Float) {
let s = decimal_string(&f, 60);
assert_eq!(
Float::with_val(113, Float::parse(&s).unwrap()),
Float::with_val(113, &f)
);
println!("pub const {name}: f128 = {s}_f128;");
}
fn main() {
print("PI", float(Constant::Pi));
print("TAU", float(Constant::Pi) * 2);
print("PHI", float(1.25).sqrt() + 0.5);
print("EGAMMA", float(Constant::Euler));
print("FRAC_PI_2", float(Constant::Pi) / 2);
print("FRAC_PI_3", float(Constant::Pi) / 3);
print("FRAC_PI_4", float(Constant::Pi) / 4);
print("FRAC_PI_6", float(Constant::Pi) / 6);
print("FRAC_PI_8", float(Constant::Pi) / 8);
print("FRAC_1_PI", float(Constant::Pi).recip());
print("FRAC_1_SQRT_PI", float(Constant::Pi).sqrt().recip());
print("FRAC_2_PI", 2 / float(Constant::Pi));
print("FRAC_2_SQRT_PI", 2 / float(Constant::Pi).sqrt());
print("SQRT_2", float(2).sqrt());
print("FRAC_1_SQRT_2", float(0.5).sqrt());
print("SQRT_3", float(3).sqrt());
print("FRAC_1_SQRT_3", float(3).sqrt().recip());
print("E", float(1).exp());
print("LOG2_10", float(10).log2());
print("LOG2_E", float(1).exp().log2());
print("LOG10_2", float(2).log10());
print("LOG10_E", float(1).exp().log10());
print("LN_2", float(2).ln());
print("LN_10", float(10).ln());
} |
No this is fine, just wanted a reference in case anybody wants to replicate or add more in the future 👍 |
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.
Okay, these all look good to me. I double checked the associated values against http://weitz.de/ieee/ and the math constants against what Julia says https://github.com/tgross35/apfloat-consts/blob/49cb0d131ea2d881205852c31ba87236fdba9749/output.rs.
Honestly it would be better to just generate bit patterns for the MIN
/MAX
/EPSILON
/etc values rather than having a tough to read literal, but that's not something to do now.
I can't r+ so somebody else has to. Thanks for the patch!
/// Smallest finite `f128` value. | ||
/// | ||
/// Equal to −[`MAX`]. | ||
/// | ||
/// [`MAX`]: f128::MAX | ||
#[unstable(feature = "f128", issue = "116909")] | ||
pub const MIN: f128 = -1.18973149535723176508575932662800701e+4932_f128; | ||
/// Smallest positive normal `f128` value. | ||
/// | ||
/// Equal to 2<sup>[`MIN_EXP`] − 1</sup>. | ||
/// | ||
/// [`MIN_EXP`]: f128::MIN_EXP | ||
#[unstable(feature = "f128", issue = "116909")] | ||
pub const MIN_POSITIVE: f128 = 3.36210314311209350626267781732175260e-4932_f128; | ||
/// Largest finite `f128` value. | ||
/// | ||
/// Equal to | ||
/// (1 − 2<sup>−[`MANTISSA_DIGITS`]</sup>) 2<sup>[`MAX_EXP`]</sup>. | ||
/// | ||
/// [`MANTISSA_DIGITS`]: f128::MANTISSA_DIGITS | ||
/// [`MAX_EXP`]: f128::MAX_EXP | ||
#[unstable(feature = "f128", issue = "116909")] | ||
pub const MAX: f128 = 1.18973149535723176508575932662800701e+4932_f128; |
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.
Minor style nit, would be good to consistently keep a line between the constant and docs (I know this was just copied from the existing floats)
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (fc47cf3): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 676.836s -> 676.942s (0.02%) |
f16
, excluding NaN and infinities as these are implemented using arithmetic forf32
andf64
.f128
, excluding NaN and infinities.std::f16::consts
.std::f128::consts
.