diff --git a/CHANGELOG.md b/CHANGELOG.md index d61050f66d2..63da9847023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ - PR #5822 Add `cudf_kafka` to `custreamz` run time conda dependency and fix bash syntax issue - PR #5845 Add support for `mask_to_bools` - PR #5851 Add support for `Index.sort_values` +- PR #5859 Add conversion form `fixed_point` to `bool` ## Improvements diff --git a/cpp/include/cudf/fixed_point/fixed_point.hpp b/cpp/include/cudf/fixed_point/fixed_point.hpp index 18a1c06672a..2ae6c277056 100644 --- a/cpp/include/cudf/fixed_point/fixed_point.hpp +++ b/cpp/include/cudf/fixed_point/fixed_point.hpp @@ -342,6 +342,16 @@ class fixed_point { return detail::shift(static_cast(_value), detail::negate(_scale)); } + /** + * @brief Explicit conversion operator to `bool` + * + * @return The `fixed_point` value as a boolean (zero is `false`, nonzero is `true`) + */ + CUDA_HOST_DEVICE_CALLABLE explicit constexpr operator bool() const + { + return static_cast(_value); + } + /** * @brief operator += * diff --git a/cpp/tests/fixed_point/fixed_point_tests.cu b/cpp/tests/fixed_point/fixed_point_tests.cu index cfd669f8246..3eb5c9131bf 100644 --- a/cpp/tests/fixed_point/fixed_point_tests.cu +++ b/cpp/tests/fixed_point/fixed_point_tests.cu @@ -335,6 +335,26 @@ TYPED_TEST(FixedPointTestBothReps, DecimalXXThrust) EXPECT_EQ(vec2, vec3); } +TYPED_TEST(FixedPointTestBothReps, BoolConversion) +{ + using decimalXX = fixed_point; + + decimalXX truthy_value{1.234567, scale_type{0}}; + decimalXX falsy_value{0, scale_type{0}}; + + // Test explicit conversions + EXPECT_EQ(static_cast(truthy_value), true); + EXPECT_EQ(static_cast(falsy_value), false); + + // These operators also *explicitly* convert to bool + EXPECT_EQ(truthy_value && true, true); + EXPECT_EQ(true && truthy_value, true); + EXPECT_EQ(falsy_value || false, false); + EXPECT_EQ(false || falsy_value, false); + EXPECT_EQ(!truthy_value, false); + EXPECT_EQ(!falsy_value, true); +} + TEST_F(FixedPointTest, OverflowDecimal32) { // This flag is needed to avoid warnings with ASSERT_DEATH