Skip to content
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

[discussion][donotmerge]: Copy Python implementation for float::div_euclid #133485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 26 additions & 5 deletions library/std/src/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,40 @@ impl f128 {
/// let b = 4.0;
/// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
/// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(a.div_euclid(-b), -2.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 1.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(11f128.div_euclid(1.1), 9.0);
/// assert_eq!((-11f128).div_euclid(1.1), -10.0);
/// assert_eq!(0.5f128.div_euclid(1.1), 0.0);
/// assert_eq!((-0.5f128).div_euclid(1.1), -1.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn div_euclid(self, rhs: f128) -> f128 {
let q = (self / rhs).trunc();
if self % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
// Copied from Python implementation: <https://github.com/python/cpython/blob/3.13/Objects/floatobject.c#L662>
// NOTE: Should we use `fmod` instead?
let rem = self % rhs;
let mut div = (self - rem) / rhs;
if rem != 0.0 {
/* ensure the remainder has the same sign as the denominator */
if (rhs < 0.0) != (rem < 0.0) {
div -= 1.0;
}
}
/* snap quotient to nearest integral value */
let q = if div != 0.0 {
let mut q = div.floor();
if div - q > 0.5 {
q += 1.0;
}
q
} else {
/* div is zero - get the same sign as the true quotient */
Self::copysign(0.0, self / rhs)
};
q
}

Expand Down
31 changes: 26 additions & 5 deletions library/std/src/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,40 @@ impl f16 {
/// let b = 4.0;
/// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
/// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(a.div_euclid(-b), -2.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 1.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(11f16.div_euclid(1.1), 9.0);
/// assert_eq!((-11f16).div_euclid(1.1), -10.0);
/// assert_eq!(0.5f16.div_euclid(1.1), 0.0);
/// assert_eq!(-0.5f16.div_euclid(1.1), -1.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f16", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn div_euclid(self, rhs: f16) -> f16 {
let q = (self / rhs).trunc();
if self % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
// Copied from Python implementation: <https://github.com/python/cpython/blob/3.13/Objects/floatobject.c#L662>
// NOTE: Should we use `fmod` instead?
let rem = self % rhs;
let mut div = (self - rem) / rhs;
if rem != 0.0 {
/* ensure the remainder has the same sign as the denominator */
if (rhs < 0.0) != (rem < 0.0) {
div -= 1.0;
}
}
/* snap quotient to nearest integral value */
let q = if div != 0.0 {
let mut q = div.floor();
if div - q > 0.5 {
q += 1.0;
}
q
} else {
/* div is zero - get the same sign as the true quotient */
Self::copysign(0.0, self / rhs)
};
q
}

Expand Down
31 changes: 26 additions & 5 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,39 @@ impl f32 {
/// let b = 4.0;
/// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
/// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(a.div_euclid(-b), -2.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 1.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(11f32.div_euclid(1.1), 9.0);
/// assert_eq!((-11f32).div_euclid(1.1), -10.0);
/// assert_eq!(0.5f32.div_euclid(1.1), 0.0);
/// assert_eq!((-0.5f32).div_euclid(1.1), -1.0);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
pub fn div_euclid(self, rhs: f32) -> f32 {
let q = (self / rhs).trunc();
if self % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
// Copied from Python implementation: <https://github.com/python/cpython/blob/3.13/Objects/floatobject.c#L662>
// NOTE: Should we use `fmod` instead?
let rem = self % rhs;
let mut div = (self - rem) / rhs;
if rem != 0.0 {
/* ensure the remainder has the same sign as the denominator */
if (rhs < 0.0) != (rem < 0.0) {
div -= 1.0;
}
}
/* snap quotient to nearest integral value */
let q = if div != 0.0 {
let mut q = div.floor();
if div - q > 0.5 {
q += 1.0;
}
q
} else {
/* div is zero - get the same sign as the true quotient */
Self::copysign(0.0, self / rhs)
};
q
}

Expand Down
31 changes: 26 additions & 5 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,39 @@ impl f64 {
/// let b = 4.0;
/// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
/// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(a.div_euclid(-b), -2.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 1.0); // -7.0 >= -4.0 * 2.0
/// assert_eq!(11f64.div_euclid(1.1), 9.0);
/// assert_eq!((-11f64).div_euclid(1.1), -10.0);
/// assert_eq!(0.5f64.div_euclid(1.1), 0.0);
/// assert_eq!((-0.5f64).div_euclid(1.1), -1.0);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
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 };
// Copied from Python implementation: <https://github.com/python/cpython/blob/3.13/Objects/floatobject.c#L662>
// NOTE: Should we use `fmod` instead?
let rem = self % rhs;
let mut div = (self - rem) / rhs;
if rem != 0.0 {
/* ensure the remainder has the same sign as the denominator */
if (rhs < 0.0) != (rem < 0.0) {
div -= 1.0;
}
}
/* snap quotient to nearest integral value */
let q = if div != 0.0 {
let mut q = div.floor();
if div - q > 0.5 {
q += 1.0;
}
q
} else {
/* div is zero - get the same sign as the true quotient */
Self::copysign(0.0, self / rhs)
};
q
}

Expand Down
Loading