MUQ  0.4.3
nlohmann::detail::dtoa_impl Namespace Reference

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
 

Detailed Description

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

Function Documentation

◆ append_exponent()

return nlohmann::detail::dtoa_impl::append_exponent ( buf  ,
n 1 
)

◆ c_minus_k()

const diyfp nlohmann::detail::dtoa_impl::c_minus_k ( cached.  f,
cached.  e 
)

◆ compute_boundaries()

template<typename FloatType >
boundaries nlohmann::detail::dtoa_impl::compute_boundaries ( FloatType  value)

Compute the (normalized) diyfp representing the input number 'value' and its boundaries.

Precondition
value must be finite and positive

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.

◆ find_largest_pow10()

int nlohmann::detail::dtoa_impl::find_largest_pow10 ( const std::uint32_t  n,
std::uint32_t &  pow10 
)
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().

◆ get_cached_power_for_binary_exponent()

cached_power nlohmann::detail::dtoa_impl::get_cached_power_for_binary_exponent ( int  e)
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.

◆ grisu2()

nlohmann::detail::dtoa_impl::grisu2 ( buf  ,
len  ,
decimal_exponent  ,
w.  minus,
w.  w,
w.  plus 
)

◆ grisu2_digit_gen() [1/2]

nlohmann::detail::dtoa_impl::grisu2_digit_gen ( buf  ,
len  ,
decimal_exponent  ,
M_minus  ,
w  ,
M_plus   
)

◆ grisu2_digit_gen() [2/2]

void nlohmann::detail::dtoa_impl::grisu2_digit_gen ( char *  buffer,
int &  length,
int &  decimal_exponent,
diyfp  M_minus,
diyfp  w,
diyfp  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.

◆ grisu2_round()

void nlohmann::detail::dtoa_impl::grisu2_round ( char *  buf,
int  len,
std::uint64_t  dist,
std::uint64_t  delta,
std::uint64_t  rest,
std::uint64_t  ten_k 
)
inline

Definition at line 14880 of file json.h.

References buf, JSON_ASSERT(), and len.

Referenced by grisu2_digit_gen().

◆ if() [1/3]

nlohmann::detail::dtoa_impl::if ( )

Definition at line 15267 of file json.h.

References buf, and e.

◆ if() [2/3]

nlohmann::detail::dtoa_impl::if ( k<=n &&n<=  max_exp)

Definition at line 15327 of file json.h.

References buf, k, and n.

◆ if() [3/3]

nlohmann::detail::dtoa_impl::if ( k  = = 1)

Definition at line 15363 of file json.h.

References buf.

◆ JSON_ASSERT() [1/5]

nlohmann::detail::dtoa_impl::JSON_ASSERT ( )

◆ JSON_ASSERT() [2/5]

◆ JSON_ASSERT() [3/5]

nlohmann::detail::dtoa_impl::JSON_ASSERT ( max_exp  ,
 
)

◆ JSON_ASSERT() [4/5]

nlohmann::detail::dtoa_impl::JSON_ASSERT ( std::isfinite(value )

◆ JSON_ASSERT() [5/5]

nlohmann::detail::dtoa_impl::JSON_ASSERT ( value  ,
 
)

◆ JSON_HEDLEY_NON_NULL()

nlohmann::detail::dtoa_impl::JSON_HEDLEY_NON_NULL ( )

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.

Returns
a pointer to the element following the exponent.
Precondition
-1000 < e < 1000

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.

Precondition
min_exp < 0
max_exp > 0

◆ M_minus()

const diyfp nlohmann::detail::dtoa_impl::M_minus ( w_minus.f+  1,
w_minus.  e 
)

Referenced by grisu2_digit_gen().

◆ M_plus()

const diyfp nlohmann::detail::dtoa_impl::M_plus ( w_plus.f -  1,
w_plus.  e 
)

Referenced by grisu2_digit_gen().

◆ reinterpret_bits()

template<typename Target , typename Source >
Target nlohmann::detail::dtoa_impl::reinterpret_bits ( const Source  source)

Definition at line 14380 of file json.h.

Variable Documentation

◆ buf

* 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().

◆ cached

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().

◆ decimal_exponent

int int nlohmann::detail::dtoa_impl::decimal_exponent = -cached.k

Definition at line 15162 of file json.h.

Referenced by grisu2_digit_gen().

◆ e

int nlohmann::detail::dtoa_impl::e
Initial value:
{
JSON_ASSERT(e > -1000)
JSON_ASSERT(last - first >=std::numeric_limits< FloatType >::max_digits10+6)

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().

◆ else

nlohmann::detail::dtoa_impl::else
Initial value:

Definition at line 15272 of file json.h.

◆ k

◆ kAlpha

constexpr int nlohmann::detail::dtoa_impl::kAlpha = -60
constexpr

Definition at line 14643 of file json.h.

Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().

◆ kGamma

constexpr int nlohmann::detail::dtoa_impl::kGamma = -32
constexpr

Definition at line 14644 of file json.h.

Referenced by get_cached_power_for_binary_exponent(), and grisu2_digit_gen().

◆ len

int nlohmann::detail::dtoa_impl::len

Definition at line 15162 of file json.h.

Referenced by muq::Utilities::HDF5File::GetChildren(), and grisu2_round().

◆ m_minus

int int diyfp nlohmann::detail::dtoa_impl::m_minus

Definition at line 15163 of file json.h.

Referenced by compute_boundaries().

◆ m_plus

int int diyfp diyfp diyfp nlohmann::detail::dtoa_impl::m_plus
Initial value:

Definition at line 15163 of file json.h.

Referenced by compute_boundaries().

◆ max_exp

int int int int nlohmann::detail::dtoa_impl::max_exp
Initial value:

Definition at line 15315 of file json.h.

◆ min_exp

int int int nlohmann::detail::dtoa_impl::min_exp

Definition at line 15315 of file json.h.

◆ n

◆ v

◆ value

int int FloatType nlohmann::detail::dtoa_impl::value
Initial value:
{
static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
"internal error: not enough precision")

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().

◆ w

◆ w_minus

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().

◆ w_plus

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().