Skip to content

Commit 96c7a44

Browse files
Alex-Velezscottmcm
andcommitted
Update src/test/assembly/x86_64-floating-point-clamp.rs
Simple Clamp Function I thought this was more robust and easier to read. I also allowed this function to return early in order to skip the extra bound check (I'm sure the difference is negligible). I'm not sure if there was a reason for binding `self` to `x`; if so, please correct me. Simple Clamp Function for f64 I thought this was more robust and easier to read. I also allowed this function to return early in order to skip the extra bound check (I'm sure the difference is negligible). I'm not sure if there was a reason for binding `self` to `x`; if so, please correct me. Floating point clamp test f32 clamp using mut self f64 clamp using mut self Update library/core/src/num/f32.rs Update f64.rs Update x86_64-floating-point-clamp.rs Update src/test/assembly/x86_64-floating-point-clamp.rs Update x86_64-floating-point-clamp.rs Co-Authored-By: scottmcm <scottmcm@users.noreply.github.com>
1 parent 2c39c88 commit 96c7a44

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

core/src/num/f32.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,15 +1282,14 @@ impl f32 {
12821282
#[must_use = "method returns a new number and does not mutate the original value"]
12831283
#[stable(feature = "clamp", since = "1.50.0")]
12841284
#[inline]
1285-
pub fn clamp(self, min: f32, max: f32) -> f32 {
1285+
pub fn clamp(mut self, min: f32, max: f32) -> f32 {
12861286
assert!(min <= max);
1287-
let mut x = self;
1288-
if x < min {
1289-
x = min;
1287+
if self < min {
1288+
self = min;
12901289
}
1291-
if x > max {
1292-
x = max;
1290+
if self > max {
1291+
self = max;
12931292
}
1294-
x
1293+
self
12951294
}
12961295
}

core/src/num/f64.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,15 +1280,14 @@ impl f64 {
12801280
#[must_use = "method returns a new number and does not mutate the original value"]
12811281
#[stable(feature = "clamp", since = "1.50.0")]
12821282
#[inline]
1283-
pub fn clamp(self, min: f64, max: f64) -> f64 {
1283+
pub fn clamp(mut self, min: f64, max: f64) -> f64 {
12841284
assert!(min <= max);
1285-
let mut x = self;
1286-
if x < min {
1287-
x = min;
1285+
if self < min {
1286+
self = min;
12881287
}
1289-
if x > max {
1290-
x = max;
1288+
if self > max {
1289+
self = max;
12911290
}
1292-
x
1291+
self
12931292
}
12941293
}

0 commit comments

Comments
 (0)