MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / grisu2

Function grisu2

examples/server/json.hpp:17611–17662  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17609*/
17610JSON_HEDLEY_NON_NULL(1)
17611inline void grisu2(char* buf, int& len, int& decimal_exponent,
17612 diyfp m_minus, diyfp v, diyfp m_plus)
17613{
17614 JSON_ASSERT(m_plus.e == m_minus.e);
17615 JSON_ASSERT(m_plus.e == v.e);
17616
17617 // --------(-----------------------+-----------------------)-------- (A)
17618 // m- v m+
17619 //
17620 // --------------------(-----------+-----------------------)-------- (B)
17621 // m- v m+
17622 //
17623 // First scale v (and m- and m+) such that the exponent is in the range
17624 // [alpha, gamma].
17625
17626 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
17627
17628 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
17629
17630 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
17631 const diyfp w = diyfp::mul(v, c_minus_k);
17632 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
17633 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
17634
17635 // ----(---+---)---------------(---+---)---------------(---+---)----
17636 // w- w w+
17637 // = c*m- = c*v = c*m+
17638 //
17639 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
17640 // w+ are now off by a small amount.
17641 // In fact:
17642 //
17643 // w - v * 10^k < 1 ulp
17644 //
17645 // To account for this inaccuracy, add resp. subtract 1 ulp.
17646 //
17647 // --------+---[---------------(---+---)---------------]---+--------
17648 // w- M- w M+ w+
17649 //
17650 // Now any number in [M-, M+] (bounds included) will round to w when input,
17651 // regardless of how the input rounding algorithm breaks ties.
17652 //
17653 // And digit_gen generates the shortest possible such number in [M-, M+].
17654 // Note that this does not mean that Grisu2 always generates the shortest
17655 // possible number in the interval (m-, m+).
17656 const diyfp M_minus(w_minus.f + 1, w_minus.e);
17657 const diyfp M_plus (w_plus.f - 1, w_plus.e );
17658
17659 decimal_exponent = -cached.k; // = -(-k) = k
17660
17661 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
17662}
17663
17664/*!
17665v = buf * 10^decimal_exponent

Callers 1

json.hppFile · 0.85

Calls 3

grisu2_digit_genFunction · 0.85
mulFunction · 0.50

Tested by

no test coverage detected