From a24beda0e3adc480b99b43ba472a32d1c341be94 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 23 Sep 2026 04:45:12 +0200 Subject: [PATCH] R-AI: pin render_f32's subnormal branch and implicit leading bit directly The first canonical_number mutation pass (previous commit) closed every survivor except four in render_f32 (biased_exponent == 0 flipped to !=, the subnormal exponent's -149 losing its sign, and the normal path's implicit-bit | flipped to ^/&). The existing round-trip tests parse the rendered string back through f32::parse and compare bits, which can still agree with a wrong bit-extraction when the wrong value happens to parse back to the bits it was carelessly derived from; none of them assert on the rendered string's shape or magnitude directly. Add a subnormal-shape test (2^-149 is "0." plus 149 fraction digits, not an astronomical integer and not one exponent off) and a normal-path test where the fraction bits are identical to zero's (1.0 and 2.0) so only the implicit leading bit the | folds in carries the value. Co-Authored-By: Claude Sonnet 5 --- packages/cultnet-rs/src/selection.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/cultnet-rs/src/selection.rs b/packages/cultnet-rs/src/selection.rs index b1b2d6d6..0fb282f1 100644 --- a/packages/cultnet-rs/src/selection.rs +++ b/packages/cultnet-rs/src/selection.rs @@ -442,6 +442,33 @@ pub mod canonical_number { assert_eq!(super::render_f64(-0.0_f64).unwrap(), "0"); } + // R-AI: cargo-mutants found `render_f32`'s subnormal-vs-normal branch survivable + // (`biased_exponent == 0` flipped to `!=`, and the subnormal exponent's `-149` losing its + // sign) even with the round-trip tests above - a round trip through a correctly-rounded + // parser can still agree with a wrong bit-extraction if the wrong value happens to parse + // back to the same bits it was carelessly derived from, and the MAX/subnormal round-trip + // tests above never assert on digit count or magnitude, only on parsing back correctly. + // This pins the *shape* of the subnormal case directly: 2^-149 is a fraction with 148 + // leading zeros after the point, not an astronomical integer (what a stray `+149` would + // render) and not a value one exponent off (what taking the normal branch for a + // biased_exponent of 0 would render). + #[test] + fn render_f32_subnormal_uses_the_subnormal_branch_and_its_negative_exponent() { + let rendered = super::render_f32(f32::from_bits(1)).unwrap(); + assert!(rendered.starts_with("0.00000"), "got {rendered:?}"); + assert_eq!(rendered.len(), 151, "2^-149 is \"0.\" plus 149 fraction digits, got {rendered:?}"); + } + + // R-AI: the normal-path implicit leading bit (`fraction | (1 << 23)`) is what distinguishes + // 1.0 from 0.0 in the mantissa - their fraction bits are identical (all zero); only the + // implicit bit the `|` folds in carries the value. Drop it (mutate `|` away, or take the + // subnormal branch instead) and both render as "0". + #[test] + fn render_f32_normal_path_folds_in_the_implicit_leading_bit() { + assert_eq!(super::render_f32(1.0_f32).unwrap(), "1"); + assert_eq!(super::render_f32(2.0_f32).unwrap(), "2"); + } + // R-AI/R-D: `is_canonical` is the Q-J door's grammar // (`^-?(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$`, minus the literal "-0") and was, until this test, // never called directly by any test in this crate - only reachable through