Skip to content

Commit 7f834b7

Browse files
committed
constify float::{div,rem}_euclid
1 parent a1d9cc4 commit 7f834b7

File tree

9 files changed

+37
-25
lines changed

9 files changed

+37
-25
lines changed

library/core/src/num/f128.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,9 @@ impl f128 {
17091709
#[inline]
17101710
#[rustc_allow_incoherent_impl]
17111711
#[unstable(feature = "f128", issue = "116909")]
1712+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
17121713
#[must_use = "method returns a new number and does not mutate the original value"]
1713-
pub fn div_euclid(self, rhs: f128) -> f128 {
1714+
pub const fn div_euclid(self, rhs: f128) -> f128 {
17141715
let q = (self / rhs).trunc();
17151716
if self % rhs < 0.0 {
17161717
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
@@ -1758,7 +1759,8 @@ impl f128 {
17581759
#[doc(alias = "modulo", alias = "mod")]
17591760
#[unstable(feature = "f128", issue = "116909")]
17601761
#[must_use = "method returns a new number and does not mutate the original value"]
1761-
pub fn rem_euclid(self, rhs: f128) -> f128 {
1762+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
1763+
pub const fn rem_euclid(self, rhs: f128) -> f128 {
17621764
let r = self % rhs;
17631765
if r < 0.0 { r + rhs.abs() } else { r }
17641766
}

library/core/src/num/f16.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,8 +1685,9 @@ impl f16 {
16851685
#[inline]
16861686
#[rustc_allow_incoherent_impl]
16871687
#[unstable(feature = "f16", issue = "116909")]
1688+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
16881689
#[must_use = "method returns a new number and does not mutate the original value"]
1689-
pub fn div_euclid(self, rhs: f16) -> f16 {
1690+
pub const fn div_euclid(self, rhs: f16) -> f16 {
16901691
let q = (self / rhs).trunc();
16911692
if self % rhs < 0.0 {
16921693
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
@@ -1733,8 +1734,9 @@ impl f16 {
17331734
#[rustc_allow_incoherent_impl]
17341735
#[doc(alias = "modulo", alias = "mod")]
17351736
#[unstable(feature = "f16", issue = "116909")]
1737+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
17361738
#[must_use = "method returns a new number and does not mutate the original value"]
1737-
pub fn rem_euclid(self, rhs: f16) -> f16 {
1739+
pub const fn rem_euclid(self, rhs: f16) -> f16 {
17381740
let r = self % rhs;
17391741
if r < 0.0 { r + rhs.abs() } else { r }
17401742
}

library/core/src/num/f32.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,8 +1826,9 @@ pub mod math {
18261826
/// [`f32::div_euclid`]: ../../../std/primitive.f32.html#method.div_euclid
18271827
#[inline]
18281828
#[unstable(feature = "core_float_math", issue = "137578")]
1829+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
18291830
#[must_use = "method returns a new number and does not mutate the original value"]
1830-
pub fn div_euclid(x: f32, rhs: f32) -> f32 {
1831+
pub const fn div_euclid(x: f32, rhs: f32) -> f32 {
18311832
let q = trunc(x / rhs);
18321833
if x % rhs < 0.0 {
18331834
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
@@ -1862,7 +1863,7 @@ pub mod math {
18621863
#[doc(alias = "modulo", alias = "mod")]
18631864
#[unstable(feature = "core_float_math", issue = "137578")]
18641865
#[must_use = "method returns a new number and does not mutate the original value"]
1865-
pub fn rem_euclid(x: f32, rhs: f32) -> f32 {
1866+
pub const fn rem_euclid(x: f32, rhs: f32) -> f32 {
18661867
let r = x % rhs;
18671868
if r < 0.0 { r + rhs.abs() } else { r }
18681869
}

library/core/src/num/f64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,8 +1825,9 @@ pub mod math {
18251825
/// [`f64::div_euclid`]: ../../../std/primitive.f64.html#method.div_euclid
18261826
#[inline]
18271827
#[unstable(feature = "core_float_math", issue = "137578")]
1828+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
18281829
#[must_use = "method returns a new number and does not mutate the original value"]
1829-
pub fn div_euclid(x: f64, rhs: f64) -> f64 {
1830+
pub const fn div_euclid(x: f64, rhs: f64) -> f64 {
18301831
let q = trunc(x / rhs);
18311832
if x % rhs < 0.0 {
18321833
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
@@ -1861,7 +1862,7 @@ pub mod math {
18611862
#[doc(alias = "modulo", alias = "mod")]
18621863
#[unstable(feature = "core_float_math", issue = "137578")]
18631864
#[must_use = "method returns a new number and does not mutate the original value"]
1864-
pub fn rem_euclid(x: f64, rhs: f64) -> f64 {
1865+
pub const fn rem_euclid(x: f64, rhs: f64) -> f64 {
18651866
let r = x % rhs;
18661867
if r < 0.0 { r + rhs.abs() } else { r }
18671868
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![feature(char_max_len)]
1717
#![feature(clone_to_uninit)]
1818
#![feature(const_eval_select)]
19+
#![feature(const_float_euclidean_division)]
1920
#![feature(const_float_round_methods)]
2021
#![feature(const_trait_impl)]
2122
#![feature(core_float_math)]

library/coretests/tests/num/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -932,23 +932,23 @@ macro_rules! test_float {
932932
$fassert!(<$fty>::NEG_INFINITY.copysign(0.0), <$fty>::INFINITY);
933933
}
934934
#[test]
935+
#[cfg(not(bootstrap))]
935936
fn rem_euclid() {
936-
// FIXME: Use $fassert when rem_euclid becomes const
937-
assert!(<$fty>::INFINITY.rem_euclid((42.0 as $fty)).is_nan());
938-
assert_eq!((42.0 as $fty).rem_euclid(<$fty>::INFINITY), (42.0 as $fty));
939-
assert!((42.0 as $fty).rem_euclid(<$fty>::NAN).is_nan());
940-
assert!(<$fty>::INFINITY.rem_euclid(<$fty>::INFINITY).is_nan());
941-
assert!(<$fty>::INFINITY.rem_euclid(<$fty>::NAN).is_nan());
942-
assert!(<$fty>::NAN.rem_euclid(<$fty>::INFINITY).is_nan());
937+
$fassert!(<$fty>::INFINITY.rem_euclid((42.0 as $fty)).is_nan());
938+
$fassert!((42.0 as $fty).rem_euclid(<$fty>::INFINITY), (42.0 as $fty));
939+
$fassert!((42.0 as $fty).rem_euclid(<$fty>::NAN).is_nan());
940+
$fassert!(<$fty>::INFINITY.rem_euclid(<$fty>::INFINITY).is_nan());
941+
$fassert!(<$fty>::INFINITY.rem_euclid(<$fty>::NAN).is_nan());
942+
$fassert!(<$fty>::NAN.rem_euclid(<$fty>::INFINITY).is_nan());
943943
}
944944
#[test]
945+
#[cfg(not(bootstrap))]
945946
fn div_euclid() {
946-
// FIXME: Use $fassert when div_euclid becomes const
947-
assert_eq!((42.0 as $fty).div_euclid(<$fty>::INFINITY), 0.0);
948-
assert!((42.0 as $fty).div_euclid(<$fty>::NAN).is_nan());
949-
assert!(<$fty>::INFINITY.div_euclid(<$fty>::INFINITY).is_nan());
950-
assert!(<$fty>::INFINITY.div_euclid(<$fty>::NAN).is_nan());
951-
assert!(<$fty>::NAN.div_euclid(<$fty>::INFINITY).is_nan());
947+
$fassert!((42.0 as $fty).div_euclid(<$fty>::INFINITY), 0.0);
948+
$fassert!((42.0 as $fty).div_euclid(<$fty>::NAN).is_nan());
949+
$fassert!(<$fty>::INFINITY.div_euclid(<$fty>::INFINITY).is_nan());
950+
$fassert!(<$fty>::INFINITY.div_euclid(<$fty>::NAN).is_nan());
951+
$fassert!(<$fty>::NAN.div_euclid(<$fty>::INFINITY).is_nan());
952952
}
953953
#[test]
954954
#[cfg(not(bootstrap))]

library/std/src/f32.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ impl f32 {
247247
#[must_use = "method returns a new number and does not mutate the original value"]
248248
#[inline]
249249
#[stable(feature = "euclidean_division", since = "1.38.0")]
250-
pub fn div_euclid(self, rhs: f32) -> f32 {
250+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
251+
pub const fn div_euclid(self, rhs: f32) -> f32 {
251252
core::f32::math::div_euclid(self, rhs)
252253
}
253254

@@ -284,7 +285,8 @@ impl f32 {
284285
#[must_use = "method returns a new number and does not mutate the original value"]
285286
#[inline]
286287
#[stable(feature = "euclidean_division", since = "1.38.0")]
287-
pub fn rem_euclid(self, rhs: f32) -> f32 {
288+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
289+
pub const fn rem_euclid(self, rhs: f32) -> f32 {
288290
core::f32::math::rem_euclid(self, rhs)
289291
}
290292

library/std/src/f64.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ impl f64 {
247247
#[must_use = "method returns a new number and does not mutate the original value"]
248248
#[inline]
249249
#[stable(feature = "euclidean_division", since = "1.38.0")]
250-
pub fn div_euclid(self, rhs: f64) -> f64 {
250+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
251+
pub const fn div_euclid(self, rhs: f64) -> f64 {
251252
core::f64::math::div_euclid(self, rhs)
252253
}
253254

@@ -284,7 +285,8 @@ impl f64 {
284285
#[must_use = "method returns a new number and does not mutate the original value"]
285286
#[inline]
286287
#[stable(feature = "euclidean_division", since = "1.38.0")]
287-
pub fn rem_euclid(self, rhs: f64) -> f64 {
288+
#[rustc_const_unstable(feature = "const_float_euclidean_division", issue = "141572")]
289+
pub const fn rem_euclid(self, rhs: f64) -> f64 {
288290
core::f64::math::rem_euclid(self, rhs)
289291
}
290292

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@
335335
#![feature(bstr_internals)]
336336
#![feature(char_internals)]
337337
#![feature(clone_to_uninit)]
338+
#![feature(const_float_euclidean_division)]
338339
#![feature(const_float_round_methods)]
339340
#![feature(core_intrinsics)]
340341
#![feature(core_io_borrowed_buf)]

0 commit comments

Comments
 (0)