Skip to content

Commit 75bf096

Browse files
authored
Merge pull request #1273 from cppalliance/examples
Add simple arithmetic differences example
2 parents fefc51c + 883b289 commit 75bf096

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

doc/modules/ROOT/pages/examples.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ Wolfram Alpha sqrt(2): 1.414213562373095
5454
....
5555
====
5656
57+
[#examples_math_results]
58+
.This https://github.com/cppalliance/decimal/blob/develop/examples/addition.cpp[example] shows the differences in arithmetic results between decimal floating point and binary floating point
59+
====
60+
[source,c++]
61+
----
62+
include::example$addition.cpp[]
63+
----
64+
65+
.Expected Output:
66+
....
67+
Decimal Result: 100
68+
Float Result: 99.999
69+
....
70+
====
71+
5772
[#examples_promotion]
5873
== Promotion and Mixed Decimal Arithmetic
5974

examples/addition.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// This example shows the difference in the results of repeated addition
6+
7+
#include <boost/decimal/decimal32_t.hpp> // For type decimal32_t and std::numeric_limits support
8+
#include <boost/decimal/iostream.hpp> // For decimal support to <iostream>
9+
#include <iostream>
10+
#include <limits>
11+
12+
int main()
13+
{
14+
using boost::decimal::decimal32_t;
15+
16+
constexpr decimal32_t decimal_one_tenth {"0.1"}; // Construct constant 0.1 from string for lossless conversion
17+
constexpr float float_one_tenth {0.1f}; // Construct floating point constant from literal
18+
19+
decimal32_t decimal_value {}; // Construct decimal 0 to start from
20+
float float_value {}; // Construct float 0 to start from
21+
22+
// We now add 0.1 1000 times which should result exactly in 100
23+
// What we actually find is that the decimal32_t value does result in exactly 100
24+
// With type float the result is not 100 due to inexact representation
25+
for (int i {}; i < 1000; ++i)
26+
{
27+
decimal_value += decimal_one_tenth; // Decimal types support compound arithmetic as expected
28+
float_value += float_one_tenth;
29+
}
30+
31+
// Each of the decimal types has complete support for std::numeric_limits,
32+
// which we leverage here with set precision to show any fractional part of the number (if applicable)
33+
std::cout << std::setprecision(std::numeric_limits<decimal32_t>::digits10)
34+
<< "Decimal Result: " << decimal_value << "\n"
35+
<< std::setprecision(std::numeric_limits<float>::digits10)
36+
<< " Float Result: " << float_value << std::endl;
37+
}

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ run ../examples/numerical_parsing.cpp ;
212212
run ../examples/first_example.cpp ;
213213
run ../examples/basic_arithmetic.cpp ;
214214
run ../examples/to_from_file.cpp ;
215+
run ../examples/addition.cpp ;
215216

216217
# Test compilation of separate headers
217218
compile compile_tests/bid_conversion.cpp ;

0 commit comments

Comments
 (0)