Skip to content

Commit dc95654

Browse files
authored
Merge pull request #1090 from cppalliance/1088
Add notes to basics section on underflow, overflow, and non-finite values handling
2 parents 460fde2 + 4a25581 commit dc95654

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

doc/modules/ROOT/pages/basics.adoc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ This is designed to reduce confusion (e.g. what would be the resulting sign of `
2828

2929
[souce, c++]
3030
----
31-
boost::decimal::decimal32_t a {1, 1}; // constructs 1e1 = 10
32-
boost::decimal::decimal32_t b {-2, -1}; // constructs -2e-2 or -0.2
33-
boost::decimal::decimal32_t c {2, -1, true}; // also constructs -2e-1 or -0.2
34-
boost::decimal::decimal32_t e {5, 5}; // constructs 5x10^5
35-
boost::decimal::decimal32_t f {1234, -3} // constructs 1.234 or 1234x10^-3
31+
boost::decimal::decimal32_t a {1, 1}; // constructs 1e1 = 10
32+
boost::decimal::decimal32_t b {-2, -1}; // constructs -2e-2 or -0.2
33+
boost::decimal::decimal32_t c {2U, -1, true}; // also constructs -2e-1 or -0.2 (Note: The coefficient must be an unsigned type)
34+
boost::decimal::decimal32_t e {5, 5}; // constructs 5x10^5
35+
boost::decimal::decimal32_t f {1234, -3} // constructs 1.234 or 1234x10^-3
36+
----
37+
38+
Overflow and underflow are handled the same way that they are with binary floating point numbers i.e. they will construct an infinity or a zero.
39+
40+
[source, c++]
41+
----
42+
boost::decimal::decimal64_t oveflow_value {100, 10000}; // Constructs +infinity
43+
boost::decimal::decimal64_t underflow_value {100, -10000}; // Constructs 0
3644
----
3745

3846
=== Construction from Integer
@@ -56,6 +64,9 @@ For example:
5664
boost::decimal::decimal128_t pi {3.14};
5765
----
5866

67+
Construction from non-finite values (e.g. `std::numeric_limits<double>::quiet_NaN()`) will yield the same non-finite value in the resulting decimal value.
68+
Overflow or underflow will construct infinity or 0.
69+
5970
NOTE: Due to the differences in decimal and binary floating point numbers there may be a difference in the resulting representation in decimal format, and thus it is not recommended to construct from binary floating point numbers
6071

6172
== Fundamental Operations

examples/basic_construction.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ int main()
4040
std::cout << "Floats are not equal" << std::endl;
4141
}
4242

43+
// Demonstration of the overflow and underflow handling
44+
constexpr decimal64_t oveflow_value {100, 10000};
45+
if (isinf(oveflow_value))
46+
{
47+
std::cout << "Overflow constructs infinity" << std::endl;
48+
}
49+
50+
constexpr decimal64_t underflow_value {100, -10000};
51+
if (underflow_value == 0)
52+
{
53+
std::cout << "Underflow constructs zero" << std::endl;
54+
}
55+
56+
const decimal32_t non_finite_from_float {std::numeric_limits<double>::quiet_NaN()};
57+
if (isnan(non_finite_from_float))
58+
{
59+
std::cout << "NaN constructs NaN" << std::endl;
60+
}
61+
4362
return 0;
4463
}
4564

0 commit comments

Comments
 (0)