Skip to content

Move several float tests to floats/mod.rs #143738

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

rocurley
Copy link
Contributor

This PR moves several tests to floats/mod.rs, as discussed in #141726. The tests moved are:

  • test_num_f*
  • test_infinity
  • test_neg_infinity
  • test_zero
  • test_neg_zero
  • test_one
  • test_is_nan
  • test_is_infinite
  • test_is_finite
  • test_is_normal
  • test_classify

Each test is its own commit, so it may be easiest to review each commit individually.

r? tgross35

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 10, 2025
@@ -20,6 +19,31 @@ impl Approx for f128 {
const LIM: Self = 1e-9;
}

trait TestableFloat {
Copy link
Contributor Author

@rocurley rocurley Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly we should merge Approx into this?

Alternatively, there could be separate floats for each logical grouping of constants. I don't really like that because it seems like a lot of boilerplate, but it would mean the traits here more meaningful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving LIM to TestableFloat sounds good to me 👍 Maybe rename it APPROX there.

@rocurley rocurley marked this pull request as ready for review July 10, 2025 16:38
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 10, 2025
@@ -20,6 +19,31 @@ impl Approx for f128 {
const LIM: Self = 1e-9;
}

trait TestableFloat {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving LIM to TestableFloat sounds good to me 👍 Maybe rename it APPROX there.

Comment on lines 375 to 374
test<Float> {
let neg_zero: Float = -0.0;
assert!(0.0 == neg_zero);
assert_biteq!(-0.0, neg_zero);
assert!(!neg_zero.is_infinite());
assert!(neg_zero.is_finite());
assert!(!neg_zero.is_sign_positive());
assert!(neg_zero.is_sign_negative());
assert!(!neg_zero.is_nan());
assert!(!neg_zero.is_normal());
assert!(matches!(neg_zero.classify(), Fp::Zero));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indent this to match?

Comment on lines 27 to 33
impl TestableFloat for f16 {
const SMALL_NORMAL: Self = 1e-4;
const SUBNORMAL: Self = 1e-5;
}

impl TestableFloat for f32 {
const SMALL_NORMAL: Self = 1e-37;
const SUBNORMAL: Self = 1e-38;
}

impl TestableFloat for f64 {
const SMALL_NORMAL: Self = 1e-307;
const SUBNORMAL: Self = 1e-308;
}

impl TestableFloat for f128 {
const SMALL_NORMAL: Self = 1e-4931;
const SUBNORMAL: Self = 1e-4932;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename SMALL_NORMAL to MIN_POSITIVE_NORMAL? Then these can just be f32::MIN_POSITIVE rather than a handwritten value. You could also use MIN_POSITIVE directly, but that name is confusing.

SUBNORMAL can similarly be renamed to MAX_SUBNORMAL with a value f32::MIN_POSITIVE.next_down().

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 11, 2025
@rocurley
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 11, 2025
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of other small things.

After this, please clean up history by squashing the fixup commits. Or just squash everything together, if that's easier.

Comment on lines 407 to 409
assert!(!0.0f32.is_nan());
assert!(!5.3f32.is_nan());
assert!(!(-10.732f32).is_nan());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all f32 so you'll need to bind them to a variable of type Float

assert!(matches!(neg_inf.classify(), Fp::Infinite));
assert!(matches!(zero.classify(), Fp::Zero));
assert!(matches!(neg_zero.classify(), Fp::Zero));
assert!(matches!(1f32.classify(), Fp::Normal));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this isn't the right type.

If it makes things easier you could add ZERO, ONE, NEG_ZERO and NEG_ONE to the trait since those specific values are used in multiple functions.

@rocurley rocurley force-pushed the float_tests_refactor_2 branch from 261da66 to 79769f2 Compare July 11, 2025 14:41
@rocurley
Copy link
Contributor Author

Oof, thanks for catching those. Should be fixed now, and I've squashed the fixes into the appropriate original commits.

It seems like keeping the commit history clean is important here. I'm curious about the motivation: are the PR commits not squashed when merging into master?

@rustbot review

@tgross35
Copy link
Contributor

It seems like keeping the commit history clean is important here. I'm curious about the motivation: are the PR commits not squashed when merging into master?

They are not, the option would be nice but unfortunately Bors doesn't support it. Feel free to squash this if you get the chance before Bors picks it up since the commits are pretty tiny. No worries if you don't get to it since at least the history is clean.

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

📌 Commit 79769f2 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 14, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 14, 2025
…=tgross35

Move several float tests to floats/mod.rs

This PR moves several tests to `floats/mod.rs`, as discussed in rust-lang#141726. The tests moved are:

- `test_num_f*`
- `test_infinity`
- `test_neg_infinity`
- `test_zero`
- `test_neg_zero`
- `test_one`
- `test_is_nan`
- `test_is_infinite`
- `test_is_finite`
- `test_is_normal`
- `test_classify`

Each test is its own commit, so it may be easiest to review each commit individually.

r? tgross35
bors added a commit that referenced this pull request Jul 14, 2025
Rollup of 17 pull requests

Successful merges:

 - #142885 (core: Add `BorrowedCursor::with_unfilled_buf`)
 - #143217 (Port #[link_ordinal] to the new attribute parsing infrastructure)
 - #143355 (wrapping shift: remove first bitmask and table)
 - #143448 (remote-test-client: Exit code `128 + <signal-number>` instead of `3`)
 - #143681 (bootstrap/miri: avoid rebuilds for test builds)
 - #143710 (Updates to random number generation APIs)
 - #143724 (Tidy cleanup)
 - #143738 (Move several float tests to floats/mod.rs)
 - #143820 (Fixed a core crate compilation failure when enabling the `optimize_for_size` feature on some targets)
 - #143850 (Compiletest: Simplify {Html,Json}DocCk directive handling)
 - #143855 (Port `#[omit_gdb_pretty_printer_section]` to the new attribute parsing)
 - #143868 (warn on align on fields to avoid breaking changes)
 - #143875 (update issue number for `const_trait_impl`)
 - #143881 (Use zero for initialized Once state)
 - #143887 (Run bootstrap tests sooner in the `x test` pipeline)
 - #143893 (Don't require `eh_personality` lang item on targets that have a personality)
 - #143901 (Region constraint nits)

Failed merges:

 - #143878 (Port `#[pointee]` to the new attribute parsing infrastructure)
 - #143891 (Port `#[coverage]` to the new attribute system)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants