Skip to content

Commit eef734d

Browse files
authored
Merge pull request #1251 from cppalliance/1206
Add basic arithmetic and `<cmath>` examples
2 parents 90105f6 + ddb865d commit eef734d

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

doc/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* xref:basics.adoc[]
33
* xref:examples.adoc[]
44
** xref:examples.adoc#examples_construction[Basic Construction]
5+
** xref:examples.adoc#examples_basic_math[Basic Arithmetic]
56
** xref:examples.adoc#examples_promotion[Promotion and Mixed Decimal Arithmetic]
67
** xref:examples.adoc#examples_charconv[`<charconv>`]
78
** xref:examples.adoc#examples_generic_programming[Generic Programming]

doc/modules/ROOT/pages/examples.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ Can not construct from invalid string
3333
....
3434
====
3535
36+
[#examples_basic_math]
37+
== Basic Arithmetic
38+
39+
.This https://github.com/cppalliance/decimal/blob/develop/examples/basic_arithmetic.cpp[example] demonstrates the behavior and functionality of arithmetic using a single type
40+
====
41+
[source, c++]
42+
----
43+
include::example$basic_arithmetic.cpp[]
44+
----
45+
46+
.Expected Output:
47+
....
48+
A: -5.123456891234567
49+
B: 3.123456891234567
50+
A + B: -2
51+
abs(A + B): 2
52+
sqrt(abs(A + B)): 1.414213562373095
53+
Wolfram Alpha sqrt(2): 1.414213562373095
54+
....
55+
====
56+
3657
[#examples_promotion]
3758
== Promotion and Mixed Decimal Arithmetic
3859

examples/basic_arithmetic.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2024 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// This file demonstrates some of the basic numerical operations with the decimal types
6+
7+
#include <boost/decimal/decimal64_t.hpp> // For type decimal64_t
8+
#include <boost/decimal/cmath.hpp> // For decimal overloads of <cmath> functions
9+
#include <boost/decimal/iostream.hpp> // For decimal support of <iostream> and <iomanip>
10+
#include <iostream>
11+
#include <iomanip>
12+
13+
int main()
14+
{
15+
using boost::decimal::decimal64_t; // Type decimal64_t
16+
17+
constexpr decimal64_t a {"-5.123456891234567"}; // Constructs -5.123456 from string
18+
constexpr decimal64_t b {"3.123456891234567"}; // Constructs 3.123456 from string
19+
constexpr decimal64_t c {a + b};
20+
21+
// Here we can see that the result is exact
22+
constexpr decimal64_t neg_two {-2};
23+
static_assert(c == neg_two, "Result should be exact");
24+
25+
// We can use std::setprecision and std::numeric_limits in their usual ways
26+
std::cout << std::setprecision(std::numeric_limits<decimal64_t>::digits10)
27+
<< "A: " << a << '\n'
28+
<< "B: " << b << '\n'
29+
<< "A + B: " << c << '\n';
30+
31+
// The decimal library provides comprehensive implementations of the <cmath> functions
32+
// that you would expect to have with the builtin floating point types
33+
//
34+
// They are all located in namespace boost::decimal::,
35+
// as overloading namespace std is not allowed
36+
37+
constexpr decimal64_t abs_c {boost::decimal::abs(c)};
38+
std::cout << "abs(A + B): " << abs_c << '\n';
39+
40+
// All cmath functions are constexpr even if their std:: counterparts are not
41+
constexpr decimal64_t sqrt_two {boost::decimal::sqrt(abs_c)};
42+
43+
// Value compute by N[Sqrt[2], 50] using Wolfram Alpha or Mathematica
44+
constexpr decimal64_t wa_sqrt_two {"1.4142135623730950488016887242096980785696718753769"};
45+
std::cout << "sqrt(abs(A + B)): " << sqrt_two << '\n'
46+
<< "Wolfram Alpha sqrt(2): " << wa_sqrt_two << '\n';
47+
}

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ run ../examples/print.cpp ;
206206
run ../examples/promotion.cpp ;
207207
run ../examples/numerical_parsing.cpp ;
208208
run ../examples/first_example.cpp ;
209+
run ../examples/basic_arithmetic.cpp ;
209210

210211
# Test compilation of separate headers
211212
compile compile_tests/bid_conversion.cpp ;

0 commit comments

Comments
 (0)