FastDtoa.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // Copyright 2010 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. // Ported to Java from Mozilla's version of V8-dtoa by Hannes Wallnoefer.
  28. // The original revision was 67d1049b0bf9 from the mozilla-central tree.
  29. using System;
  30. using System.Diagnostics;
  31. namespace Jint.Native.Number.Dtoa
  32. {
  33. public class FastDtoa
  34. {
  35. // FastDtoa will produce at most kFastDtoaMaximalLength digits.
  36. public const int KFastDtoaMaximalLength = 17;
  37. // The minimal and maximal target exponent define the range of w's binary
  38. // exponent, where 'w' is the result of multiplying the input by a cached power
  39. // of ten.
  40. //
  41. // A different range might be chosen on a different platform, to optimize digit
  42. // generation, but a smaller range requires more powers of ten to be cached.
  43. private const int MinimalTargetExponent = -60;
  44. private const int MaximalTargetExponent = -32;
  45. // Adjusts the last digit of the generated number, and screens out generated
  46. // solutions that may be inaccurate. A solution may be inaccurate if it is
  47. // outside the safe interval, or if we ctannot prove that it is closer to the
  48. // input than a neighboring representation of the same length.
  49. //
  50. // Input: * buffer containing the digits of too_high / 10^kappa
  51. // * distance_too_high_w == (too_high - w).f() * unit
  52. // * unsafe_interval == (too_high - too_low).f() * unit
  53. // * rest = (too_high - buffer * 10^kappa).f() * unit
  54. // * ten_kappa = 10^kappa * unit
  55. // * unit = the common multiplier
  56. // Output: returns true if the buffer is guaranteed to contain the closest
  57. // representable number to the input.
  58. // Modifies the generated digits in the buffer to approach (round towards) w.
  59. private static bool RoundWeed(FastDtoaBuilder buffer,
  60. long distanceTooHighW,
  61. long unsafeInterval,
  62. long rest,
  63. long tenKappa,
  64. long unit)
  65. {
  66. long smallDistance = distanceTooHighW - unit;
  67. long bigDistance = distanceTooHighW + unit;
  68. // Let w_low = too_high - big_distance, and
  69. // w_high = too_high - small_distance.
  70. // Note: w_low < w < w_high
  71. //
  72. // The real w (* unit) must lie somewhere inside the interval
  73. // ]w_low; w_low[ (often written as "(w_low; w_low)")
  74. // Basically the buffer currently contains a number in the unsafe interval
  75. // ]too_low; too_high[ with too_low < w < too_high
  76. //
  77. // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  78. // ^v 1 unit ^ ^ ^ ^
  79. // boundary_high --------------------- . . . .
  80. // ^v 1 unit . . . .
  81. // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
  82. // . . ^ . .
  83. // . big_distance . . .
  84. // . . . . rest
  85. // small_distance . . . .
  86. // v . . . .
  87. // w_high - - - - - - - - - - - - - - - - - - . . . .
  88. // ^v 1 unit . . . .
  89. // w ---------------------------------------- . . . .
  90. // ^v 1 unit v . . .
  91. // w_low - - - - - - - - - - - - - - - - - - - - - . . .
  92. // . . v
  93. // buffer --------------------------------------------------+-------+--------
  94. // . .
  95. // safe_interval .
  96. // v .
  97. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - .
  98. // ^v 1 unit .
  99. // boundary_low ------------------------- unsafe_interval
  100. // ^v 1 unit v
  101. // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  102. //
  103. //
  104. // Note that the value of buffer could lie anywhere inside the range too_low
  105. // to too_high.
  106. //
  107. // boundary_low, boundary_high and w are approximations of the real boundaries
  108. // and v (the input number). They are guaranteed to be precise up to one unit.
  109. // In fact the error is guaranteed to be strictly less than one unit.
  110. //
  111. // Anything that lies outside the unsafe interval is guaranteed not to round
  112. // to v when read again.
  113. // Anything that lies inside the safe interval is guaranteed to round to v
  114. // when read again.
  115. // If the number inside the buffer lies inside the unsafe interval but not
  116. // inside the safe interval then we simply do not know and bail out (returning
  117. // false).
  118. //
  119. // Similarly we have to take into account the imprecision of 'w' when rounding
  120. // the buffer. If we have two potential representations we need to make sure
  121. // that the chosen one is closer to w_low and w_high since v can be anywhere
  122. // between them.
  123. //
  124. // By generating the digits of too_high we got the largest (closest to
  125. // too_high) buffer that is still in the unsafe interval. In the case where
  126. // w_high < buffer < too_high we try to decrement the buffer.
  127. // This way the buffer approaches (rounds towards) w.
  128. // There are 3 conditions that stop the decrementation process:
  129. // 1) the buffer is already below w_high
  130. // 2) decrementing the buffer would make it leave the unsafe interval
  131. // 3) decrementing the buffer would yield a number below w_high and farther
  132. // away than the current number. In other words:
  133. // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
  134. // Instead of using the buffer directly we use its distance to too_high.
  135. // Conceptually rest ~= too_high - buffer
  136. while (rest < smallDistance && // Negated condition 1
  137. unsafeInterval - rest >= tenKappa && // Negated condition 2
  138. (rest + tenKappa < smallDistance || // buffer{-1} > w_high
  139. smallDistance - rest >= rest + tenKappa - smallDistance))
  140. {
  141. buffer.DecreaseLast();
  142. rest += tenKappa;
  143. }
  144. // We have approached w+ as much as possible. We now test if approaching w-
  145. // would require changing the buffer. If yes, then we have two possible
  146. // representations close to w, but we cannot decide which one is closer.
  147. if (rest < bigDistance &&
  148. unsafeInterval - rest >= tenKappa &&
  149. (rest + tenKappa < bigDistance ||
  150. bigDistance - rest > rest + tenKappa - bigDistance))
  151. {
  152. return false;
  153. }
  154. // Weeding test.
  155. // The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
  156. // Since too_low = too_high - unsafe_interval this is equivalent to
  157. // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
  158. // Conceptually we have: rest ~= too_high - buffer
  159. return (2*unit <= rest) && (rest <= unsafeInterval - 4*unit);
  160. }
  161. private const int KTen4 = 10000;
  162. private const int KTen5 = 100000;
  163. private const int KTen6 = 1000000;
  164. private const int KTen7 = 10000000;
  165. private const int KTen8 = 100000000;
  166. private const int KTen9 = 1000000000;
  167. // Returns the biggest power of ten that is less than or equal than the given
  168. // number. We furthermore receive the maximum number of bits 'number' has.
  169. // If number_bits == 0 then 0^-1 is returned
  170. // The number of bits must be <= 32.
  171. // Precondition: (1 << number_bits) <= number < (1 << (number_bits + 1)).
  172. private static long BiggestPowerTen(int number,
  173. int numberBits)
  174. {
  175. int power, exponent;
  176. switch (numberBits)
  177. {
  178. case 32:
  179. case 31:
  180. case 30:
  181. if (KTen9 <= number)
  182. {
  183. power = KTen9;
  184. exponent = 9;
  185. break;
  186. } // else fallthrough
  187. goto case 29;
  188. case 29:
  189. case 28:
  190. case 27:
  191. if (KTen8 <= number)
  192. {
  193. power = KTen8;
  194. exponent = 8;
  195. break;
  196. } // else fallthrough
  197. goto case 26;
  198. case 26:
  199. case 25:
  200. case 24:
  201. if (KTen7 <= number)
  202. {
  203. power = KTen7;
  204. exponent = 7;
  205. break;
  206. } // else fallthrough
  207. goto case 23;
  208. case 23:
  209. case 22:
  210. case 21:
  211. case 20:
  212. if (KTen6 <= number)
  213. {
  214. power = KTen6;
  215. exponent = 6;
  216. break;
  217. } // else fallthrough
  218. goto case 19;
  219. case 19:
  220. case 18:
  221. case 17:
  222. if (KTen5 <= number)
  223. {
  224. power = KTen5;
  225. exponent = 5;
  226. break;
  227. } // else fallthrough
  228. goto case 16;
  229. case 16:
  230. case 15:
  231. case 14:
  232. if (KTen4 <= number)
  233. {
  234. power = KTen4;
  235. exponent = 4;
  236. break;
  237. } // else fallthrough
  238. goto case 13;
  239. case 13:
  240. case 12:
  241. case 11:
  242. case 10:
  243. if (1000 <= number)
  244. {
  245. power = 1000;
  246. exponent = 3;
  247. break;
  248. } // else fallthrough
  249. goto case 9;
  250. case 9:
  251. case 8:
  252. case 7:
  253. if (100 <= number)
  254. {
  255. power = 100;
  256. exponent = 2;
  257. break;
  258. } // else fallthrough
  259. goto case 6;
  260. case 6:
  261. case 5:
  262. case 4:
  263. if (10 <= number)
  264. {
  265. power = 10;
  266. exponent = 1;
  267. break;
  268. } // else fallthrough
  269. goto case 3;
  270. case 3:
  271. case 2:
  272. case 1:
  273. if (1 <= number)
  274. {
  275. power = 1;
  276. exponent = 0;
  277. break;
  278. } // else fallthrough
  279. goto case 0;
  280. case 0:
  281. power = 0;
  282. exponent = -1;
  283. break;
  284. default:
  285. // Following assignments are here to silence compiler warnings.
  286. power = 0;
  287. exponent = 0;
  288. // UNREACHABLE();
  289. break;
  290. }
  291. return ((long) power << 32) | (0xffffffffL & exponent);
  292. }
  293. // Generates the digits of input number w.
  294. // w is a floating-point number (DiyFp), consisting of a significand and an
  295. // exponent. Its exponent is bounded by minimal_target_exponent and
  296. // maximal_target_exponent.
  297. // Hence -60 <= w.e() <= -32.
  298. //
  299. // Returns false if it fails, in which case the generated digits in the buffer
  300. // should not be used.
  301. // Preconditions:
  302. // * low, w and high are correct up to 1 ulp (unit in the last place). That
  303. // is, their error must be less that a unit of their last digits.
  304. // * low.e() == w.e() == high.e()
  305. // * low < w < high, and taking into account their error: low~ <= high~
  306. // * minimal_target_exponent <= w.e() <= maximal_target_exponent
  307. // Postconditions: returns false if procedure fails.
  308. // otherwise:
  309. // * buffer is not null-terminated, but len contains the number of digits.
  310. // * buffer contains the shortest possible decimal digit-sequence
  311. // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
  312. // correct values of low and high (without their error).
  313. // * if more than one decimal representation gives the minimal number of
  314. // decimal digits then the one closest to W (where W is the correct value
  315. // of w) is chosen.
  316. // Remark: this procedure takes into account the imprecision of its input
  317. // numbers. If the precision is not enough to guarantee all the postconditions
  318. // then false is returned. This usually happens rarely (~0.5%).
  319. //
  320. // Say, for the sake of example, that
  321. // w.e() == -48, and w.f() == 0x1234567890abcdef
  322. // w's value can be computed by w.f() * 2^w.e()
  323. // We can obtain w's integral digits by simply shifting w.f() by -w.e().
  324. // -> w's integral part is 0x1234
  325. // w's fractional part is therefore 0x567890abcdef.
  326. // Printing w's integral part is easy (simply print 0x1234 in decimal).
  327. // In order to print its fraction we repeatedly multiply the fraction by 10 and
  328. // get each digit. Example the first digit after the point would be computed by
  329. // (0x567890abcdef * 10) >> 48. -> 3
  330. // The whole thing becomes slightly more complicated because we want to stop
  331. // once we have enough digits. That is, once the digits inside the buffer
  332. // represent 'w' we can stop. Everything inside the interval low - high
  333. // represents w. However we have to pay attention to low, high and w's
  334. // imprecision.
  335. private static bool DigitGen(DiyFp low,
  336. DiyFp w,
  337. DiyFp high,
  338. FastDtoaBuilder buffer,
  339. int mk)
  340. {
  341. // low, w and high are imprecise, but by less than one ulp (unit in the last
  342. // place).
  343. // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
  344. // the new numbers are outside of the interval we want the final
  345. // representation to lie in.
  346. // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
  347. // numbers that are certain to lie in the interval. We will use this fact
  348. // later on.
  349. // We will now start by generating the digits within the uncertain
  350. // interval. Later we will weed out representations that lie outside the safe
  351. // interval and thus _might_ lie outside the correct interval.
  352. long unit = 1;
  353. var tooLow = new DiyFp(low.F - unit, low.E);
  354. var tooHigh = new DiyFp(high.F + unit, high.E);
  355. // too_low and too_high are guaranteed to lie outside the interval we want the
  356. // generated number in.
  357. var unsafeInterval = DiyFp.Minus(tooHigh, tooLow);
  358. // We now cut the input number into two parts: the integral digits and the
  359. // fractionals. We will not write any decimal separator though, but adapt
  360. // kappa instead.
  361. // Reminder: we are currently computing the digits (stored inside the buffer)
  362. // such that: too_low < buffer * 10^kappa < too_high
  363. // We use too_high for the digit_generation and stop as soon as possible.
  364. // If we stop early we effectively round down.
  365. var one = new DiyFp(1L << -w.E, w.E);
  366. // Division by one is a shift.
  367. var integrals = (int) (tooHigh.F.UnsignedShift(-one.E) & 0xffffffffL);
  368. // Modulo by one is an and.
  369. long fractionals = tooHigh.F & (one.F - 1);
  370. long result = BiggestPowerTen(integrals, DiyFp.KSignificandSize - (-one.E));
  371. var divider = (int) (result.UnsignedShift(32) & 0xffffffffL);
  372. var dividerExponent = (int) (result & 0xffffffffL);
  373. var kappa = dividerExponent + 1;
  374. // Loop invariant: buffer = too_high / 10^kappa (integer division)
  375. // The invariant holds for the first iteration: kappa has been initialized
  376. // with the divider exponent + 1. And the divider is the biggest power of ten
  377. // that is smaller than integrals.
  378. while (kappa > 0)
  379. {
  380. int digit = integrals/divider;
  381. buffer.Append((char) ('0' + digit));
  382. integrals %= divider;
  383. kappa--;
  384. // Note that kappa now equals the exponent of the divider and that the
  385. // invariant thus holds again.
  386. long rest =
  387. ((long) integrals << -one.E) + fractionals;
  388. // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
  389. // Reminder: unsafe_interval.e() == one.e()
  390. if (rest < unsafeInterval.F)
  391. {
  392. // Rounding down (by not emitting the remaining digits) yields a number
  393. // that lies within the unsafe interval.
  394. buffer.Point = buffer.End - mk + kappa;
  395. return RoundWeed(buffer, DiyFp.Minus(tooHigh, w).F,
  396. unsafeInterval.F, rest,
  397. (long) divider << -one.E, unit);
  398. }
  399. divider /= 10;
  400. }
  401. // The integrals have been generated. We are at the point of the decimal
  402. // separator. In the following loop we simply multiply the remaining digits by
  403. // 10 and divide by one. We just need to pay attention to multiply associated
  404. // data (like the interval or 'unit'), too.
  405. // Instead of multiplying by 10 we multiply by 5 (cheaper operation) and
  406. // increase its (imaginary) exponent. At the same time we decrease the
  407. // divider's (one's) exponent and shift its significand.
  408. // Basically, if fractionals was a DiyFp (with fractionals.e == one.e):
  409. // fractionals.f *= 10;
  410. // fractionals.f >>= 1; fractionals.e++; // value remains unchanged.
  411. // one.f >>= 1; one.e++; // value remains unchanged.
  412. // and we have again fractionals.e == one.e which allows us to divide
  413. // fractionals.f() by one.f()
  414. // We simply combine the *= 10 and the >>= 1.
  415. while (true)
  416. {
  417. fractionals *= 5;
  418. unit *= 5;
  419. unsafeInterval.F = unsafeInterval.F*5;
  420. unsafeInterval.E = unsafeInterval.E + 1; // Will be optimized out.
  421. one.F = one.F.UnsignedShift(1);
  422. one.E = one.E + 1;
  423. // Integer division by one.
  424. var digit = (int) ((fractionals.UnsignedShift(-one.E)) & 0xffffffffL);
  425. buffer.Append((char) ('0' + digit));
  426. fractionals &= one.F - 1; // Modulo by one.
  427. kappa--;
  428. if (fractionals < unsafeInterval.F)
  429. {
  430. buffer.Point = buffer.End - mk + kappa;
  431. return RoundWeed(buffer, DiyFp.Minus(tooHigh, w).F*unit,
  432. unsafeInterval.F, fractionals, one.F, unit);
  433. }
  434. }
  435. }
  436. // Provides a decimal representation of v.
  437. // Returns true if it succeeds, otherwise the result cannot be trusted.
  438. // There will be *length digits inside the buffer (not null-terminated).
  439. // If the function returns true then
  440. // v == (double) (buffer * 10^decimal_exponent).
  441. // The digits in the buffer are the shortest representation possible: no
  442. // 0.09999999999999999 instead of 0.1. The shorter representation will even be
  443. // chosen even if the longer one would be closer to v.
  444. // The last digit will be closest to the actual v. That is, even if several
  445. // digits might correctly yield 'v' when read again, the closest will be
  446. // computed.
  447. private static bool Grisu3(double v, FastDtoaBuilder buffer)
  448. {
  449. long bits = BitConverter.DoubleToInt64Bits(v);
  450. DiyFp w = DoubleHelper.AsNormalizedDiyFp(bits);
  451. // boundary_minus and boundary_plus are the boundaries between v and its
  452. // closest floating-point neighbors. Any number strictly between
  453. // boundary_minus and boundary_plus will round to v when convert to a double.
  454. // Grisu3 will never output representations that lie exactly on a boundary.
  455. DiyFp boundaryMinus = new DiyFp(), boundaryPlus = new DiyFp();
  456. DoubleHelper.NormalizedBoundaries(bits, boundaryMinus, boundaryPlus);
  457. Debug.Assert(boundaryPlus.E == w.E);
  458. var tenMk = new DiyFp(); // Cached power of ten: 10^-k
  459. int mk = CachedPowers.GetCachedPower(w.E + DiyFp.KSignificandSize,
  460. MinimalTargetExponent, MaximalTargetExponent, tenMk);
  461. Debug.Assert(MinimalTargetExponent <= w.E + tenMk.E +
  462. DiyFp.KSignificandSize &&
  463. MaximalTargetExponent >= w.E + tenMk.E +
  464. DiyFp.KSignificandSize);
  465. // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
  466. // 64 bit significand and ten_mk is thus only precise up to 64 bits.
  467. // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
  468. // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
  469. // off by a small amount.
  470. // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
  471. // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
  472. // (f-1) * 2^e < w*10^k < (f+1) * 2^e
  473. DiyFp scaledW = DiyFp.Times(w, tenMk);
  474. Debug.Assert(scaledW.E ==
  475. boundaryPlus.E + tenMk.E + DiyFp.KSignificandSize);
  476. // In theory it would be possible to avoid some recomputations by computing
  477. // the difference between w and boundary_minus/plus (a power of 2) and to
  478. // compute scaled_boundary_minus/plus by subtracting/adding from
  479. // scaled_w. However the code becomes much less readable and the speed
  480. // enhancements are not terriffic.
  481. DiyFp scaledBoundaryMinus = DiyFp.Times(boundaryMinus, tenMk);
  482. DiyFp scaledBoundaryPlus = DiyFp.Times(boundaryPlus, tenMk);
  483. // DigitGen will generate the digits of scaled_w. Therefore we have
  484. // v == (double) (scaled_w * 10^-mk).
  485. // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
  486. // integer than it will be updated. For instance if scaled_w == 1.23 then
  487. // the buffer will be filled with "123" und the decimal_exponent will be
  488. // decreased by 2.
  489. return DigitGen(scaledBoundaryMinus, scaledW, scaledBoundaryPlus, buffer, mk);
  490. }
  491. public static bool Dtoa(double v, FastDtoaBuilder buffer)
  492. {
  493. Debug.Assert(v > 0);
  494. Debug.Assert(!Double.IsNaN(v));
  495. Debug.Assert(!Double.IsInfinity(v));
  496. return Grisu3(v, buffer);
  497. }
  498. public static string NumberToString(double v)
  499. {
  500. var buffer = new FastDtoaBuilder();
  501. return NumberToString(v, buffer) ? buffer.Format() : null;
  502. }
  503. public static bool NumberToString(double v, FastDtoaBuilder buffer)
  504. {
  505. buffer.Reset();
  506. if (v < 0)
  507. {
  508. buffer.Append('-');
  509. v = -v;
  510. }
  511. return Dtoa(v, buffer);
  512. }
  513. }
  514. }