From bc17bcdef005f427c74a39161dcfd1cd5d3b1a3c Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 5 Sep 2025 02:01:18 -0400 Subject: [PATCH] Simplify `{f16, f32, f54, f128}::midpoint()` `(float_ty::MAX / 2) - (float_ty::MIN_POSITIVE * 2)` equals `(float_ty::MAX / 2) + (float_ty::MIN_POSITIVE * 2)` equals `(float_ty::MAX / 2)`. So these branches are pointless --- library/core/src/num/f128.rs | 8 -------- library/core/src/num/f16.rs | 8 -------- library/core/src/num/f32.rs | 8 -------- library/core/src/num/f64.rs | 8 -------- 4 files changed, 32 deletions(-) diff --git a/library/core/src/num/f128.rs b/library/core/src/num/f128.rs index 4282b1af9f206..66c892aadd083 100644 --- a/library/core/src/num/f128.rs +++ b/library/core/src/num/f128.rs @@ -832,7 +832,6 @@ impl f128 { #[unstable(feature = "f128", issue = "116909")] #[rustc_const_unstable(feature = "f128", issue = "116909")] pub const fn midpoint(self, other: f128) -> f128 { - const LO: f128 = f128::MIN_POSITIVE * 2.; const HI: f128 = f128::MAX / 2.; let (a, b) = (self, other); @@ -842,14 +841,7 @@ impl f128 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible (a + b) / 2. - } else if abs_a < LO { - // Not safe to halve `a` (would underflow) - a + (b / 2.) - } else if abs_b < LO { - // Not safe to halve `b` (would underflow) - (a / 2.) + b } else { - // Safe to halve `a` and `b` (a / 2.) + (b / 2.) } } diff --git a/library/core/src/num/f16.rs b/library/core/src/num/f16.rs index 23a8661c14b7a..81220065e72a6 100644 --- a/library/core/src/num/f16.rs +++ b/library/core/src/num/f16.rs @@ -820,7 +820,6 @@ impl f16 { #[unstable(feature = "f16", issue = "116909")] #[rustc_const_unstable(feature = "f16", issue = "116909")] pub const fn midpoint(self, other: f16) -> f16 { - const LO: f16 = f16::MIN_POSITIVE * 2.; const HI: f16 = f16::MAX / 2.; let (a, b) = (self, other); @@ -830,14 +829,7 @@ impl f16 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible (a + b) / 2. - } else if abs_a < LO { - // Not safe to halve `a` (would underflow) - a + (b / 2.) - } else if abs_b < LO { - // Not safe to halve `b` (would underflow) - (a / 2.) + b } else { - // Safe to halve `a` and `b` (a / 2.) + (b / 2.) } } diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index da08bfc595059..4bb0db58fa585 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -1025,7 +1025,6 @@ impl f32 { ((self as f64 + other as f64) / 2.0) as f32 } _ => { - const LO: f32 = f32::MIN_POSITIVE * 2.; const HI: f32 = f32::MAX / 2.; let (a, b) = (self, other); @@ -1035,14 +1034,7 @@ impl f32 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible (a + b) / 2. - } else if abs_a < LO { - // Not safe to halve `a` (would underflow) - a + (b / 2.) - } else if abs_b < LO { - // Not safe to halve `b` (would underflow) - (a / 2.) + b } else { - // Safe to halve `a` and `b` (a / 2.) + (b / 2.) } } diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index dc0977cb1474c..9dd1141e70331 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -1026,7 +1026,6 @@ impl f64 { #[stable(feature = "num_midpoint", since = "1.85.0")] #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")] pub const fn midpoint(self, other: f64) -> f64 { - const LO: f64 = f64::MIN_POSITIVE * 2.; const HI: f64 = f64::MAX / 2.; let (a, b) = (self, other); @@ -1036,14 +1035,7 @@ impl f64 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible (a + b) / 2. - } else if abs_a < LO { - // Not safe to halve `a` (would underflow) - a + (b / 2.) - } else if abs_b < LO { - // Not safe to halve `b` (would underflow) - (a / 2.) + b } else { - // Safe to halve `a` and `b` (a / 2.) + (b / 2.) } }