Skip to content
Open
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
10 changes: 7 additions & 3 deletions include/boost/decimal/detail/cmath/cosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ constexpr auto cosh_impl(const T x) noexcept

result = fma(result, xsq, one);
}
else
else if (x > one)
{
const auto exp_pos_val = exp(x);

constexpr T two { 2, 0 };
result = (exp_pos_val + (one / exp_pos_val)) / 2;
}
else
{
constexpr T local_cosh_one { numbers::e_v<T> };

result = (exp_pos_val + (one / exp_pos_val)) / two;
result = (local_cosh_one + (one / local_cosh_one)) / 2;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions include/boost/decimal/detail/cmath/exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ constexpr auto exp_impl(T x) noexcept
{
result = one / exp(-x);
}
else if(x == one)
{
result = numbers::e_v<T>;
}
else
{
// Scale the argument to 0 < x < log(2).
Expand Down
30 changes: 23 additions & 7 deletions test/test_cosh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 -2024 Matt Borland
// Copyright 2023 -2024 Christopher Kormanyos
// Copyright 2023 -2026 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

Expand All @@ -22,8 +22,8 @@

#include <boost/core/lightweight_test.hpp>

auto my_zero() -> boost::decimal::decimal32_t&;
auto my_one () -> boost::decimal::decimal32_t&;
auto my_zero() -> boost::decimal::decimal32_t;
auto my_one () -> boost::decimal::decimal32_t;

namespace local
{
Expand Down Expand Up @@ -217,6 +217,22 @@ namespace local
result_is_ok = (result_val_zero_neg_is_ok && result_is_ok);
}

for(auto i = static_cast<unsigned>(UINT8_C(0)); i < static_cast<unsigned>(UINT8_C(4)); ++i)
{
static_cast<void>(i);

const auto val_cosh_one = cosh(::my_one());

// N[Cosh[1], 40]
constexpr decimal_type local_cosh_one { "1.543080634815243778477905620757061682602" };

const auto result_val_one_is_ok = local::is_close_fraction(val_cosh_one, local_cosh_one, std::numeric_limits<decimal_type>::epsilon() * 4);

BOOST_TEST(result_val_one_is_ok);

result_is_ok = (result_val_one_is_ok && result_is_ok);
}

return result_is_ok;
}

Expand Down Expand Up @@ -351,9 +367,9 @@ auto main() -> int

const auto result_edge_is_ok = local::test_cosh_edge();

const auto result_pos64_is_ok = local::test_cosh_64(64);
const auto result_pos64_is_ok = local::test_cosh_64(32);

const auto result_pos128_is_ok = local::test_cosh_128(400000);
const auto result_pos128_is_ok = local::test_cosh_128(32);

BOOST_TEST(result_pos_is_ok);
BOOST_TEST(result_neg_is_ok);
Expand Down Expand Up @@ -390,5 +406,5 @@ auto main() -> int
return (result_is_ok ? 0 : -1);
}

auto my_zero() -> boost::decimal::decimal32_t& { static boost::decimal::decimal32_t val_zero { 0, 0 }; return val_zero; }
auto my_one () -> boost::decimal::decimal32_t& { static boost::decimal::decimal32_t val_one { 1, 0 }; return val_one; }
auto my_zero() -> boost::decimal::decimal32_t { return boost::decimal::decimal32_t { 0 }; }
auto my_one () -> boost::decimal::decimal32_t { return boost::decimal::decimal32_t { 1 }; }
11 changes: 7 additions & 4 deletions test/test_exp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 - 2024 Matt Borland
// Copyright 2023 - 2024 Christopher Kormanyos
// Copyright 2023 - 2026 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

Expand All @@ -22,8 +22,8 @@

#include <boost/core/lightweight_test.hpp>

template<typename DecimalType> auto my_zero() -> DecimalType& { using decimal_type = DecimalType; static decimal_type val_zero { 0, 0 }; return val_zero; }
template<typename DecimalType> auto my_one () -> DecimalType& { using decimal_type = DecimalType; static decimal_type val_one { 1, 0 }; return val_one; }
template<typename DecimalType> auto my_zero() -> DecimalType;
template<typename DecimalType> auto my_one () -> DecimalType;

namespace local
{
Expand Down Expand Up @@ -381,7 +381,7 @@ auto main() -> int
}

{
const auto result_pos128_is_ok = local::test_exp_128(8192);
const auto result_pos128_is_ok = local::test_exp_128(256);

BOOST_TEST(result_pos128_is_ok);

Expand All @@ -392,3 +392,6 @@ auto main() -> int

return (result_is_ok ? 0 : -1);
}

template<typename DecimalType> auto my_zero() -> DecimalType { using decimal_type = DecimalType; return decimal_type { 0 }; }
template<typename DecimalType> auto my_one () -> DecimalType { using decimal_type = DecimalType; return decimal_type { 1 }; }
Loading