Skip to content

Commit 153d4ad

Browse files
authored
Merge pull request #343 from cppalliance/nan
Add cmath nan function
2 parents a1956a6 + 89e4065 commit 153d4ad

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

include/boost/decimal/cmath.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <boost/decimal/detail/cmath/tanh.hpp>
5353
#include <boost/decimal/detail/cmath/tgamma.hpp>
5454
#include <boost/decimal/detail/cmath/trunc.hpp>
55+
#include <boost/decimal/detail/cmath/nan.hpp>
5556
#include <boost/decimal/numbers.hpp>
5657

5758
// Macros from 3.6.2
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2023 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#ifndef BOOST_DECIMAL_DETAIL_CMATH_NAN_HPP
6+
#define BOOST_DECIMAL_DETAIL_CMATH_NAN_HPP
7+
8+
#include <boost/decimal/fwd.hpp>
9+
#include <boost/decimal/detail/config.hpp>
10+
#include <boost/decimal/detail/type_traits.hpp>
11+
#include <boost/decimal/cstdlib.hpp>
12+
#include <limits>
13+
14+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
15+
16+
namespace boost {
17+
namespace decimal {
18+
19+
namespace detail {
20+
21+
template <typename TargetDecimalType>
22+
constexpr auto nan_impl(const char* arg) noexcept -> TargetDecimalType
23+
{
24+
char* endptr {};
25+
const auto val {strtod_impl<TargetDecimalType>(arg, &endptr)};
26+
return val & std::numeric_limits<TargetDecimalType>::quiet_NaN();
27+
}
28+
29+
} //namespace detail
30+
31+
constexpr auto nand32(const char* arg) noexcept -> decimal32
32+
{
33+
return detail::nan_impl<decimal32>(arg);
34+
}
35+
36+
template <typename Dec>
37+
constexpr auto nan(const char* arg) noexcept -> Dec
38+
{
39+
return detail::nan_impl<Dec>(arg);
40+
}
41+
42+
constexpr auto nand64(const char* arg) noexcept -> decimal64
43+
{
44+
return detail::nan_impl<decimal64>(arg);
45+
}
46+
47+
constexpr auto nand128(const char* arg) noexcept -> decimal128
48+
{
49+
return detail::nan_impl<decimal128>(arg);
50+
}
51+
52+
} //namespace decimal
53+
} //namespace boost
54+
55+
#endif //#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
56+
57+
#endif //BOOST_DECIMAL_DETAIL_CMATH_NAN_HPP

test/test_cmath.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,16 @@ void test_exp2()
13401340
BOOST_TEST_EQ(exp2(-std::numeric_limits<T>::infinity()), T(0 * dist(rng)));
13411341
}
13421342

1343+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
1344+
template <typename T>
1345+
void test_nan()
1346+
{
1347+
BOOST_TEST(!isnan(nan<T>("1") & std::numeric_limits<T>::quiet_NaN()));
1348+
BOOST_TEST(!isnan(nan<T>("2") & std::numeric_limits<T>::quiet_NaN()));
1349+
BOOST_TEST(!isnan(nan<T>("-1") & std::numeric_limits<T>::quiet_NaN()));
1350+
}
1351+
#endif
1352+
13431353
int main()
13441354
{
13451355
test_fmax<decimal32>();
@@ -1448,5 +1458,11 @@ int main()
14481458
test_exp2<decimal32>();
14491459
test_exp2<decimal64>();
14501460

1461+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
1462+
test_nan<decimal32>();
1463+
test_nan<decimal64>();
1464+
test_nan<decimal128>();
1465+
#endif
1466+
14511467
return boost::report_errors();
14521468
}

0 commit comments

Comments
 (0)