From d5e19c42f07b36efc6b853729ae70b4811bc453f Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Mon, 13 May 2024 13:52:54 +0200 Subject: [PATCH 1/7] Add basic construction example --- example/basic_construction.cpp | 26 ++++++++++++++++++++++++++ test/Jamfile | 3 +++ 2 files changed, 29 insertions(+) create mode 100644 example/basic_construction.cpp diff --git a/example/basic_construction.cpp b/example/basic_construction.cpp new file mode 100644 index 000000000..6317a708a --- /dev/null +++ b/example/basic_construction.cpp @@ -0,0 +1,26 @@ +// Copyright 2024 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +int main() +{ + using namespace boost::decimal; + + constexpr decimal32 val_1 {100}; // Construction from an integer + constexpr decimal32 val_2 {10, 1}; // Construction from an integer and exponent + constexpr decimal32 val_3 {1, 2, false}; // Construction from an integer, exponent, and sign + + std::cout << "Val_1: " << val_1 << '\n' + << "Val_2: " << val_2 << '\n' + << "Val_3: " << val_3 << '\n'; + + if (val_1 == val_2 && val_2 == val_3 && val_1 == val_3) + { + std::cout << "All equal values" << std::endl; + } + + return 0; +} diff --git a/test/Jamfile b/test/Jamfile index cf017d74b..ef9c74e07 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -123,3 +123,6 @@ run test_tgamma.cpp ; run test_to_chars.cpp ; run test_to_string.cpp ; run test_type_traits.cpp ; + +# Examples +run ../example/basic_construction.cpp ; From 87aae5b82ad1f6e1e7ab4ccc8628448b7a4f646b Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Mon, 13 May 2024 14:12:43 +0200 Subject: [PATCH 2/7] Add floating point example --- example/basic_construction.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/example/basic_construction.cpp b/example/basic_construction.cpp index 6317a708a..8ee40f7b0 100644 --- a/example/basic_construction.cpp +++ b/example/basic_construction.cpp @@ -4,6 +4,7 @@ #include #include +#include int main() { @@ -22,5 +23,23 @@ int main() std::cout << "All equal values" << std::endl; } + constexpr decimal64 val_4 {decimal64{2, -1} + decimal64{1, -1}}; + constexpr double float_val_4 {0.2 + 0.1}; + const decimal64 val_5 { float_val_4 }; + + std::cout << std::setprecision(17) << "Val_4: " << val_4 << '\n' + << "Float: " << float_val_4 << '\n' + << "Val_5: " << val_5 << '\n'; + + if (val_4 == val_5) + { + std::cout << "Floats are equal" << std::endl; + } + else + { + std::cout << "Floats are not equal" << std::endl; + } + return 0; } + From 15fd12e171bf6b2a712e403e7fe3974ac1b25b15 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Mon, 13 May 2024 16:47:33 +0200 Subject: [PATCH 3/7] Add rounding mode example --- doc/decimal/examples.adoc | 2 +- {example => examples}/basic_construction.cpp | 0 examples/rounding_mode.cpp | 18 ++++++++++++++++++ test/Jamfile | 3 ++- 4 files changed, 21 insertions(+), 2 deletions(-) rename {example => examples}/basic_construction.cpp (100%) create mode 100644 examples/rounding_mode.cpp diff --git a/doc/decimal/examples.adoc b/doc/decimal/examples.adoc index 754c468e1..bba5c3ef4 100644 --- a/doc/decimal/examples.adoc +++ b/doc/decimal/examples.adoc @@ -96,7 +96,7 @@ int main() { auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero - auto new_rounding_mode = fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest); + auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest); assert(default_rounding_mode != new_rounding_mode); diff --git a/example/basic_construction.cpp b/examples/basic_construction.cpp similarity index 100% rename from example/basic_construction.cpp rename to examples/basic_construction.cpp diff --git a/examples/rounding_mode.cpp b/examples/rounding_mode.cpp new file mode 100644 index 000000000..a994f36ad --- /dev/null +++ b/examples/rounding_mode.cpp @@ -0,0 +1,18 @@ +// Copyright 2024 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +int main() +{ + auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero + + auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest); + + assert(default_rounding_mode != new_rounding_mode); + + return 0; +} + diff --git a/test/Jamfile b/test/Jamfile index ef9c74e07..8cfab0199 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -125,4 +125,5 @@ run test_to_string.cpp ; run test_type_traits.cpp ; # Examples -run ../example/basic_construction.cpp ; +run ../examples/basic_construction.cpp ; +run ../examples/rounding_mode.cpp ; From 5dc3c3f7dfc4eb5df34a46889d5dd8c393710c1d Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 14 May 2024 08:41:08 +0200 Subject: [PATCH 4/7] Update charconv example --- doc/decimal/examples.adoc | 9 ++++++++- examples/charconv.cpp | 31 +++++++++++++++++++++++++++++++ test/Jamfile | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 examples/charconv.cpp diff --git a/doc/decimal/examples.adoc b/doc/decimal/examples.adoc index bba5c3ef4..b21180bfb 100644 --- a/doc/decimal/examples.adoc +++ b/doc/decimal/examples.adoc @@ -8,6 +8,8 @@ https://www.boost.org/LICENSE_1_0.txt = Examples :idprefix: examples_ +All examples can be found in the library `examples/` folder as well. + == Construction from an Integer and Exponent [source, c++] @@ -64,13 +66,14 @@ int main() [source, c++] ---- #include +#include #include int main() { using namespace boost::decimal; - decimal64 val {0.25}; // Construction from a double (not recommended but allowed) + decimal64 val {0.25}; // Construction from a double (not recommended but explicit construction is allowed) char buffer[256]; auto r_to = to_chars(buffer, buffer + sizeof(buffer) - 1, val); @@ -79,9 +82,13 @@ int main() decimal64 return_value; auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value); + assert(r_from); assert(val == return_value); + std::cout << " Initial Value: " << val << '\n' + << "Returned Value: " << return_value << std::endl; + return 0; } ---- diff --git a/examples/charconv.cpp b/examples/charconv.cpp new file mode 100644 index 000000000..867b55a62 --- /dev/null +++ b/examples/charconv.cpp @@ -0,0 +1,31 @@ +// Copyright 2024 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int main() +{ + using namespace boost::decimal; + + decimal64 val {0.25}; // Construction from a double (not recommended but explicit construction is allowed) + + char buffer[256]; + auto r_to = to_chars(buffer, buffer + sizeof(buffer) - 1, val); + assert(r_to); // checks std::errc() + *r_to.ptr = '\0'; + + decimal64 return_value; + auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value); + assert(r_from); + + assert(val == return_value); + + std::cout << " Initial Value: " << val << '\n' + << "Returned Value: " << return_value << std::endl; + + return 0; +} + diff --git a/test/Jamfile b/test/Jamfile index 8cfab0199..4b33dc926 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -126,4 +126,5 @@ run test_type_traits.cpp ; # Examples run ../examples/basic_construction.cpp ; +run ../examples/charconv.cpp ; run ../examples/rounding_mode.cpp ; From 2797494844a6b37d1b48398de504135774bb3489 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 14 May 2024 08:53:50 +0200 Subject: [PATCH 5/7] Add generic programming example --- doc/decimal/examples.adoc | 40 ++++++++++++++++++++++++++++++++++++++ examples/adl.cpp | 41 +++++++++++++++++++++++++++++++++++++++ test/Jamfile | 1 + 3 files changed, 82 insertions(+) create mode 100644 examples/adl.cpp diff --git a/doc/decimal/examples.adoc b/doc/decimal/examples.adoc index b21180bfb..1cca6d473 100644 --- a/doc/decimal/examples.adoc +++ b/doc/decimal/examples.adoc @@ -110,3 +110,43 @@ int main() return 0; } ---- + +== Generic Programming +[source, c++] +---- +#include +#include +#include + +int error_counter = 0; + +template +bool float_equal(T lhs, T rhs) +{ + using std::fabs; + return fabs(lhs - rhs) < std::numeric_limits::epsilon(); // numeric_limits is overloaded for all decimal types +} + +template +void test(T val) +{ + using std::sin; // ADL allows builtin and decimal types to both be used + if (!float_equal(sin(val), -sin(-val))) // sin(x) == -sin(-x) + { + ++error_counter; + } +} + +int main() +{ + test(-0.5F); + test(-0.5); + test(-0.5L); + + test(boost::decimal::decimal32{5, -1, true}); + test(boost::decimal::decimal64{5, -1, true}); + test(boost::decimal::decimal128{5, -1, true}); + + return error_counter; +} +---- diff --git a/examples/adl.cpp b/examples/adl.cpp new file mode 100644 index 000000000..9f9680b68 --- /dev/null +++ b/examples/adl.cpp @@ -0,0 +1,41 @@ +// Copyright 2024 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int error_counter = 0; + +template +bool float_equal(T lhs, T rhs) +{ + using std::fabs; + return fabs(lhs - rhs) < std::numeric_limits::epsilon(); // numeric_limits is overloaded for all decimal types +} + +template +void test(T val) +{ + using std::sin; // ADL allows builtin and decimal types to both be used + if (!float_equal(sin(val), -sin(-val))) // sin(x) == -sin(-x) + { + ++error_counter; + } +} + +int main() +{ + test(-0.5F); + test(-0.5); + test(-0.5L); + + test(boost::decimal::decimal32{5, -1, true}); + test(boost::decimal::decimal64{5, -1, true}); + test(boost::decimal::decimal128{5, -1, true}); + + return error_counter; +} + + diff --git a/test/Jamfile b/test/Jamfile index 4b33dc926..8552a5b2d 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -125,6 +125,7 @@ run test_to_string.cpp ; run test_type_traits.cpp ; # Examples +run ../examples/adl.cpp ; run ../examples/basic_construction.cpp ; run ../examples/charconv.cpp ; run ../examples/rounding_mode.cpp ; From 07aa45ad7bfddfbc421895e44f058546c0e2bfcf Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 14 May 2024 09:30:20 +0200 Subject: [PATCH 6/7] Add literals and constants example --- doc/decimal/examples.adoc | 29 +++++++++++++++++++++++++++++ examples/literals.cpp | 30 ++++++++++++++++++++++++++++++ test/Jamfile | 1 + 3 files changed, 60 insertions(+) create mode 100644 examples/literals.cpp diff --git a/doc/decimal/examples.adoc b/doc/decimal/examples.adoc index 1cca6d473..fdee4e150 100644 --- a/doc/decimal/examples.adoc +++ b/doc/decimal/examples.adoc @@ -150,3 +150,32 @@ int main() return error_counter; } ---- + +== Literals and Constants +[source, c++] +---- +#include +#include + +template +bool float_equal(T lhs, T rhs) +{ + using std::fabs; + return fabs(lhs - rhs) < std::numeric_limits::epsilon(); // numeric_limits is overloaded for all decimal types +} + + +int main() +{ + using namespace boost::decimal; + + const auto pi_32 {"3.141592653589793238"_DF}; + const auto pi_64 {"3.141592653589793238"_DD}; + + assert(float_equal(pi_32, static_cast(pi_64))); // Explicit conversion between decimal types + assert(float_equal(pi_32, boost::decimal::numbers::pi_v)); // Constants available in numbers namespace + assert(float_equal(pi_64, numbers::pi)); // Default constant type is decimal64 + + return 0; +} +---- diff --git a/examples/literals.cpp b/examples/literals.cpp new file mode 100644 index 000000000..6361e26ff --- /dev/null +++ b/examples/literals.cpp @@ -0,0 +1,30 @@ +// Copyright 2024 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +template +bool float_equal(T lhs, T rhs) +{ + using std::fabs; + return fabs(lhs - rhs) < std::numeric_limits::epsilon(); // numeric_limits is overloaded for all decimal types +} + + +int main() +{ + using namespace boost::decimal; + + const auto pi_32 {"3.141592653589793238"_DF}; + const auto pi_64 {"3.141592653589793238"_DD}; + + assert(float_equal(pi_32, static_cast(pi_64))); // Explicit conversion between decimal types + assert(float_equal(pi_32, boost::decimal::numbers::pi_v)); // Constants available in numbers namespace + assert(float_equal(pi_64, numbers::pi)); // Default constant type is decimal64 + + return 0; +} + + diff --git a/test/Jamfile b/test/Jamfile index 8552a5b2d..1be80e28e 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -128,4 +128,5 @@ run test_type_traits.cpp ; run ../examples/adl.cpp ; run ../examples/basic_construction.cpp ; run ../examples/charconv.cpp ; +run ../examples/literals.cpp ; run ../examples/rounding_mode.cpp ; From df5a8ad0e6d803ff16ae07e4eccac954099494cb Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 14 May 2024 10:08:49 +0200 Subject: [PATCH 7/7] Remove examples from CI run --- test/Jamfile | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/Jamfile b/test/Jamfile index 1be80e28e..cf017d74b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -123,10 +123,3 @@ run test_tgamma.cpp ; run test_to_chars.cpp ; run test_to_string.cpp ; run test_type_traits.cpp ; - -# Examples -run ../examples/adl.cpp ; -run ../examples/basic_construction.cpp ; -run ../examples/charconv.cpp ; -run ../examples/literals.cpp ; -run ../examples/rounding_mode.cpp ;