From 267796532f8d0fa60207dbc3d4aabeebd3b58d0c Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Sat, 16 May 2026 19:45:23 +0200 Subject: [PATCH] Change division to multiplication in floating-point midpoint --- library/core/src/num/f128.rs | 6 +++--- library/core/src/num/f16.rs | 6 +++--- library/core/src/num/f32.rs | 8 ++++---- library/core/src/num/f64.rs | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/library/core/src/num/f128.rs b/library/core/src/num/f128.rs index c17f55a25896a..d381402b469f4 100644 --- a/library/core/src/num/f128.rs +++ b/library/core/src/num/f128.rs @@ -977,7 +977,7 @@ impl f128 { #[must_use = "this returns the result of the operation, \ without modifying the original"] pub const fn midpoint(self, other: f128) -> f128 { - const HI: f128 = f128::MAX / 2.; + const HI: f128 = f128::MAX * 0.5; let (a, b) = (self, other); let abs_a = a.abs(); @@ -985,9 +985,9 @@ impl f128 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible - (a + b) / 2. + (a + b) * 0.5 } else { - (a / 2.) + (b / 2.) + (a * 0.5) + (b * 0.5) } } diff --git a/library/core/src/num/f16.rs b/library/core/src/num/f16.rs index 110465068b8f6..c26ae17d870cc 100644 --- a/library/core/src/num/f16.rs +++ b/library/core/src/num/f16.rs @@ -973,7 +973,7 @@ impl f16 { #[must_use = "this returns the result of the operation, \ without modifying the original"] pub const fn midpoint(self, other: f16) -> f16 { - const HI: f16 = f16::MAX / 2.; + const HI: f16 = f16::MAX * 0.5; let (a, b) = (self, other); let abs_a = a.abs(); @@ -981,9 +981,9 @@ impl f16 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible - (a + b) / 2. + (a + b) * 0.5 } else { - (a / 2.) + (b / 2.) + (a * 0.5) + (b * 0.5) } } diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index f9cb7cc650f4f..24c97a6491c11 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -1164,10 +1164,10 @@ impl f32 { target_arch = "wasm32", target_arch = "wasm64", ) => { - ((self as f64 + other as f64) / 2.0) as f32 + ((self as f64 + other as f64) * 0.5) as f32 } _ => { - const HI: f32 = f32::MAX / 2.; + const HI: f32 = f32::MAX * 0.5; let (a, b) = (self, other); let abs_a = a.abs(); @@ -1175,9 +1175,9 @@ impl f32 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible - (a + b) / 2. + (a + b) * 0.5 } else { - (a / 2.) + (b / 2.) + (a * 0.5) + (b * 0.5) } } } diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index 87f5505ce2b33..be045033a3553 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -1150,7 +1150,7 @@ impl f64 { #[must_use = "this returns the result of the operation, \ without modifying the original"] pub const fn midpoint(self, other: f64) -> f64 { - const HI: f64 = f64::MAX / 2.; + const HI: f64 = f64::MAX * 0.5; let (a, b) = (self, other); let abs_a = a.abs(); @@ -1158,9 +1158,9 @@ impl f64 { if abs_a <= HI && abs_b <= HI { // Overflow is impossible - (a + b) / 2. + (a + b) * 0.5 } else { - (a / 2.) + (b / 2.) + (a * 0.5) + (b * 0.5) } }