Files
multiprecision/doc/tutorial_float_builtin_ctor.qbk
ivanpanch 55bf069621 Fix mistakes (#729)
* Update Jamfile.v2

* Update introduction.qbk

* Update tutorial.qbk

* Update tutorial_cpp_int.qbk

* Update tutorial_gmp_int.qbk

* Update tutorial_tommath.qbk

* Update integer_examples.cpp

* Update tutorial_cpp_bin_float.qbk

* Update tutorial_cpp_dec_float.qbk

* Update tutorial_gmp_float.qbk

* Update tutorial_mpfr_float.qbk

* Update tutorial_float128.qbk

* Update tutorial_float_builtin_ctor.qbk

* Update big_seventh.cpp

* Update tutorial_float_eg.qbk

* Update floating_point_examples.cpp

* Update mpfr_precision.cpp

* Update gauss_laguerre_quadrature.cpp

* Update tutorial_interval_mpfi.qbk

* Update tutorial_cpp_complex.qbk

* Update tutorial_mpc_complex.qbk

* Update tutorial_float128_complex.qbk

* Update tutorial_complex_adaptor.qbk

* Update tutorial_rational.qbk

* Update tutorial_tommath_rational.qbk

* Update tutorial_logged_adaptor.qbk

* Update tutorial_debug_adaptor.qbk

* Update tutorial_visualizers.qbk

* Update tutorial_fwd.qbk

* Update tutorial_conversions.qbk

* Update tutorial_random.qbk

* Update random_snips.cpp

* Update tutorial_constexpr.qbk

* Update tutorial_import_export.qbk

* Update cpp_int_import_export.cpp

* Update tutorial_mixed_precision.qbk

* Update tutorial_variable_precision.qbk

* Update scoped_precision_example.cpp

* Update tutorial_numeric_limits.qbk

* Update tutorial_numeric_limits.qbk

* Update numeric_limits_snips.cpp

* Update numeric_limits_snips.cpp

* Update tutorial_numeric_limits.qbk

* Update numeric_limits_snips.cpp

* Update numeric_limits_snips.cpp

* Update tutorial_io.qbk

* Update reference_number.qbk

* Update reference_cpp_bin_float.qbk

* Update reference_cpp_double_fp_backend.qbk

* Update reference_internal_support.qbk

* Update reference_backend_requirements.qbk

* Update performance.qbk

* Update performance_overhead.qbk

* Update performance_real_world.qbk

* Update performance_integer_real_world.qbk

* Update performance_rational_real_world.qbk

* Update reference_number.qbk

* Update tutorial_numeric_limits.qbk

* Update reference_backend_requirements.qbk
2025-08-18 13:14:39 +02:00

60 lines
2.1 KiB
Plaintext

[/
Copyright 2021 John Maddock.
Copyright 2021 Paul A. Bristow.
Copyright 2021 Christopher Kormanyos.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
]
[section:floatbuiltinctor Construction from Specific Values Without Precision Loss]
Construction of multiprecision types from built-in floating-point types
can lead to potentially unexpected, yet correct, results.
Consider, for instance, constructing an instance of `cpp_dec_float_50`
from the literal built-in floating-point `double` value 11.1.
#include <iomanip>
#include <iostream>
#include <limits>
#include <boost/multiprecision/cpp_dec_float.hpp>
int main()
{
using my_dec_100 = boost::multiprecision::cpp_dec_float_50;
const my_dec_100 f11(11.1);
// On a system with 64-bit double:
// 11.09999999999999964472863211994990706443786621093750
std::cout << std::setprecision(std::numeric_limits<my_dec_100>::digits10)
<< std::fixed
<< f11
<< std::endl;
}
In this example, the system has a 64-bit built-in `double` representation.
The variable `f11` is initialized with the literal
`double` value 11.1. Recall that built-in floating-point representations
are based on successive binary fractional approximations.
These are, in fact, very close approximations.
But they are approximations nonetheless, having their built-in finite precision.
For this reason,
the full multiple precision value of the `double` approximation
of 11.1 is given by the large value shown above. Observations show
us that the value is reliable up to the approximate 15 decimal
digit precision of built-in 64-bit `double` on this system.
If the exact value of 11.1 is desired
(within the wider precision of the multiprecision type),
then construction from literal string or from a rational
integral construction/division sequence should be used.
const my_dec_100 f11_str("11.1");
const my_dec_100 f11_n (my_dec_100(111) / 10);
[endsect] [/section:floatbuiltinctor Construction from Built-In Floats]