Skip to content

Commit 3afb4d8

Browse files
committed
Add 64-bit test set of addition and subtraction using downward round
1 parent 7a727a7 commit 3afb4d8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ run test_decimal64_fast_stream.cpp ;
128128
run test_decimal64_stream.cpp ;
129129
#run test_decimal128_basis.cpp ;
130130
run test_decimal_quantum.cpp ;
131+
run test_downward_rounding.cpp ;
131132
run test_dpd_conversions.cpp ;
132133
run test_edges_and_behave.cpp ;
133134
run test_edit_members.cpp ;

test/test_downward_rounding.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
// Use compile-time rounding mode change so the test run on all platforms
6+
#define BOOST_DECIMAL_FE_DEC_DOWNWARD
7+
8+
#include <boost/decimal.hpp>
9+
#include <boost/core/lightweight_test.hpp>
10+
#include <functional>
11+
#include <iostream>
12+
#include <iomanip>
13+
14+
template <typename T, typename Func>
15+
void test(const char* lhs_str, const char* rhs_str, const char* result_str, Func f)
16+
{
17+
const T lhs {lhs_str};
18+
const T rhs {rhs_str};
19+
const T result {result_str};
20+
21+
const T func_result {f(lhs, rhs)};
22+
23+
BOOST_TEST_EQ(func_result, result);
24+
}
25+
26+
template <typename T>
27+
void test_add(const char* lhs, const char* rhs, const char* result)
28+
{
29+
std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10);
30+
return test<T>(lhs, rhs, result, std::plus<>());
31+
}
32+
33+
template <typename T>
34+
void test_sub(const char* lhs, const char* rhs, const char* result)
35+
{
36+
std::cerr << std::setprecision(std::numeric_limits<T>::max_digits10);
37+
return test<T>(lhs, rhs, result, std::minus<>());
38+
}
39+
40+
int main()
41+
{
42+
using namespace boost::decimal;
43+
44+
test_add<decimal64_t>("1e+2", "-1e-383", "99.99999999999999");
45+
test_add<decimal64_t>("1e+1", "-1e-383", "9.999999999999999");
46+
47+
test_add<decimal_fast64_t>("1e+2", "-1e-383", "99.99999999999999");
48+
test_add<decimal_fast64_t>("1e+1", "-1e-383", "9.999999999999999");
49+
50+
test_sub<decimal64_t>("1e+2", "1e-383", "99.99999999999999");
51+
test_sub<decimal64_t>("1e+1", "1e-383", "9.999999999999999");
52+
53+
test_sub<decimal_fast64_t>("1e+2", "1e-383", "99.99999999999999");
54+
test_sub<decimal_fast64_t>("1e+1", "1e-383", "9.999999999999999");
55+
56+
return boost::report_errors();
57+
}

0 commit comments

Comments
 (0)