Skip to content
Merged
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
16 changes: 16 additions & 0 deletions include/boost/decimal/detail/to_integral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ BOOST_DECIMAL_CUDA_CONSTEXPR auto to_integral(Decimal val) noexcept

constexpr Decimal max_target_type { (std::numeric_limits<TargetType>::max)() };
constexpr Decimal min_target_type { (std::numeric_limits<TargetType>::min)() };
constexpr Decimal one {1};

if (isnan(val))
{
Expand All @@ -49,6 +50,13 @@ BOOST_DECIMAL_CUDA_CONSTEXPR auto to_integral(Decimal val) noexcept
}
return static_cast<TargetType>(std::numeric_limits<TargetType>::max());
}

// Anything in [0, 1) should be flushed to 0
if (abs(val) < one)
{
return static_cast<TargetType>(0);
}

if (val > max_target_type || val < min_target_type)
{
#if defined(__clang__) && __clang_major__ >= 20
Expand Down Expand Up @@ -98,6 +106,7 @@ constexpr auto to_integral_128(Decimal val) noexcept
{
constexpr Decimal max_target_type { (std::numeric_limits<TargetType>::max)() };
constexpr Decimal min_target_type { (std::numeric_limits<TargetType>::min)() };
constexpr Decimal one {1};

if (isnan(val))
{
Expand All @@ -110,6 +119,13 @@ constexpr auto to_integral_128(Decimal val) noexcept

return static_cast<TargetType>(std::numeric_limits<TargetType>::max());
}

// Anything in [0, 1) should be flushed to 0
if (abs(val) < one)
{
return static_cast<TargetType>(0);
}

if (val > max_target_type || val < min_target_type)
{
#if defined(__clang__) && __clang_major__ >= 20
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ run github_issue_1306.cpp ;
run github_issue_1312.cpp ;
run github_issue_1319.cpp ;
run github_issue_1329.cpp ;
run github_issue_1361.cpp ;

run link_1.cpp link_2.cpp link_3.cpp ;
run quick.cpp ;
Expand Down
55 changes: 55 additions & 0 deletions test/github_issue_1361.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2026 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
//
// See: https://github.com/boostorg/decimal/issues/1319

#include <boost/decimal.hpp>
#include <boost/core/lightweight_test.hpp>
#include <limits>

using namespace boost::decimal;

#if !(defined(__clang__) && __clang_major__ < 9)

template <typename T>
void test()
{
constexpr T zero {0};
constexpr T numerator {1, -20};
constexpr T res {"3.1830988618379067153776752674502872406891929148091289749533e-21"}; // Wolfram
const auto val {numerator / numbers::pi_v<T>};
BOOST_TEST_NE(zero, val);
BOOST_TEST_EQ(val, res);
const auto res_as_unsigned {static_cast<unsigned>(res)};
BOOST_TEST_EQ(0U, res_as_unsigned);
BOOST_TEST_EQ(0U, static_cast<unsigned>(val));

constexpr T small_angle {1, -4};
const T sin_small_angle {sin(small_angle)};
// Approx: 0.00009999999983333334
BOOST_TEST_GE(small_angle, sin_small_angle);
BOOST_TEST_LE(small_angle / 10U, sin_small_angle);

constexpr T smaller_angle {1, -20};
BOOST_TEST_EQ(smaller_angle, sin(smaller_angle));
}

int main()
{
test<decimal32_t>();
test<decimal64_t>();
test<decimal128_t>();

test<decimal_fast32_t>();
test<decimal_fast64_t>();
test<decimal_fast128_t>();

return boost::report_errors();
}

#else

int main() { return 0; }

#endif
Loading