implements the Grisu2 algorithm for binary to decimal floating-point conversion. More...
Classes | |
struct | diyfp |
struct | boundaries |
struct | cached_power |
Functions | |
template<typename Target , typename Source > | |
Target | reinterpret_bits (const Source source) |
template<typename FloatType > | |
boundaries | compute_boundaries (FloatType value) |
cached_power | get_cached_power_for_binary_exponent (int e) |
int | find_largest_pow10 (const std::uint32_t n, std::uint32_t &pow10) |
void | grisu2_round (char *buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k) |
void | grisu2_digit_gen (char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus) |
JSON_HEDLEY_NON_NULL (1) JSON_HEDLEY_RETURNS_NON_NULL inline char *append_exponent(char *buf | |
appends a decimal representation of e to buf More... | |
JSON_ASSERT (m_plus.e==v.e) | |
const diyfp | c_minus_k (cached.f, cached.e) |
const diyfp | M_minus (w_minus.f+1, w_minus.e) |
const diyfp | M_plus (w_plus.f - 1, w_plus.e) |
grisu2_digit_gen (buf, len, decimal_exponent, M_minus, w, M_plus) | |
JSON_ASSERT (std::isfinite(value)) | |
JSON_ASSERT (value > 0) | |
grisu2 (buf, len, decimal_exponent, w.minus, w.w, w.plus) | |
JSON_ASSERT (e< 1000) | |
if (e< 0) | |
JSON_ASSERT (max_exp > 0) | |
if (k<=n &&n<=max_exp) | |
if (k==1) | |
return | append_exponent (buf, n - 1) |
Variables | |
constexpr int | kAlpha = -60 |
constexpr int | kGamma = -32 |
int & | len |
int int & | decimal_exponent = -cached.k |
int int diyfp | m_minus |
int int diyfp diyfp | v |
int int diyfp diyfp diyfp | m_plus |
const cached_power | cached = get_cached_power_for_binary_exponent(m_plus.e) |
const diyfp | w = diyfp::mul(v, c_minus_k) |
const diyfp | w_minus = diyfp::mul(m_minus, c_minus_k) |
const diyfp | w_plus = diyfp::mul(m_plus, c_minus_k) |
int int FloatType | value |
int | e |
else | |
auto | k = static_cast<std::uint32_t>(e) |
* | buf = static_cast<char>('0' + k / 10) |
int int int | min_exp |
int int int int | max_exp |
const int | n = len + decimal_exponent |
implements the Grisu2 algorithm for binary to decimal floating-point conversion.
This implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz).
The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
For a detailed description of the algorithm see:
[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming Language Design and Implementation, PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, PLDI 1996
const diyfp nlohmann::detail::dtoa_impl::c_minus_k | ( | cached. | f, |
cached. | e | ||
) |
boundaries nlohmann::detail::dtoa_impl::compute_boundaries | ( | FloatType | value | ) |
Compute the (normalized) diyfp representing the input number 'value' and its boundaries.
Definition at line 14521 of file json.h.
References nlohmann::detail::dtoa_impl::diyfp::e, nlohmann::detail::dtoa_impl::diyfp::f, JSON_ASSERT(), nlohmann::detail::kMinExp, m_minus, m_plus, nlohmann::detail::dtoa_impl::diyfp::normalize(), nlohmann::detail::dtoa_impl::diyfp::normalize_to(), v, value, w_minus, and w_plus.
|
inline |
For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. For n == 0, returns 1 and sets pow10 := 1.
Definition at line 14824 of file json.h.
References n.
Referenced by grisu2_digit_gen().
|
inline |
For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c satisfies (Definition 3.2 from [1])
alpha <= e_c + e + q <= gamma.
Definition at line 14660 of file json.h.
References cached, nlohmann::detail::dtoa_impl::cached_power::e, e, JSON_ASSERT(), k, kAlpha, and kGamma.
nlohmann::detail::dtoa_impl::grisu2 | ( | buf | , |
len | , | ||
decimal_exponent | , | ||
w. | minus, | ||
w. | w, | ||
w. | plus | ||
) |
nlohmann::detail::dtoa_impl::grisu2_digit_gen | ( | buf | , |
len | , | ||
decimal_exponent | , | ||
M_minus | , | ||
w | , | ||
M_plus | |||
) |
|
inline |
Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
Definition at line 14921 of file json.h.
References decimal_exponent, nlohmann::detail::dtoa_impl::diyfp::e, nlohmann::detail::dtoa_impl::diyfp::f, find_largest_pow10(), grisu2_round(), JSON_ASSERT(), k, kAlpha, kGamma, M_minus(), M_plus(), n, nlohmann::detail::dtoa_impl::diyfp::sub(), and w.
|
inline |
Definition at line 14880 of file json.h.
References buf, JSON_ASSERT(), and len.
Referenced by grisu2_digit_gen().
nlohmann::detail::dtoa_impl::JSON_ASSERT | ( | ) |
nlohmann::detail::dtoa_impl::JSON_ASSERT | ( | m_plus. | e = =v.e | ) |
nlohmann::detail::dtoa_impl::JSON_ASSERT | ( | max_exp | , |
0 | |||
) |
nlohmann::detail::dtoa_impl::JSON_ASSERT | ( | std::isfinite(value) | ) |
nlohmann::detail::dtoa_impl::JSON_ASSERT | ( | value | , |
0 | |||
) |
nlohmann::detail::dtoa_impl::JSON_HEDLEY_NON_NULL | ( | 1 | ) |
appends a decimal representation of e to buf
prettify v = buf * 10^decimal_exponent
v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10.
If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point notation. Otherwise it will be printed in exponential notation.
const diyfp nlohmann::detail::dtoa_impl::M_minus | ( | w_minus.f+ | 1, |
w_minus. | e | ||
) |
Referenced by grisu2_digit_gen().
const diyfp nlohmann::detail::dtoa_impl::M_plus | ( | w_plus.f - | 1, |
w_plus. | e | ||
) |
Referenced by grisu2_digit_gen().
Target nlohmann::detail::dtoa_impl::reinterpret_bits | ( | const Source | source | ) |
* nlohmann::detail::dtoa_impl::buf = static_cast<char>('0' + k / 10) |
Definition at line 15295 of file json.h.
Referenced by grisu2_round(), if(), and muq::Optimization::NewtonTrust::Solve().
const cached_power nlohmann::detail::dtoa_impl::cached = get_cached_power_for_binary_exponent(m_plus.e) |
Definition at line 15177 of file json.h.
Referenced by get_cached_power_for_binary_exponent().
int int nlohmann::detail::dtoa_impl::decimal_exponent = -cached.k |
Definition at line 15162 of file json.h.
Referenced by grisu2_digit_gen().
int nlohmann::detail::dtoa_impl::e |
Definition at line 15262 of file json.h.
Referenced by muq::SamplingAlgorithms::ParallelTempering::AdaptTemperatures(), muq::Modeling::ModPiece::ApplyHessianByFD(), muq::Modeling::ModPiece::ApplyJacobianByFD(), muq::Modeling::ODE::ApplyJacobianImpl(), muq::Modeling::Distribution::ApplyLogDensityHessianImpl(), muq::Modeling::WorkGraph::BindEdge(), BuildStateSpaceGP(), umbridge::check_model_exists(), ComputeMAP(), muq::Modeling::WorkGraph::Constant(), muq::Utilities::MultiIndexFactory::CreateHyperbolic(), muq::Modeling::WorkGraph::CreateModPiece(), muq::Modeling::DependentPredicate::DownstreamNodes(), muq::Modeling::ODE::EvaluateImpl(), muq::Modeling::LinearSDE::ExtractOptions(), muq::Approximation::WhiteNoiseKernel::FillBlockImpl(), FitData(), nlohmann::detail::from_json(), get_cached_power_for_binary_exponent(), muq::Modeling::WorkGraph::GetChildren(), muq::Modeling::WorkGraph::GetConstantOutputs(), muq::Modeling::WorkGraph::GetEdges(), muq::Modeling::WorkGraph::GetParent(), muq::Modeling::WorkGraph::GetParents(), muq::Modeling::ModPiece::GetRunTime(), muq::Modeling::ModGraphPiece::GradientGraph(), muq::Modeling::ODE::GradientImpl(), muq::Modeling::Distribution::GradLogDensityImpl(), muq::Modeling::WorkGraph::GraphInputs(), muq::Modeling::WorkGraph::GraphOutputs(), if(), muq::Modeling::ModGraphPiece::InputNodes(), muq::Modeling::WorkGraphPiece::InputNodes(), muq::Modeling::ModPiece::JacobianByFD(), muq::Modeling::ModGraphPiece::JacobianGraph(), muq::Modeling::ODE::JacobianImpl(), muq::SamplingAlgorithms::MALAProposal::LogDensity(), muq::Modeling::WorkGraph::Print(), muq::Modeling::WorkGraph::RecursiveCut(), muq::Modeling::ODE::IntegratorOptions::SetOptions(), muq::Optimization::NewtonTrust::Solve(), muq::Approximation::GaussianProcess::SolveFromEig(), muq::SamplingAlgorithms::MHKernel::Step(), nlohmann::detail::to_json(), and muq::Modeling::UpstreamPredicate::UpstreamNodes().
nlohmann::detail::dtoa_impl::else |
const int nlohmann::detail::dtoa_impl::k = static_cast<std::uint32_t>(e) |
Definition at line 15277 of file json.h.
Referenced by muq::SamplingAlgorithms::GMHKernel::AcceptanceDensity(), muq::Approximation::Laguerre::ak(), muq::Approximation::Legendre::ak(), muq::SamplingAlgorithms::DRKernel::Alpha(), muq::Approximation::OrthogonalPolynomial::BasisEvaluate(), muq::Approximation::Laguerre::bk(), muq::Approximation::Laguerre::ck(), muq::Approximation::Legendre::ck(), muq::Approximation::PhysicistHermite::ck(), muq::Approximation::ProbabilistHermite::ck(), muq::Approximation::PCEFactory::Compute(), muq::Approximation::PolynomialChaosExpansion::ComputeWeightedSum(), muq::Approximation::Monomial::DerivativeEvaluate(), muq::Approximation::PhysicistHermite::DerivativeEvaluate(), muq::Approximation::ProbabilistHermite::DerivativeEvaluate(), muq::Modeling::FenicsPiece::EvaluateImpl(), FitData(), get_cached_power_for_binary_exponent(), muq::Utilities::RandomGenerator::GetDiscrete(), muq::Approximation::BasisExpansion::GetHessians(), muq::Approximation::PolynomialChaosExpansion::GetNormalizationVec(), grisu2_digit_gen(), if(), nlohmann::detail::json_sax_dom_callback_parser< BasicJsonType >::key(), main(), muq::Approximation::HermiteFunction::nChoosek(), muq::Modeling::FlannCache::NearestNeighbors(), muq::Approximation::PolynomialChaosExpansion::SobolSensitivity(), muq::Approximation::SmolyakQuadrature::UpdateWeights(), and WriteResults().
|
constexpr |
Definition at line 14643 of file json.h.
Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().
|
constexpr |
Definition at line 14644 of file json.h.
Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().
int nlohmann::detail::dtoa_impl::len |
Definition at line 15162 of file json.h.
Referenced by muq::Utilities::HDF5File::GetChildren(), and grisu2_round().
int int diyfp nlohmann::detail::dtoa_impl::m_minus |
Definition at line 15163 of file json.h.
Referenced by compute_boundaries().
int int int int nlohmann::detail::dtoa_impl::max_exp |
const int nlohmann::detail::dtoa_impl::n = len + decimal_exponent |
Definition at line 15321 of file json.h.
Referenced by muq::Optimization::NLoptOptimizer::Constraint(), muq::Optimization::NLoptOptimizer::Cost(), muq::Approximation::LocalRegression::ErrorIndicator(), muq::Approximation::MaternKernel::Factorial(), find_largest_pow10(), nlohmann::detail::from_json(), muq::Utilities::RandomGenerator::GetNormal(), grisu2_digit_gen(), if(), JSON_HEDLEY_NON_NULL(), muq::Approximation::HermiteFunction::nChoosek(), muq::Approximation::nlopt_obj(), nlohmann::detail::primitive_iterator_t::operator+(), nlohmann::detail::primitive_iterator_t::operator+=(), nlohmann::detail::primitive_iterator_t::operator-=(), nlohmann::detail::iter_impl< BasicJsonType >::operator[](), nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_compact_float(), nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number(), and nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number_with_ubjson_prefix().
Definition at line 15163 of file json.h.
Referenced by compute_boundaries(), muq::Approximation::Regression::ComputeBasisDerivatives(), nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::get_to(), muq::Modeling::WorkGraph::GetChildren(), muq::Modeling::WorkGraph::GetNodeIterator(), muq::Modeling::WorkGraph::GetParent(), muq::Modeling::WorkGraph::GetParents(), muq::Modeling::WorkGraph::GetPiece(), muq::Modeling::WorkGraph::GraphInputs(), muq::Modeling::WorkGraph::GraphOutputs(), nlohmann::detail::json_sax_dom_parser< BasicJsonType >::handle_value(), nlohmann::detail::json_sax_dom_callback_parser< BasicJsonType >::handle_value(), muq::Modeling::ModGraphPiece::ModGraphPiece(), muq::Modeling::WorkGraph::Print(), muq::Modeling::WorkGraph::RecursiveCut(), muq::Modeling::ODE::Interface::RHSJacobianAction(), muq::Approximation::Regression::VandermondeMatrix(), muq::Modeling::WorkGraph::Visualize(), muq::Modeling::WorkGraphPiece::WorkGraphPiece(), nlohmann::detail::output_vector_adapter< CharType >::write_character(), and nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_ubjson().
int int FloatType nlohmann::detail::dtoa_impl::value |
Definition at line 15222 of file json.h.
Referenced by muq::Modeling::ModPiece::ApplyHessianRecurse(), muq::Modeling::ModPiece::ApplyJacobianRecurse(), compute_boundaries(), and muq::Modeling::ModPiece::GradientRecurse().
const boundaries nlohmann::detail::dtoa_impl::w = diyfp::mul(v, c_minus_k) |
Definition at line 15182 of file json.h.
Referenced by muq::SamplingAlgorithms::ParallelTempering::AdaptTemperatures(), nlohmann::detail::parse_error::create(), nlohmann::detail::invalid_iterator::create(), nlohmann::detail::type_error::create(), nlohmann::detail::out_of_range::create(), nlohmann::detail::other_error::create(), and grisu2_digit_gen().
const diyfp nlohmann::detail::dtoa_impl::w_minus = diyfp::mul(m_minus, c_minus_k) |
Definition at line 15183 of file json.h.
Referenced by compute_boundaries().
const diyfp nlohmann::detail::dtoa_impl::w_plus = diyfp::mul(m_plus, c_minus_k) |
Definition at line 15184 of file json.h.
Referenced by compute_boundaries().