TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ grisu2() [1/2]

void nlohmann::detail::dtoa_impl::grisu2 ( char *  buf,
int &  len,
int &  decimal_exponent,
diyfp  m_minus,
diyfp  v,
diyfp  m_plus 
)
inline

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.

Definition at line 16321 of file json.hpp.

16323{
16324 JSON_ASSERT(m_plus.e == m_minus.e);
16325 JSON_ASSERT(m_plus.e == v.e);
16326 // --------(-----------------------+-----------------------)-------- (A)
16327 // m- v m+
16328 //
16329 // --------------------(-----------+-----------------------)-------- (B)
16330 // m- v m+
16331 //
16332 // First scale v (and m- and m+) such that the exponent is in the range
16333 // [alpha, gamma].
16334 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
16335 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
16336 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
16337 const diyfp w = diyfp::mul(v, c_minus_k);
16338 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
16339 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
16340 // ----(---+---)---------------(---+---)---------------(---+---)----
16341 // w- w w+
16342 // = c*m- = c*v = c*m+
16343 //
16344 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
16345 // w+ are now off by a small amount.
16346 // In fact:
16347 //
16348 // w - v * 10^k < 1 ulp
16349 //
16350 // To account for this inaccuracy, add resp. subtract 1 ulp.
16351 //
16352 // --------+---[---------------(---+---)---------------]---+--------
16353 // w- M- w M+ w+
16354 //
16355 // Now any number in [M-, M+] (bounds included) will round to w when input,
16356 // regardless of how the input rounding algorithm breaks ties.
16357 //
16358 // And digit_gen generates the shortest possible such number in [M-, M+].
16359 // Note that this does not mean that Grisu2 always generates the shortest
16360 // possible number in the interval (m-, m+).
16361 const diyfp M_minus(w_minus.f + 1, w_minus.e);
16362 const diyfp M_plus (w_plus.f - 1, w_plus.e );
16363 decimal_exponent = -cached.k; // = -(-k) = k
16364 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
16365}
void grisu2_digit_gen(char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
Definition: json.hpp:16097
cached_power get_cached_power_for_binary_exponent(int e)
Definition: json.hpp:15837