Skip to content

Fix {f16,f32,f64,f128}::div_euclid #133755

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions library/std/src/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,21 @@ impl f128 {
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn rem_euclid(self, rhs: f128) -> f128 {
let r = self % rhs;
if r < 0.0 { r + rhs.abs() } else { r }
if rhs.is_infinite() {
if self.is_infinite() || self.is_nan() {
return f128::NAN;
} else {
return self;
}
}
// FIXME(#133755): Though `self % rhs` is documented to be
// equivalent to this, it is not, and the distinction matters
// here.
let r = self - rhs * (self / rhs).trunc();
if r < 0.0 {
return if rhs > 0.0 { r + rhs } else { r - rhs };
}
r
Comment on lines +323 to +326
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: is there a reason this has a return instead of

Suggested change
if r < 0.0 {
return if rhs > 0.0 { r + rhs } else { r - rhs };
}
r
if r < 0.0 {
if rhs > 0.0 { r + rhs } else { r - rhs }
} else {
r
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For consistency (both stylistic and as it makes the mathematically analogy more clear), I followed the style of the existing div_euclid implementation:

    pub fn div_euclid(self, rhs: f64) -> f64 {
        let q = (self / rhs).trunc();
        if self % rhs < 0.0 {
            return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
        }
        q
    }

}

/// Raises a number to an integer power.
Expand Down
17 changes: 15 additions & 2 deletions library/std/src/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,21 @@ impl f16 {
#[unstable(feature = "f16", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn rem_euclid(self, rhs: f16) -> f16 {
let r = self % rhs;
if r < 0.0 { r + rhs.abs() } else { r }
if rhs.is_infinite() {
if self.is_infinite() || self.is_nan() {
return f16::NAN;
} else {
return self;
}
}
// FIXME(#133755): Though `self % rhs` is documented to be
// equivalent to this, it is not, and the distinction matters
// here.
let r = self - rhs * (self / rhs).trunc();
if r < 0.0 {
return if rhs > 0.0 { r + rhs } else { r - rhs };
}
r
}

/// Raises a number to an integer power.
Expand Down
17 changes: 15 additions & 2 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,21 @@ impl f32 {
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
pub fn rem_euclid(self, rhs: f32) -> f32 {
let r = self % rhs;
if r < 0.0 { r + rhs.abs() } else { r }
if rhs.is_infinite() {
if self.is_infinite() || self.is_nan() {
return f32::NAN;
} else {
return self;
}
}
// FIXME(#133755): Though `self % rhs` is documented to be
// equivalent to this, it is not, and the distinction matters
// here.
let r = self - rhs * (self / rhs).trunc();
if r < 0.0 {
return if rhs > 0.0 { r + rhs } else { r - rhs };
}
r
}

/// Raises a number to an integer power.
Expand Down
17 changes: 15 additions & 2 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,21 @@ impl f64 {
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
pub fn rem_euclid(self, rhs: f64) -> f64 {
let r = self % rhs;
if r < 0.0 { r + rhs.abs() } else { r }
if rhs.is_infinite() {
if self.is_infinite() || self.is_nan() {
return f64::NAN;
} else {
return self;
}
}
// FIXME(#133755): Though `self % rhs` is documented to be
// equivalent to this, it is not, and the distinction matters
// here.
let r = self - rhs * (self / rhs).trunc();
if r < 0.0 {
return if rhs > 0.0 { r + rhs } else { r - rhs };
}
r
}

/// Raises a number to an integer power.
Expand Down
Loading