File tree 3 files changed +19
-5
lines changed
3 files changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -936,12 +936,20 @@ sus_pure inline _self to_degrees() const& noexcept {
936
936
sus_pure inline _self to_radians () const & noexcept {
937
937
return primitive_value * (consts::PI.primitive_value / _primitive{180 });
938
938
}
939
- // / Rounds toward zero and converts to any primitive integer type, assuming
940
- // / that the value is finite and fits in that type.
941
- template <Integer I>
942
- sus_pure constexpr inline I to_int_unchecked (
939
+
940
+ // / Rounds toward zero and converts to any [safe integer type]($sus::num)
941
+ // / assuming that the value is finite and fits in that type.
942
+ // /
943
+ // / # Safety
944
+ // / To avoid Undefined Behaviour, the value must:
945
+ // / * Not be `NaN`.
946
+ // / * Not be infinite.
947
+ // / * Be representable in the return type `Int`, after truncating off its
948
+ // / fractional part.
949
+ template <Integer Int>
950
+ sus_pure constexpr inline Int to_int_unchecked (
943
951
::sus::marker::UnsafeFnMarker) const & noexcept {
944
- return static_cast <decltype (I ::primitive_value)>(primitive_value);
952
+ return static_cast <decltype (Int ::primitive_value)>(primitive_value);
945
953
}
946
954
947
955
// / Raw transmutation from `##_unsigned##`.
Original file line number Diff line number Diff line change @@ -1007,10 +1007,13 @@ TEST(f32, ToRadians) {
1007
1007
1008
1008
TEST (f32, ToIntUnchecked) {
1009
1009
auto a = (198 .054321_f32).to_int_unchecked <u8>(unsafe_fn);
1010
+ static_assert (std::same_as<decltype (a), u8>);
1010
1011
EXPECT_EQ (a, 198_u8);
1011
1012
auto b = (198 .054321_f32).to_int_unchecked <u32>(unsafe_fn);
1013
+ static_assert (std::same_as<decltype (b), u32>);
1012
1014
EXPECT_EQ (b, 198_u32);
1013
1015
auto c = (-108 .054321_f32).to_int_unchecked <i8>(unsafe_fn);
1016
+ static_assert (std::same_as<decltype (c), i8>);
1014
1017
EXPECT_EQ (c, -108_i8);
1015
1018
}
1016
1019
Original file line number Diff line number Diff line change @@ -1015,10 +1015,13 @@ TEST(f64, ToRadians) {
1015
1015
1016
1016
TEST (f64, ToIntUnchecked) {
1017
1017
auto a = (198 .054321_f64).to_int_unchecked <u8>(unsafe_fn);
1018
+ static_assert (std::same_as<decltype (a), u8>);
1018
1019
EXPECT_EQ (a, 198_u8);
1019
1020
auto b = (198 .054321_f64).to_int_unchecked <u32>(unsafe_fn);
1021
+ static_assert (std::same_as<decltype (b), u32>);
1020
1022
EXPECT_EQ (b, 198_u32);
1021
1023
auto c = (-108 .054321_f64).to_int_unchecked <i8>(unsafe_fn);
1024
+ static_assert (std::same_as<decltype (c), i8>);
1022
1025
EXPECT_EQ (c, -108_i8);
1023
1026
}
1024
1027
You can’t perform that action at this time.
0 commit comments