Skip to content

Commit

Permalink
Merge pull request #5859 from bdice/fixed_point-to-bool
Browse files Browse the repository at this point in the history
[REVIEW] Add explicit conversion for fixed_point to bool.
  • Loading branch information
jrhemstad authored Aug 6, 2020
2 parents 7e9aba7 + bfe17b4 commit c86d76c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions cpp/include/cudf/fixed_point/fixed_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ class fixed_point {
return detail::shift<Rep, Rad>(static_cast<U>(_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<bool>(_value);
}

/**
* @brief operator +=
*
Expand Down
20 changes: 20 additions & 0 deletions cpp/tests/fixed_point/fixed_point_tests.cu
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,26 @@ TYPED_TEST(FixedPointTestBothReps, DecimalXXThrust)
EXPECT_EQ(vec2, vec3);
}

TYPED_TEST(FixedPointTestBothReps, BoolConversion)
{
using decimalXX = fixed_point<TypeParam, Radix::BASE_10>;

decimalXX truthy_value{1.234567, scale_type{0}};
decimalXX falsy_value{0, scale_type{0}};

// Test explicit conversions
EXPECT_EQ(static_cast<bool>(truthy_value), true);
EXPECT_EQ(static_cast<bool>(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
Expand Down

0 comments on commit c86d76c

Please sign in to comment.