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
13 changes: 11 additions & 2 deletions include/boost/decimal/decimal128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@
}
else
{
constexpr auto zero_bits {detail::uint128{UINT64_C(0x2208000000000000), UINT64_C(0)}};

// The value is probably infinity

// If we can offset some extra power in the coefficient try to do so
Expand All @@ -900,7 +902,7 @@
{
if (exp < 0)
{
*this = decimal128(0, 0, isneg);
bits_ = zero_bits;

Check warning on line 905 in include/boost/decimal/decimal128.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/decimal/decimal128.hpp#L905

Added line #L905 was not covered by tests
}
else
{
Expand All @@ -910,7 +912,14 @@
}
else
{
bits_ = detail::d128_comb_inf_mask;
if (exp < 0)
{
bits_ = zero_bits;
}
else
{
bits_ = detail::d128_comb_inf_mask;
}
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#ifndef BOOST_DECIMAL_DECIMAL128_FAST_HPP
#define BOOST_DECIMAL_DECIMAL128_FAST_HPP

#include <boost/decimal/decimal128.hpp>
#include <boost/decimal/detail/apply_sign.hpp>
#include <boost/decimal/detail/type_traits.hpp>
Expand All @@ -22,9 +25,6 @@

#endif

#ifndef BOOST_DECIMAL_DECIMAL128_FAST_HPP
#define BOOST_DECIMAL_DECIMAL128_FAST_HPP

namespace boost {
namespace decimal {

Expand Down Expand Up @@ -394,15 +394,22 @@ constexpr decimal128_fast::decimal128_fast(T1 coeff, T2 exp, bool sign) noexcept
exp = 0;
}

const auto biased_exp {static_cast<exponent_type>(exp + detail::bias_v<decimal128>)};
const auto biased_exp {exp + detail::bias_v<decimal128>};

if (biased_exp > detail::max_biased_exp_v<decimal128>)
{
significand_ = detail::d128_fast_inf;
}
else if (biased_exp >= 0)
{
exponent_ = static_cast<exponent_type>(biased_exp);
}
else
{
exponent_ = biased_exp;
// Flush denorms to zero
significand_ = static_cast<significand_type>(0);
exponent_ = static_cast<exponent_type>(0 + detail::bias_v<decimal128>);
sign_ = false;
}
}

Expand Down
13 changes: 11 additions & 2 deletions include/boost/decimal/decimal32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@
}
else
{
constexpr auto zero_bits {UINT32_C(0x22500000)};
// The value is probably infinity

// If we can offset some extra power in the coefficient try to do so
Expand All @@ -770,7 +771,7 @@
{
if (exp < 0)
{
*this = decimal32(0, 0, isneg);
bits_ = zero_bits;

Check warning on line 774 in include/boost/decimal/decimal32.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/decimal/decimal32.hpp#L774

Added line #L774 was not covered by tests
}
else
{
Expand All @@ -780,7 +781,15 @@
}
else
{
bits_ = detail::d32_comb_inf_mask;
if (exp < 0)
{
// Normalized zero
bits_ = zero_bits;
}
else
{
bits_ = detail::d32_comb_inf_mask;
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions include/boost/decimal/decimal32_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,24 @@ constexpr decimal32_fast::decimal32_fast(T1 coeff, T2 exp, bool sign) noexcept
exp = 0;
}

auto biased_exp {static_cast<std::uint_fast32_t>(exp + detail::bias)};
const auto biased_exp {exp + detail::bias};

// Decimal32 exponent holds 8 bits
if (biased_exp > detail::max_biased_exp_v<decimal32_fast>)
{
significand_ = detail::d32_fast_inf;
}
else
else if (biased_exp >= 0)
{
exponent_ = static_cast<exponent_type>(biased_exp);
}
else
{
// Flush denorms to zero
significand_ = static_cast<significand_type>(0);
exponent_ = static_cast<exponent_type>(101);
sign_ = false;
}
}

template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
Expand Down
13 changes: 11 additions & 2 deletions include/boost/decimal/decimal64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@
}
else
{
constexpr auto zero_bits {UINT64_C(0x2238000000000000)};

// The value is probably infinity

// If we can offset some extra power in the coefficient try to do so
Expand All @@ -763,7 +765,7 @@
{
if (exp < 0)
{
*this = decimal64(0, 0, isneg);
bits_ = zero_bits;

Check warning on line 768 in include/boost/decimal/decimal64.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/decimal/decimal64.hpp#L768

Added line #L768 was not covered by tests
}
else
{
Expand All @@ -773,7 +775,14 @@
}
else
{
bits_ = detail::d64_comb_inf_mask;
if (exp < 0)
{
bits_ = zero_bits;
}
else
{
bits_ = detail::d64_comb_inf_mask;
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions include/boost/decimal/decimal64_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,23 @@ constexpr decimal64_fast::decimal64_fast(T1 coeff, T2 exp, bool sign) noexcept
exp = 0;
}

const auto biased_exp {static_cast<std::uint_fast32_t>(exp + detail::bias_v<decimal64>)};
const auto biased_exp {exp + detail::bias_v<decimal64>};

if (biased_exp > detail::max_biased_exp_v<decimal64>)
{
significand_ = detail::d64_fast_inf;
}
else
else if (biased_exp >= 0)
{
exponent_ = static_cast<exponent_type>(biased_exp);
}
else
{
// Flush denorms to zero
significand_ = static_cast<significand_type>(0);
exponent_ = static_cast<exponent_type>(0 + detail::bias_v<decimal64>);
sign_ = false;
}
}

#ifdef BOOST_DECIMAL_HAS_CONCEPTS
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ run github_issue_798.cpp ;
run github_issue_799.cpp ;
run github_issue_802.cpp ;
run github_issue_805.cpp ;
run github_issue_808.cpp ;
run link_1.cpp link_2.cpp link_3.cpp ;
run quick.cpp ;
run random_decimal32_comp.cpp ;
Expand Down
12 changes: 10 additions & 2 deletions test/github_issue_798.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
#include <boost/decimal/detail/cmath/next.hpp>
#include <iomanip>
#include <limits>
#include <random>

template <typename T>
void test_zero()
{
constexpr T zero {0};
constexpr T one {1};
std::mt19937_64 rng(42);
std::uniform_int_distribution<int> dist(1, 1);

const T zero {0};
const T one {dist(rng)};

const auto next_after_zero {boost::decimal::nextafter(zero, one)};

BOOST_TEST_GT(next_after_zero, zero);
BOOST_TEST_LT(next_after_zero, zero + 2*std::numeric_limits<T>::min());

const auto two_next_after_zero {boost::decimal::nextafter(next_after_zero, one)};
BOOST_TEST_GT(two_next_after_zero, next_after_zero);
BOOST_TEST_LT(two_next_after_zero, zero + 3*std::numeric_limits<T>::min());
}

template <typename T>
Expand Down
31 changes: 31 additions & 0 deletions test/github_issue_808.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

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

using namespace boost::decimal;

template <typename Dec>
void tiny_div()
{
constexpr Dec zero {0};

const auto tiny = std::numeric_limits<Dec>::denorm_min();
const auto tiny2 = tiny / Dec{10};
BOOST_TEST(tiny2 == zero);
}

int main()
{
tiny_div<decimal32>();
tiny_div<decimal64>();
tiny_div<decimal128>();

tiny_div<decimal32_fast>();
tiny_div<decimal64_fast>();
tiny_div<decimal128_fast>();

return boost::report_errors();
}
Loading