to_chars.hpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  7. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  8. // SPDX-License-Identifier: MIT
  9. #pragma once
  10. #include <array> // array
  11. #include <cmath> // signbit, isfinite
  12. #include <cstdint> // intN_t, uintN_t
  13. #include <cstring> // memcpy, memmove
  14. #include <limits> // numeric_limits
  15. #include <type_traits> // conditional
  16. #include <nlohmann/detail/macro_scope.hpp>
  17. NLOHMANN_JSON_NAMESPACE_BEGIN
  18. namespace detail
  19. {
  20. /*!
  21. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  22. conversion.
  23. This implementation is a slightly modified version of the reference
  24. implementation which may be obtained from
  25. http://florian.loitsch.com/publications (bench.tar.gz).
  26. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  27. For a detailed description of the algorithm see:
  28. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  29. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  30. Language Design and Implementation, PLDI 2010
  31. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  32. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  33. Design and Implementation, PLDI 1996
  34. */
  35. namespace dtoa_impl
  36. {
  37. template<typename Target, typename Source>
  38. Target reinterpret_bits(const Source source)
  39. {
  40. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  41. Target target;
  42. std::memcpy(&target, &source, sizeof(Source));
  43. return target;
  44. }
  45. struct diyfp // f * 2^e
  46. {
  47. static constexpr int kPrecision = 64; // = q
  48. std::uint64_t f = 0;
  49. int e = 0;
  50. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  51. /*!
  52. @brief returns x - y
  53. @pre x.e == y.e and x.f >= y.f
  54. */
  55. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  56. {
  57. JSON_ASSERT(x.e == y.e);
  58. JSON_ASSERT(x.f >= y.f);
  59. return {x.f - y.f, x.e};
  60. }
  61. /*!
  62. @brief returns x * y
  63. @note The result is rounded. (Only the upper q bits are returned.)
  64. */
  65. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  66. {
  67. static_assert(kPrecision == 64, "internal error");
  68. // Computes:
  69. // f = round((x.f * y.f) / 2^q)
  70. // e = x.e + y.e + q
  71. // Emulate the 64-bit * 64-bit multiplication:
  72. //
  73. // p = u * v
  74. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  75. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  76. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  77. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  78. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  79. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  80. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  81. //
  82. // (Since Q might be larger than 2^32 - 1)
  83. //
  84. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  85. //
  86. // (Q_hi + H does not overflow a 64-bit int)
  87. //
  88. // = p_lo + 2^64 p_hi
  89. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  90. const std::uint64_t u_hi = x.f >> 32u;
  91. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  92. const std::uint64_t v_hi = y.f >> 32u;
  93. const std::uint64_t p0 = u_lo * v_lo;
  94. const std::uint64_t p1 = u_lo * v_hi;
  95. const std::uint64_t p2 = u_hi * v_lo;
  96. const std::uint64_t p3 = u_hi * v_hi;
  97. const std::uint64_t p0_hi = p0 >> 32u;
  98. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  99. const std::uint64_t p1_hi = p1 >> 32u;
  100. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  101. const std::uint64_t p2_hi = p2 >> 32u;
  102. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  103. // The full product might now be computed as
  104. //
  105. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  106. // p_lo = p0_lo + (Q << 32)
  107. //
  108. // But in this particular case here, the full p_lo is not required.
  109. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  110. // Q_hi + 1 does not overflow).
  111. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  112. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  113. return {h, x.e + y.e + 64};
  114. }
  115. /*!
  116. @brief normalize x such that the significand is >= 2^(q-1)
  117. @pre x.f != 0
  118. */
  119. static diyfp normalize(diyfp x) noexcept
  120. {
  121. JSON_ASSERT(x.f != 0);
  122. while ((x.f >> 63u) == 0)
  123. {
  124. x.f <<= 1u;
  125. x.e--;
  126. }
  127. return x;
  128. }
  129. /*!
  130. @brief normalize x such that the result has the exponent E
  131. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  132. */
  133. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  134. {
  135. const int delta = x.e - target_exponent;
  136. JSON_ASSERT(delta >= 0);
  137. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  138. return {x.f << delta, target_exponent};
  139. }
  140. };
  141. struct boundaries
  142. {
  143. diyfp w;
  144. diyfp minus;
  145. diyfp plus;
  146. };
  147. /*!
  148. Compute the (normalized) diyfp representing the input number 'value' and its
  149. boundaries.
  150. @pre value must be finite and positive
  151. */
  152. template<typename FloatType>
  153. boundaries compute_boundaries(FloatType value)
  154. {
  155. JSON_ASSERT(std::isfinite(value));
  156. JSON_ASSERT(value > 0);
  157. // Convert the IEEE representation into a diyfp.
  158. //
  159. // If v is denormal:
  160. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  161. // If v is normalized:
  162. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  163. static_assert(std::numeric_limits<FloatType>::is_iec559,
  164. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  165. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  166. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  167. constexpr int kMinExp = 1 - kBias;
  168. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  169. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  170. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  171. const std::uint64_t E = bits >> (kPrecision - 1);
  172. const std::uint64_t F = bits & (kHiddenBit - 1);
  173. const bool is_denormal = E == 0;
  174. const diyfp v = is_denormal
  175. ? diyfp(F, kMinExp)
  176. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  177. // Compute the boundaries m- and m+ of the floating-point value
  178. // v = f * 2^e.
  179. //
  180. // Determine v- and v+, the floating-point predecessor and successor if v,
  181. // respectively.
  182. //
  183. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  184. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  185. //
  186. // v+ = v + 2^e
  187. //
  188. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  189. // between m- and m+ round to v, regardless of how the input rounding
  190. // algorithm breaks ties.
  191. //
  192. // ---+-------------+-------------+-------------+-------------+--- (A)
  193. // v- m- v m+ v+
  194. //
  195. // -----------------+------+------+-------------+-------------+--- (B)
  196. // v- m- v m+ v+
  197. const bool lower_boundary_is_closer = F == 0 && E > 1;
  198. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  199. const diyfp m_minus = lower_boundary_is_closer
  200. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  201. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  202. // Determine the normalized w+ = m+.
  203. const diyfp w_plus = diyfp::normalize(m_plus);
  204. // Determine w- = m- such that e_(w-) = e_(w+).
  205. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  206. return {diyfp::normalize(v), w_minus, w_plus};
  207. }
  208. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  209. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  210. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  211. //
  212. // alpha <= e = e_c + e_w + q <= gamma
  213. //
  214. // or
  215. //
  216. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  217. // <= f_c * f_w * 2^gamma
  218. //
  219. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  220. //
  221. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  222. //
  223. // or
  224. //
  225. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  226. //
  227. // The choice of (alpha,gamma) determines the size of the table and the form of
  228. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  229. // in practice:
  230. //
  231. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  232. // processed independently: An integral part p1, and a fractional part p2:
  233. //
  234. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  235. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  236. // = p1 + p2 * 2^e
  237. //
  238. // The conversion of p1 into decimal form requires a series of divisions and
  239. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  240. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  241. // achieved by choosing
  242. //
  243. // -e >= 32 or e <= -32 := gamma
  244. //
  245. // In order to convert the fractional part
  246. //
  247. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  248. //
  249. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  250. // d[-i] are extracted in order:
  251. //
  252. // (10 * p2) div 2^-e = d[-1]
  253. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  254. //
  255. // The multiplication by 10 must not overflow. It is sufficient to choose
  256. //
  257. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  258. //
  259. // Since p2 = f mod 2^-e < 2^-e,
  260. //
  261. // -e <= 60 or e >= -60 := alpha
  262. constexpr int kAlpha = -60;
  263. constexpr int kGamma = -32;
  264. struct cached_power // c = f * 2^e ~= 10^k
  265. {
  266. std::uint64_t f;
  267. int e;
  268. int k;
  269. };
  270. /*!
  271. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  272. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  273. satisfies (Definition 3.2 from [1])
  274. alpha <= e_c + e + q <= gamma.
  275. */
  276. inline cached_power get_cached_power_for_binary_exponent(int e)
  277. {
  278. // Now
  279. //
  280. // alpha <= e_c + e + q <= gamma (1)
  281. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  282. //
  283. // and since the c's are normalized, 2^(q-1) <= f_c,
  284. //
  285. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  286. // ==> 2^(alpha - e - 1) <= c
  287. //
  288. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  289. //
  290. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  291. // = ceil( (alpha - e - 1) * log_10(2) )
  292. //
  293. // From the paper:
  294. // "In theory the result of the procedure could be wrong since c is rounded,
  295. // and the computation itself is approximated [...]. In practice, however,
  296. // this simple function is sufficient."
  297. //
  298. // For IEEE double precision floating-point numbers converted into
  299. // normalized diyfp's w = f * 2^e, with q = 64,
  300. //
  301. // e >= -1022 (min IEEE exponent)
  302. // -52 (p - 1)
  303. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  304. // -11 (normalize the diyfp)
  305. // = -1137
  306. //
  307. // and
  308. //
  309. // e <= +1023 (max IEEE exponent)
  310. // -52 (p - 1)
  311. // -11 (normalize the diyfp)
  312. // = 960
  313. //
  314. // This binary exponent range [-1137,960] results in a decimal exponent
  315. // range [-307,324]. One does not need to store a cached power for each
  316. // k in this range. For each such k it suffices to find a cached power
  317. // such that the exponent of the product lies in [alpha,gamma].
  318. // This implies that the difference of the decimal exponents of adjacent
  319. // table entries must be less than or equal to
  320. //
  321. // floor( (gamma - alpha) * log_10(2) ) = 8.
  322. //
  323. // (A smaller distance gamma-alpha would require a larger table.)
  324. // NB:
  325. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  326. constexpr int kCachedPowersMinDecExp = -300;
  327. constexpr int kCachedPowersDecStep = 8;
  328. static constexpr std::array<cached_power, 79> kCachedPowers =
  329. {
  330. {
  331. { 0xAB70FE17C79AC6CA, -1060, -300 },
  332. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  333. { 0xBE5691EF416BD60C, -1007, -284 },
  334. { 0x8DD01FAD907FFC3C, -980, -276 },
  335. { 0xD3515C2831559A83, -954, -268 },
  336. { 0x9D71AC8FADA6C9B5, -927, -260 },
  337. { 0xEA9C227723EE8BCB, -901, -252 },
  338. { 0xAECC49914078536D, -874, -244 },
  339. { 0x823C12795DB6CE57, -847, -236 },
  340. { 0xC21094364DFB5637, -821, -228 },
  341. { 0x9096EA6F3848984F, -794, -220 },
  342. { 0xD77485CB25823AC7, -768, -212 },
  343. { 0xA086CFCD97BF97F4, -741, -204 },
  344. { 0xEF340A98172AACE5, -715, -196 },
  345. { 0xB23867FB2A35B28E, -688, -188 },
  346. { 0x84C8D4DFD2C63F3B, -661, -180 },
  347. { 0xC5DD44271AD3CDBA, -635, -172 },
  348. { 0x936B9FCEBB25C996, -608, -164 },
  349. { 0xDBAC6C247D62A584, -582, -156 },
  350. { 0xA3AB66580D5FDAF6, -555, -148 },
  351. { 0xF3E2F893DEC3F126, -529, -140 },
  352. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  353. { 0x87625F056C7C4A8B, -475, -124 },
  354. { 0xC9BCFF6034C13053, -449, -116 },
  355. { 0x964E858C91BA2655, -422, -108 },
  356. { 0xDFF9772470297EBD, -396, -100 },
  357. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  358. { 0xF8A95FCF88747D94, -343, -84 },
  359. { 0xB94470938FA89BCF, -316, -76 },
  360. { 0x8A08F0F8BF0F156B, -289, -68 },
  361. { 0xCDB02555653131B6, -263, -60 },
  362. { 0x993FE2C6D07B7FAC, -236, -52 },
  363. { 0xE45C10C42A2B3B06, -210, -44 },
  364. { 0xAA242499697392D3, -183, -36 },
  365. { 0xFD87B5F28300CA0E, -157, -28 },
  366. { 0xBCE5086492111AEB, -130, -20 },
  367. { 0x8CBCCC096F5088CC, -103, -12 },
  368. { 0xD1B71758E219652C, -77, -4 },
  369. { 0x9C40000000000000, -50, 4 },
  370. { 0xE8D4A51000000000, -24, 12 },
  371. { 0xAD78EBC5AC620000, 3, 20 },
  372. { 0x813F3978F8940984, 30, 28 },
  373. { 0xC097CE7BC90715B3, 56, 36 },
  374. { 0x8F7E32CE7BEA5C70, 83, 44 },
  375. { 0xD5D238A4ABE98068, 109, 52 },
  376. { 0x9F4F2726179A2245, 136, 60 },
  377. { 0xED63A231D4C4FB27, 162, 68 },
  378. { 0xB0DE65388CC8ADA8, 189, 76 },
  379. { 0x83C7088E1AAB65DB, 216, 84 },
  380. { 0xC45D1DF942711D9A, 242, 92 },
  381. { 0x924D692CA61BE758, 269, 100 },
  382. { 0xDA01EE641A708DEA, 295, 108 },
  383. { 0xA26DA3999AEF774A, 322, 116 },
  384. { 0xF209787BB47D6B85, 348, 124 },
  385. { 0xB454E4A179DD1877, 375, 132 },
  386. { 0x865B86925B9BC5C2, 402, 140 },
  387. { 0xC83553C5C8965D3D, 428, 148 },
  388. { 0x952AB45CFA97A0B3, 455, 156 },
  389. { 0xDE469FBD99A05FE3, 481, 164 },
  390. { 0xA59BC234DB398C25, 508, 172 },
  391. { 0xF6C69A72A3989F5C, 534, 180 },
  392. { 0xB7DCBF5354E9BECE, 561, 188 },
  393. { 0x88FCF317F22241E2, 588, 196 },
  394. { 0xCC20CE9BD35C78A5, 614, 204 },
  395. { 0x98165AF37B2153DF, 641, 212 },
  396. { 0xE2A0B5DC971F303A, 667, 220 },
  397. { 0xA8D9D1535CE3B396, 694, 228 },
  398. { 0xFB9B7CD9A4A7443C, 720, 236 },
  399. { 0xBB764C4CA7A44410, 747, 244 },
  400. { 0x8BAB8EEFB6409C1A, 774, 252 },
  401. { 0xD01FEF10A657842C, 800, 260 },
  402. { 0x9B10A4E5E9913129, 827, 268 },
  403. { 0xE7109BFBA19C0C9D, 853, 276 },
  404. { 0xAC2820D9623BF429, 880, 284 },
  405. { 0x80444B5E7AA7CF85, 907, 292 },
  406. { 0xBF21E44003ACDD2D, 933, 300 },
  407. { 0x8E679C2F5E44FF8F, 960, 308 },
  408. { 0xD433179D9C8CB841, 986, 316 },
  409. { 0x9E19DB92B4E31BA9, 1013, 324 },
  410. }
  411. };
  412. // This computation gives exactly the same results for k as
  413. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  414. // for |e| <= 1500, but doesn't require floating-point operations.
  415. // NB: log_10(2) ~= 78913 / 2^18
  416. JSON_ASSERT(e >= -1500);
  417. JSON_ASSERT(e <= 1500);
  418. const int f = kAlpha - e - 1;
  419. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  420. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  421. JSON_ASSERT(index >= 0);
  422. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  423. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  424. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  425. JSON_ASSERT(kGamma >= cached.e + e + 64);
  426. return cached;
  427. }
  428. /*!
  429. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  430. For n == 0, returns 1 and sets pow10 := 1.
  431. */
  432. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  433. {
  434. // LCOV_EXCL_START
  435. if (n >= 1000000000)
  436. {
  437. pow10 = 1000000000;
  438. return 10;
  439. }
  440. // LCOV_EXCL_STOP
  441. if (n >= 100000000)
  442. {
  443. pow10 = 100000000;
  444. return 9;
  445. }
  446. if (n >= 10000000)
  447. {
  448. pow10 = 10000000;
  449. return 8;
  450. }
  451. if (n >= 1000000)
  452. {
  453. pow10 = 1000000;
  454. return 7;
  455. }
  456. if (n >= 100000)
  457. {
  458. pow10 = 100000;
  459. return 6;
  460. }
  461. if (n >= 10000)
  462. {
  463. pow10 = 10000;
  464. return 5;
  465. }
  466. if (n >= 1000)
  467. {
  468. pow10 = 1000;
  469. return 4;
  470. }
  471. if (n >= 100)
  472. {
  473. pow10 = 100;
  474. return 3;
  475. }
  476. if (n >= 10)
  477. {
  478. pow10 = 10;
  479. return 2;
  480. }
  481. pow10 = 1;
  482. return 1;
  483. }
  484. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  485. std::uint64_t rest, std::uint64_t ten_k)
  486. {
  487. JSON_ASSERT(len >= 1);
  488. JSON_ASSERT(dist <= delta);
  489. JSON_ASSERT(rest <= delta);
  490. JSON_ASSERT(ten_k > 0);
  491. // <--------------------------- delta ---->
  492. // <---- dist --------->
  493. // --------------[------------------+-------------------]--------------
  494. // M- w M+
  495. //
  496. // ten_k
  497. // <------>
  498. // <---- rest ---->
  499. // --------------[------------------+----+--------------]--------------
  500. // w V
  501. // = buf * 10^k
  502. //
  503. // ten_k represents a unit-in-the-last-place in the decimal representation
  504. // stored in buf.
  505. // Decrement buf by ten_k while this takes buf closer to w.
  506. // The tests are written in this order to avoid overflow in unsigned
  507. // integer arithmetic.
  508. while (rest < dist
  509. && delta - rest >= ten_k
  510. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  511. {
  512. JSON_ASSERT(buf[len - 1] != '0');
  513. buf[len - 1]--;
  514. rest += ten_k;
  515. }
  516. }
  517. /*!
  518. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  519. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  520. */
  521. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  522. diyfp M_minus, diyfp w, diyfp M_plus)
  523. {
  524. static_assert(kAlpha >= -60, "internal error");
  525. static_assert(kGamma <= -32, "internal error");
  526. // Generates the digits (and the exponent) of a decimal floating-point
  527. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  528. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  529. //
  530. // <--------------------------- delta ---->
  531. // <---- dist --------->
  532. // --------------[------------------+-------------------]--------------
  533. // M- w M+
  534. //
  535. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  536. // V is in [M-,M+].
  537. JSON_ASSERT(M_plus.e >= kAlpha);
  538. JSON_ASSERT(M_plus.e <= kGamma);
  539. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  540. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  541. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  542. //
  543. // M+ = f * 2^e
  544. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  545. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  546. // = p1 + p2 * 2^e
  547. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  548. auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
  549. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  550. // 1)
  551. //
  552. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  553. JSON_ASSERT(p1 > 0);
  554. std::uint32_t pow10{};
  555. const int k = find_largest_pow10(p1, pow10);
  556. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  557. //
  558. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  559. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  560. //
  561. // M+ = p1 + p2 * 2^e
  562. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  563. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  564. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  565. //
  566. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  567. //
  568. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  569. //
  570. // but stop as soon as
  571. //
  572. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  573. int n = k;
  574. while (n > 0)
  575. {
  576. // Invariants:
  577. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  578. // pow10 = 10^(n-1) <= p1 < 10^n
  579. //
  580. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  581. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  582. //
  583. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  584. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  585. //
  586. JSON_ASSERT(d <= 9);
  587. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  588. //
  589. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  590. //
  591. p1 = r;
  592. n--;
  593. //
  594. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  595. // pow10 = 10^n
  596. //
  597. // Now check if enough digits have been generated.
  598. // Compute
  599. //
  600. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  601. //
  602. // Note:
  603. // Since rest and delta share the same exponent e, it suffices to
  604. // compare the significands.
  605. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  606. if (rest <= delta)
  607. {
  608. // V = buffer * 10^n, with M- <= V <= M+.
  609. decimal_exponent += n;
  610. // We may now just stop. But instead look if the buffer could be
  611. // decremented to bring V closer to w.
  612. //
  613. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  614. // The rounding procedure works with diyfp's with an implicit
  615. // exponent of e.
  616. //
  617. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  618. //
  619. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  620. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  621. return;
  622. }
  623. pow10 /= 10;
  624. //
  625. // pow10 = 10^(n-1) <= p1 < 10^n
  626. // Invariants restored.
  627. }
  628. // 2)
  629. //
  630. // The digits of the integral part have been generated:
  631. //
  632. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  633. // = buffer + p2 * 2^e
  634. //
  635. // Now generate the digits of the fractional part p2 * 2^e.
  636. //
  637. // Note:
  638. // No decimal point is generated: the exponent is adjusted instead.
  639. //
  640. // p2 actually represents the fraction
  641. //
  642. // p2 * 2^e
  643. // = p2 / 2^-e
  644. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  645. //
  646. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  647. //
  648. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  649. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  650. //
  651. // using
  652. //
  653. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  654. // = ( d) * 2^-e + ( r)
  655. //
  656. // or
  657. // 10^m * p2 * 2^e = d + r * 2^e
  658. //
  659. // i.e.
  660. //
  661. // M+ = buffer + p2 * 2^e
  662. // = buffer + 10^-m * (d + r * 2^e)
  663. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  664. //
  665. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  666. JSON_ASSERT(p2 > delta);
  667. int m = 0;
  668. for (;;)
  669. {
  670. // Invariant:
  671. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  672. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  673. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  674. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  675. //
  676. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  677. p2 *= 10;
  678. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  679. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  680. //
  681. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  682. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  683. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  684. //
  685. JSON_ASSERT(d <= 9);
  686. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  687. //
  688. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  689. //
  690. p2 = r;
  691. m++;
  692. //
  693. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  694. // Invariant restored.
  695. // Check if enough digits have been generated.
  696. //
  697. // 10^-m * p2 * 2^e <= delta * 2^e
  698. // p2 * 2^e <= 10^m * delta * 2^e
  699. // p2 <= 10^m * delta
  700. delta *= 10;
  701. dist *= 10;
  702. if (p2 <= delta)
  703. {
  704. break;
  705. }
  706. }
  707. // V = buffer * 10^-m, with M- <= V <= M+.
  708. decimal_exponent -= m;
  709. // 1 ulp in the decimal representation is now 10^-m.
  710. // Since delta and dist are now scaled by 10^m, we need to do the
  711. // same with ulp in order to keep the units in sync.
  712. //
  713. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  714. //
  715. const std::uint64_t ten_m = one.f;
  716. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  717. // By construction this algorithm generates the shortest possible decimal
  718. // number (Loitsch, Theorem 6.2) which rounds back to w.
  719. // For an input number of precision p, at least
  720. //
  721. // N = 1 + ceil(p * log_10(2))
  722. //
  723. // decimal digits are sufficient to identify all binary floating-point
  724. // numbers (Matula, "In-and-Out conversions").
  725. // This implies that the algorithm does not produce more than N decimal
  726. // digits.
  727. //
  728. // N = 17 for p = 53 (IEEE double precision)
  729. // N = 9 for p = 24 (IEEE single precision)
  730. }
  731. /*!
  732. v = buf * 10^decimal_exponent
  733. len is the length of the buffer (number of decimal digits)
  734. The buffer must be large enough, i.e. >= max_digits10.
  735. */
  736. JSON_HEDLEY_NON_NULL(1)
  737. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  738. diyfp m_minus, diyfp v, diyfp m_plus)
  739. {
  740. JSON_ASSERT(m_plus.e == m_minus.e);
  741. JSON_ASSERT(m_plus.e == v.e);
  742. // --------(-----------------------+-----------------------)-------- (A)
  743. // m- v m+
  744. //
  745. // --------------------(-----------+-----------------------)-------- (B)
  746. // m- v m+
  747. //
  748. // First scale v (and m- and m+) such that the exponent is in the range
  749. // [alpha, gamma].
  750. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  751. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  752. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  753. const diyfp w = diyfp::mul(v, c_minus_k);
  754. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  755. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  756. // ----(---+---)---------------(---+---)---------------(---+---)----
  757. // w- w w+
  758. // = c*m- = c*v = c*m+
  759. //
  760. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  761. // w+ are now off by a small amount.
  762. // In fact:
  763. //
  764. // w - v * 10^k < 1 ulp
  765. //
  766. // To account for this inaccuracy, add resp. subtract 1 ulp.
  767. //
  768. // --------+---[---------------(---+---)---------------]---+--------
  769. // w- M- w M+ w+
  770. //
  771. // Now any number in [M-, M+] (bounds included) will round to w when input,
  772. // regardless of how the input rounding algorithm breaks ties.
  773. //
  774. // And digit_gen generates the shortest possible such number in [M-, M+].
  775. // Note that this does not mean that Grisu2 always generates the shortest
  776. // possible number in the interval (m-, m+).
  777. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  778. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  779. decimal_exponent = -cached.k; // = -(-k) = k
  780. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  781. }
  782. /*!
  783. v = buf * 10^decimal_exponent
  784. len is the length of the buffer (number of decimal digits)
  785. The buffer must be large enough, i.e. >= max_digits10.
  786. */
  787. template<typename FloatType>
  788. JSON_HEDLEY_NON_NULL(1)
  789. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  790. {
  791. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  792. "internal error: not enough precision");
  793. JSON_ASSERT(std::isfinite(value));
  794. JSON_ASSERT(value > 0);
  795. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  796. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  797. // decimal representations are not exactly "short".
  798. //
  799. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  800. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  801. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  802. // does.
  803. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  804. // representation using the corresponding std::from_chars function recovers value exactly". That
  805. // indicates that single precision floating-point numbers should be recovered using
  806. // 'std::strtof'.
  807. //
  808. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  809. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  810. // value is off by 1 ulp.
  811. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  812. const boundaries w = compute_boundaries(static_cast<double>(value));
  813. #else
  814. const boundaries w = compute_boundaries(value);
  815. #endif
  816. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  817. }
  818. /*!
  819. @brief appends a decimal representation of e to buf
  820. @return a pointer to the element following the exponent.
  821. @pre -1000 < e < 1000
  822. */
  823. JSON_HEDLEY_NON_NULL(1)
  824. JSON_HEDLEY_RETURNS_NON_NULL
  825. inline char* append_exponent(char* buf, int e)
  826. {
  827. JSON_ASSERT(e > -1000);
  828. JSON_ASSERT(e < 1000);
  829. if (e < 0)
  830. {
  831. e = -e;
  832. *buf++ = '-';
  833. }
  834. else
  835. {
  836. *buf++ = '+';
  837. }
  838. auto k = static_cast<std::uint32_t>(e);
  839. if (k < 10)
  840. {
  841. // Always print at least two digits in the exponent.
  842. // This is for compatibility with printf("%g").
  843. *buf++ = '0';
  844. *buf++ = static_cast<char>('0' + k);
  845. }
  846. else if (k < 100)
  847. {
  848. *buf++ = static_cast<char>('0' + k / 10);
  849. k %= 10;
  850. *buf++ = static_cast<char>('0' + k);
  851. }
  852. else
  853. {
  854. *buf++ = static_cast<char>('0' + k / 100);
  855. k %= 100;
  856. *buf++ = static_cast<char>('0' + k / 10);
  857. k %= 10;
  858. *buf++ = static_cast<char>('0' + k);
  859. }
  860. return buf;
  861. }
  862. /*!
  863. @brief prettify v = buf * 10^decimal_exponent
  864. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  865. notation. Otherwise it will be printed in exponential notation.
  866. @pre min_exp < 0
  867. @pre max_exp > 0
  868. */
  869. JSON_HEDLEY_NON_NULL(1)
  870. JSON_HEDLEY_RETURNS_NON_NULL
  871. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  872. int min_exp, int max_exp)
  873. {
  874. JSON_ASSERT(min_exp < 0);
  875. JSON_ASSERT(max_exp > 0);
  876. const int k = len;
  877. const int n = len + decimal_exponent;
  878. // v = buf * 10^(n-k)
  879. // k is the length of the buffer (number of decimal digits)
  880. // n is the position of the decimal point relative to the start of the buffer.
  881. if (k <= n && n <= max_exp)
  882. {
  883. // digits[000]
  884. // len <= max_exp + 2
  885. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  886. // Make it look like a floating-point number (#362, #378)
  887. buf[n + 0] = '.';
  888. buf[n + 1] = '0';
  889. return buf + (static_cast<size_t>(n) + 2);
  890. }
  891. if (0 < n && n <= max_exp)
  892. {
  893. // dig.its
  894. // len <= max_digits10 + 1
  895. JSON_ASSERT(k > n);
  896. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  897. buf[n] = '.';
  898. return buf + (static_cast<size_t>(k) + 1U);
  899. }
  900. if (min_exp < n && n <= 0)
  901. {
  902. // 0.[000]digits
  903. // len <= 2 + (-min_exp - 1) + max_digits10
  904. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  905. buf[0] = '0';
  906. buf[1] = '.';
  907. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  908. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  909. }
  910. if (k == 1)
  911. {
  912. // dE+123
  913. // len <= 1 + 5
  914. buf += 1;
  915. }
  916. else
  917. {
  918. // d.igitsE+123
  919. // len <= max_digits10 + 1 + 5
  920. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  921. buf[1] = '.';
  922. buf += 1 + static_cast<size_t>(k);
  923. }
  924. *buf++ = 'e';
  925. return append_exponent(buf, n - 1);
  926. }
  927. } // namespace dtoa_impl
  928. /*!
  929. @brief generates a decimal representation of the floating-point number value in [first, last).
  930. The format of the resulting decimal representation is similar to printf's %g
  931. format. Returns an iterator pointing past-the-end of the decimal representation.
  932. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  933. @note The buffer must be large enough.
  934. @note The result is NOT null-terminated.
  935. */
  936. template<typename FloatType>
  937. JSON_HEDLEY_NON_NULL(1, 2)
  938. JSON_HEDLEY_RETURNS_NON_NULL
  939. char* to_chars(char* first, const char* last, FloatType value)
  940. {
  941. static_cast<void>(last); // maybe unused - fix warning
  942. JSON_ASSERT(std::isfinite(value));
  943. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  944. if (std::signbit(value))
  945. {
  946. value = -value;
  947. *first++ = '-';
  948. }
  949. #ifdef __GNUC__
  950. #pragma GCC diagnostic push
  951. #pragma GCC diagnostic ignored "-Wfloat-equal"
  952. #endif
  953. if (value == 0) // +-0
  954. {
  955. *first++ = '0';
  956. // Make it look like a floating-point number (#362, #378)
  957. *first++ = '.';
  958. *first++ = '0';
  959. return first;
  960. }
  961. #ifdef __GNUC__
  962. #pragma GCC diagnostic pop
  963. #endif
  964. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  965. // Compute v = buffer * 10^decimal_exponent.
  966. // The decimal digits are stored in the buffer, which needs to be interpreted
  967. // as an unsigned decimal integer.
  968. // len is the length of the buffer, i.e. the number of decimal digits.
  969. int len = 0;
  970. int decimal_exponent = 0;
  971. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  972. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  973. // Format the buffer like printf("%.*g", prec, value)
  974. constexpr int kMinExp = -4;
  975. // Use digits10 here to increase compatibility with version 2.
  976. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  977. JSON_ASSERT(last - first >= kMaxExp + 2);
  978. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  979. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  980. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  981. }
  982. } // namespace detail
  983. NLOHMANN_JSON_NAMESPACE_END