MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / grisu2

Function grisu2

extern/json/json.hpp:17649–17700  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17647*/
17648JSON_HEDLEY_NON_NULL(1)
17649inline void grisu2(char* buf, int& len, int& decimal_exponent,
17650 diyfp m_minus, diyfp v, diyfp m_plus)
17651{
17652 JSON_ASSERT(m_plus.e == m_minus.e);
17653 JSON_ASSERT(m_plus.e == v.e);
17654
17655 // --------(-----------------------+-----------------------)-------- (A)
17656 // m- v m+
17657 //
17658 // --------------------(-----------+-----------------------)-------- (B)
17659 // m- v m+
17660 //
17661 // First scale v (and m- and m+) such that the exponent is in the range
17662 // [alpha, gamma].
17663
17664 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
17665
17666 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
17667
17668 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
17669 const diyfp w = diyfp::mul(v, c_minus_k);
17670 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
17671 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
17672
17673 // ----(---+---)---------------(---+---)---------------(---+---)----
17674 // w- w w+
17675 // = c*m- = c*v = c*m+
17676 //
17677 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
17678 // w+ are now off by a small amount.
17679 // In fact:
17680 //
17681 // w - v * 10^k < 1 ulp
17682 //
17683 // To account for this inaccuracy, add resp. subtract 1 ulp.
17684 //
17685 // --------+---[---------------(---+---)---------------]---+--------
17686 // w- M- w M+ w+
17687 //
17688 // Now any number in [M-, M+] (bounds included) will round to w when input,
17689 // regardless of how the input rounding algorithm breaks ties.
17690 //
17691 // And digit_gen generates the shortest possible such number in [M-, M+].
17692 // Note that this does not mean that Grisu2 always generates the shortest
17693 // possible number in the interval (m-, m+).
17694 const diyfp M_minus(w_minus.f + 1, w_minus.e);
17695 const diyfp M_plus (w_plus.f - 1, w_plus.e );
17696
17697 decimal_exponent = -cached.k; // = -(-k) = k
17698
17699 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
17700}
17701
17702/*!
17703v = buf * 10^decimal_exponent

Callers 1

json.hppFile · 0.85

Calls 3

mulFunction · 0.85
grisu2_digit_genFunction · 0.85

Tested by

no test coverage detected