]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
1 | /*! |
2 | ||
3 | Floating-point number to decimal conversion routines. | |
4 | ||
5 | # Problem statement | |
6 | ||
7 | We are given the floating-point number `v = f * 2^e` with an integer `f`, | |
8 | and its bounds `minus` and `plus` such that any number between `v - minus` and | |
9 | `v + plus` will be rounded to `v`. For the simplicity we assume that | |
10 | this range is exclusive. Then we would like to get the unique decimal | |
11 | representation `V = 0.d[0..n-1] * 10^k` such that: | |
12 | ||
13 | - `d[0]` is non-zero. | |
14 | ||
15 | - It's correctly rounded when parsed back: `v - minus < V < v + plus`. | |
0731742a | 16 | Furthermore it is shortest such one, i.e., there is no representation |
d9579d0f AL |
17 | with less than `n` digits that is correctly rounded. |
18 | ||
19 | - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Note that | |
20 | there might be two representations satisfying this uniqueness requirement, | |
21 | in which case some tie-breaking mechanism is used. | |
22 | ||
23 | We will call this mode of operation as to the *shortest* mode. This mode is used | |
24 | when there is no additional constraint, and can be thought as a "natural" mode | |
25 | as it matches the ordinary intuition (it at least prints `0.1f32` as "0.1"). | |
26 | ||
27 | We have two more modes of operation closely related to each other. In these modes | |
28 | we are given either the number of significant digits `n` or the last-digit | |
29 | limitation `limit` (which determines the actual `n`), and we would like to get | |
30 | the representation `V = 0.d[0..n-1] * 10^k` such that: | |
31 | ||
32 | - `d[0]` is non-zero, unless `n` was zero in which case only `k` is returned. | |
33 | ||
34 | - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Again, | |
35 | there might be some tie-breaking mechanism. | |
36 | ||
37 | When `limit` is given but not `n`, we set `n` such that `k - n = limit` | |
38 | so that the last digit `d[n-1]` is scaled by `10^(k-n) = 10^limit`. | |
39 | If such `n` is negative, we clip it to zero so that we will only get `k`. | |
40 | We are also limited by the supplied buffer. This limitation is used to print | |
41 | the number up to given number of fractional digits without knowing | |
42 | the correct `k` beforehand. | |
43 | ||
44 | We will call the mode of operation requiring `n` as to the *exact* mode, | |
45 | and one requiring `limit` as to the *fixed* mode. The exact mode is a subset of | |
46 | the fixed mode: the sufficiently large last-digit limitation will eventually fill | |
47 | the supplied buffer and let the algorithm to return. | |
48 | ||
49 | # Implementation overview | |
50 | ||
51 | It is easy to get the floating point printing correct but slow (Russ Cox has | |
52 | [demonstrated](http://research.swtch.com/ftoa) how it's easy), or incorrect but | |
53 | fast (naïve division and modulo). But it is surprisingly hard to print | |
54 | floating point numbers correctly *and* efficiently. | |
55 | ||
56 | There are two classes of algorithms widely known to be correct. | |
57 | ||
58 | - The "Dragon" family of algorithm is first described by Guy L. Steele Jr. and | |
59 | Jon L. White. They rely on the fixed-size big integer for their correctness. | |
60 | A slight improvement was found later, which is posthumously described by | |
61 | Robert G. Burger and R. Kent Dybvig. David Gay's `dtoa.c` routine is | |
62 | a popular implementation of this strategy. | |
63 | ||
64 | - The "Grisu" family of algorithm is first described by Florian Loitsch. | |
65 | They use very cheap integer-only procedure to determine the close-to-correct | |
66 | representation which is at least guaranteed to be shortest. The variant, | |
67 | Grisu3, actively detects if the resulting representation is incorrect. | |
68 | ||
69 | We implement both algorithms with necessary tweaks to suit our requirements. | |
70 | In particular, published literatures are short of the actual implementation | |
71 | difficulties like how to avoid arithmetic overflows. Each implementation, | |
72 | available in `strategy::dragon` and `strategy::grisu` respectively, | |
73 | extensively describes all necessary justifications and many proofs for them. | |
74 | (It is still difficult to follow though. You have been warned.) | |
75 | ||
76 | Both implementations expose two public functions: | |
77 | ||
78 | - `format_shortest(decoded, buf)`, which always needs at least | |
79 | `MAX_SIG_DIGITS` digits of buffer. Implements the shortest mode. | |
80 | ||
81 | - `format_exact(decoded, buf, limit)`, which accepts as small as | |
82 | one digit of buffer. Implements exact and fixed modes. | |
83 | ||
84 | They try to fill the `u8` buffer with digits and returns the number of digits | |
85 | written and the exponent `k`. They are total for all finite `f32` and `f64` | |
86 | inputs (Grisu internally falls back to Dragon if necessary). | |
87 | ||
88 | The rendered digits are formatted into the actual string form with | |
89 | four functions: | |
90 | ||
91 | - `to_shortest_str` prints the shortest representation, which can be padded by | |
92 | zeroes to make *at least* given number of fractional digits. | |
93 | ||
94 | - `to_shortest_exp_str` prints the shortest representation, which can be | |
95 | padded by zeroes when its exponent is in the specified ranges, | |
96 | or can be printed in the exponential form such as `1.23e45`. | |
97 | ||
98 | - `to_exact_exp_str` prints the exact representation with given number of | |
99 | digits in the exponential form. | |
100 | ||
101 | - `to_exact_fixed_str` prints the fixed representation with *exactly* | |
102 | given number of fractional digits. | |
103 | ||
104 | They all return a slice of preallocated `Part` array, which corresponds to | |
105 | the individual part of strings: a fixed string, a part of rendered digits, | |
106 | a number of zeroes or a small (`u16`) number. The caller is expected to | |
107 | provide a large enough buffer and `Part` array, and to assemble the final | |
108 | string from resulting `Part`s itself. | |
109 | ||
110 | All algorithms and formatting functions are accompanied by extensive tests | |
cc61c64b | 111 | in `coretests::num::flt2dec` module. It also shows how to use individual |
d9579d0f AL |
112 | functions. |
113 | ||
114 | */ | |
115 | ||
116 | // while this is extensively documented, this is in principle private which is | |
117 | // only made public for testing. do not expose us. | |
118 | #![doc(hidden)] | |
60c5eb7d XL |
119 | #![unstable( |
120 | feature = "flt2dec", | |
121 | reason = "internal routines only exposed for testing", | |
122 | issue = "0" | |
123 | )] | |
d9579d0f | 124 | |
60c5eb7d | 125 | pub use self::decoder::{decode, DecodableFloat, Decoded, FullDecoded}; |
48663c56 | 126 | use crate::i16; |
d9579d0f | 127 | |
d9579d0f | 128 | pub mod decoder; |
60c5eb7d | 129 | pub mod estimator; |
d9579d0f AL |
130 | |
131 | /// Digit-generation algorithms. | |
132 | pub mod strategy { | |
133 | pub mod dragon; | |
134 | pub mod grisu; | |
135 | } | |
136 | ||
137 | /// The minimum size of buffer necessary for the shortest mode. | |
138 | /// | |
139 | /// It is a bit non-trivial to derive, but this is one plus the maximal number of | |
140 | /// significant decimal digits from formatting algorithms with the shortest result. | |
141 | /// The exact formula is `ceil(# bits in mantissa * log_10 2 + 1)`. | |
142 | pub const MAX_SIG_DIGITS: usize = 17; | |
143 | ||
144 | /// When `d[..n]` contains decimal digits, increase the last digit and propagate carry. | |
145 | /// Returns a next digit when it causes the length change. | |
146 | #[doc(hidden)] | |
147 | pub fn round_up(d: &mut [u8], n: usize) -> Option<u8> { | |
148 | match d[..n].iter().rposition(|&c| c != b'9') { | |
60c5eb7d XL |
149 | Some(i) => { |
150 | // d[i+1..n] is all nines | |
d9579d0f | 151 | d[i] += 1; |
60c5eb7d XL |
152 | for j in i + 1..n { |
153 | d[j] = b'0'; | |
154 | } | |
d9579d0f AL |
155 | None |
156 | } | |
60c5eb7d XL |
157 | None if n > 0 => { |
158 | // 999..999 rounds to 1000..000 with an increased exponent | |
d9579d0f | 159 | d[0] = b'1'; |
60c5eb7d XL |
160 | for j in 1..n { |
161 | d[j] = b'0'; | |
162 | } | |
d9579d0f AL |
163 | Some(b'0') |
164 | } | |
60c5eb7d XL |
165 | None => { |
166 | // an empty buffer rounds up (a bit strange but reasonable) | |
d9579d0f AL |
167 | Some(b'1') |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | /// Formatted parts. | |
173 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | |
174 | pub enum Part<'a> { | |
175 | /// Given number of zero digits. | |
176 | Zero(usize), | |
177 | /// A literal number up to 5 digits. | |
178 | Num(u16), | |
179 | /// A verbatim copy of given bytes. | |
180 | Copy(&'a [u8]), | |
181 | } | |
182 | ||
183 | impl<'a> Part<'a> { | |
184 | /// Returns the exact byte length of given part. | |
185 | pub fn len(&self) -> usize { | |
186 | match *self { | |
187 | Part::Zero(nzeroes) => nzeroes, | |
60c5eb7d XL |
188 | Part::Num(v) => { |
189 | if v < 1_000 { | |
190 | if v < 10 { | |
191 | 1 | |
192 | } else if v < 100 { | |
193 | 2 | |
194 | } else { | |
195 | 3 | |
196 | } | |
197 | } else { | |
198 | if v < 10_000 { 4 } else { 5 } | |
199 | } | |
200 | } | |
d9579d0f AL |
201 | Part::Copy(buf) => buf.len(), |
202 | } | |
203 | } | |
204 | ||
205 | /// Writes a part into the supplied buffer. | |
206 | /// Returns the number of written bytes, or `None` if the buffer is not enough. | |
207 | /// (It may still leave partially written bytes in the buffer; do not rely on that.) | |
208 | pub fn write(&self, out: &mut [u8]) -> Option<usize> { | |
209 | let len = self.len(); | |
210 | if out.len() >= len { | |
211 | match *self { | |
212 | Part::Zero(nzeroes) => { | |
60c5eb7d XL |
213 | for c in &mut out[..nzeroes] { |
214 | *c = b'0'; | |
215 | } | |
d9579d0f AL |
216 | } |
217 | Part::Num(mut v) => { | |
218 | for c in out[..len].iter_mut().rev() { | |
219 | *c = b'0' + (v % 10) as u8; | |
220 | v /= 10; | |
221 | } | |
222 | } | |
223 | Part::Copy(buf) => { | |
7453a54e | 224 | out[..buf.len()].copy_from_slice(buf); |
d9579d0f AL |
225 | } |
226 | } | |
227 | Some(len) | |
228 | } else { | |
229 | None | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | /// Formatted result containing one or more parts. | |
235 | /// This can be written to the byte buffer or converted to the allocated string. | |
54a0048b | 236 | #[allow(missing_debug_implementations)] |
d9579d0f AL |
237 | #[derive(Clone)] |
238 | pub struct Formatted<'a> { | |
239 | /// A byte slice representing a sign, either `""`, `"-"` or `"+"`. | |
240 | pub sign: &'static [u8], | |
241 | /// Formatted parts to be rendered after a sign and optional zero padding. | |
242 | pub parts: &'a [Part<'a>], | |
243 | } | |
244 | ||
245 | impl<'a> Formatted<'a> { | |
246 | /// Returns the exact byte length of combined formatted result. | |
247 | pub fn len(&self) -> usize { | |
248 | let mut len = self.sign.len(); | |
249 | for part in self.parts { | |
250 | len += part.len(); | |
251 | } | |
252 | len | |
253 | } | |
254 | ||
255 | /// Writes all formatted parts into the supplied buffer. | |
256 | /// Returns the number of written bytes, or `None` if the buffer is not enough. | |
257 | /// (It may still leave partially written bytes in the buffer; do not rely on that.) | |
258 | pub fn write(&self, out: &mut [u8]) -> Option<usize> { | |
60c5eb7d XL |
259 | if out.len() < self.sign.len() { |
260 | return None; | |
261 | } | |
7453a54e | 262 | out[..self.sign.len()].copy_from_slice(self.sign); |
d9579d0f AL |
263 | |
264 | let mut written = self.sign.len(); | |
265 | for part in self.parts { | |
532ac7d7 XL |
266 | let len = part.write(&mut out[written..])?; |
267 | written += len; | |
d9579d0f AL |
268 | } |
269 | Some(written) | |
270 | } | |
271 | } | |
272 | ||
273 | /// Formats given decimal digits `0.<...buf...> * 10^exp` into the decimal form | |
274 | /// with at least given number of fractional digits. The result is stored to | |
275 | /// the supplied parts array and a slice of written parts is returned. | |
276 | /// | |
277 | /// `frac_digits` can be less than the number of actual fractional digits in `buf`; | |
278 | /// it will be ignored and full digits will be printed. It is only used to print | |
279 | /// additional zeroes after rendered digits. Thus `frac_digits` of 0 means that | |
280 | /// it will only print given digits and nothing else. | |
60c5eb7d XL |
281 | fn digits_to_dec_str<'a>( |
282 | buf: &'a [u8], | |
283 | exp: i16, | |
284 | frac_digits: usize, | |
285 | parts: &'a mut [Part<'a>], | |
286 | ) -> &'a [Part<'a>] { | |
d9579d0f AL |
287 | assert!(!buf.is_empty()); |
288 | assert!(buf[0] > b'0'); | |
289 | assert!(parts.len() >= 4); | |
290 | ||
291 | // if there is the restriction on the last digit position, `buf` is assumed to be | |
292 | // left-padded with the virtual zeroes. the number of virtual zeroes, `nzeroes`, | |
293 | // equals to `max(0, exp + frac_digits - buf.len())`, so that the position of | |
294 | // the last digit `exp - buf.len() - nzeroes` is no more than `-frac_digits`: | |
295 | // | |
296 | // |<-virtual->| | |
297 | // |<---- buf ---->| zeroes | exp | |
298 | // 0. 1 2 3 4 5 6 7 8 9 _ _ _ _ _ _ x 10 | |
299 | // | | | | |
300 | // 10^exp 10^(exp-buf.len()) 10^(exp-buf.len()-nzeroes) | |
301 | // | |
302 | // `nzeroes` is individually calculated for each case in order to avoid overflow. | |
303 | ||
304 | if exp <= 0 { | |
305 | // the decimal point is before rendered digits: [0.][000...000][1234][____] | |
306 | let minus_exp = -(exp as i32) as usize; | |
307 | parts[0] = Part::Copy(b"0."); | |
308 | parts[1] = Part::Zero(minus_exp); | |
309 | parts[2] = Part::Copy(buf); | |
310 | if frac_digits > buf.len() && frac_digits - buf.len() > minus_exp { | |
311 | parts[3] = Part::Zero((frac_digits - buf.len()) - minus_exp); | |
312 | &parts[..4] | |
313 | } else { | |
314 | &parts[..3] | |
315 | } | |
316 | } else { | |
317 | let exp = exp as usize; | |
318 | if exp < buf.len() { | |
319 | // the decimal point is inside rendered digits: [12][.][34][____] | |
320 | parts[0] = Part::Copy(&buf[..exp]); | |
321 | parts[1] = Part::Copy(b"."); | |
322 | parts[2] = Part::Copy(&buf[exp..]); | |
323 | if frac_digits > buf.len() - exp { | |
324 | parts[3] = Part::Zero(frac_digits - (buf.len() - exp)); | |
325 | &parts[..4] | |
326 | } else { | |
327 | &parts[..3] | |
328 | } | |
329 | } else { | |
330 | // the decimal point is after rendered digits: [1234][____0000] or [1234][__][.][__]. | |
331 | parts[0] = Part::Copy(buf); | |
332 | parts[1] = Part::Zero(exp - buf.len()); | |
333 | if frac_digits > 0 { | |
334 | parts[2] = Part::Copy(b"."); | |
335 | parts[3] = Part::Zero(frac_digits); | |
336 | &parts[..4] | |
337 | } else { | |
338 | &parts[..2] | |
339 | } | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
532ac7d7 XL |
344 | /// Formats the given decimal digits `0.<...buf...> * 10^exp` into the exponential |
345 | /// form with at least the given number of significant digits. When `upper` is `true`, | |
d9579d0f AL |
346 | /// the exponent will be prefixed by `E`; otherwise that's `e`. The result is |
347 | /// stored to the supplied parts array and a slice of written parts is returned. | |
348 | /// | |
349 | /// `min_digits` can be less than the number of actual significant digits in `buf`; | |
350 | /// it will be ignored and full digits will be printed. It is only used to print | |
532ac7d7 XL |
351 | /// additional zeroes after rendered digits. Thus, `min_digits == 0` means that |
352 | /// it will only print the given digits and nothing else. | |
60c5eb7d XL |
353 | fn digits_to_exp_str<'a>( |
354 | buf: &'a [u8], | |
355 | exp: i16, | |
356 | min_ndigits: usize, | |
357 | upper: bool, | |
358 | parts: &'a mut [Part<'a>], | |
359 | ) -> &'a [Part<'a>] { | |
d9579d0f AL |
360 | assert!(!buf.is_empty()); |
361 | assert!(buf[0] > b'0'); | |
362 | assert!(parts.len() >= 6); | |
363 | ||
364 | let mut n = 0; | |
365 | ||
366 | parts[n] = Part::Copy(&buf[..1]); | |
367 | n += 1; | |
368 | ||
369 | if buf.len() > 1 || min_ndigits > 1 { | |
370 | parts[n] = Part::Copy(b"."); | |
371 | parts[n + 1] = Part::Copy(&buf[1..]); | |
372 | n += 2; | |
373 | if min_ndigits > buf.len() { | |
374 | parts[n] = Part::Zero(min_ndigits - buf.len()); | |
375 | n += 1; | |
376 | } | |
377 | } | |
378 | ||
379 | // 0.1234 x 10^exp = 1.234 x 10^(exp-1) | |
380 | let exp = exp as i32 - 1; // avoid underflow when exp is i16::MIN | |
381 | if exp < 0 { | |
382 | parts[n] = Part::Copy(if upper { b"E-" } else { b"e-" }); | |
383 | parts[n + 1] = Part::Num(-exp as u16); | |
384 | } else { | |
385 | parts[n] = Part::Copy(if upper { b"E" } else { b"e" }); | |
386 | parts[n + 1] = Part::Num(exp as u16); | |
387 | } | |
388 | &parts[..n + 2] | |
389 | } | |
390 | ||
391 | /// Sign formatting options. | |
392 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | |
393 | pub enum Sign { | |
394 | /// Prints `-` only for the negative non-zero values. | |
60c5eb7d | 395 | Minus, // -inf -1 0 0 1 inf nan |
d9579d0f | 396 | /// Prints `-` only for any negative values (including the negative zero). |
60c5eb7d | 397 | MinusRaw, // -inf -1 -0 0 1 inf nan |
d9579d0f | 398 | /// Prints `-` for the negative non-zero values, or `+` otherwise. |
60c5eb7d | 399 | MinusPlus, // -inf -1 +0 +0 +1 +inf nan |
d9579d0f AL |
400 | /// Prints `-` for any negative values (including the negative zero), or `+` otherwise. |
401 | MinusPlusRaw, // -inf -1 -0 +0 +1 +inf nan | |
402 | } | |
403 | ||
404 | /// Returns the static byte string corresponding to the sign to be formatted. | |
405 | /// It can be either `b""`, `b"+"` or `b"-"`. | |
406 | fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static [u8] { | |
407 | match (*decoded, sign) { | |
408 | (FullDecoded::Nan, _) => b"", | |
409 | (FullDecoded::Zero, Sign::Minus) => b"", | |
60c5eb7d XL |
410 | (FullDecoded::Zero, Sign::MinusRaw) => { |
411 | if negative { | |
412 | b"-" | |
413 | } else { | |
414 | b"" | |
415 | } | |
416 | } | |
d9579d0f | 417 | (FullDecoded::Zero, Sign::MinusPlus) => b"+", |
60c5eb7d XL |
418 | (FullDecoded::Zero, Sign::MinusPlusRaw) => { |
419 | if negative { | |
420 | b"-" | |
421 | } else { | |
422 | b"+" | |
423 | } | |
424 | } | |
425 | (_, Sign::Minus) | (_, Sign::MinusRaw) => { | |
426 | if negative { | |
427 | b"-" | |
428 | } else { | |
429 | b"" | |
430 | } | |
431 | } | |
432 | (_, Sign::MinusPlus) | (_, Sign::MinusPlusRaw) => { | |
433 | if negative { | |
434 | b"-" | |
435 | } else { | |
436 | b"+" | |
437 | } | |
438 | } | |
d9579d0f AL |
439 | } |
440 | } | |
441 | ||
532ac7d7 | 442 | /// Formats the given floating point number into the decimal form with at least |
d9579d0f AL |
443 | /// given number of fractional digits. The result is stored to the supplied parts |
444 | /// array while utilizing given byte buffer as a scratch. `upper` is currently | |
445 | /// unused but left for the future decision to change the case of non-finite values, | |
0731742a | 446 | /// i.e., `inf` and `nan`. The first part to be rendered is always a `Part::Sign` |
d9579d0f AL |
447 | /// (which can be an empty string if no sign is rendered). |
448 | /// | |
449 | /// `format_shortest` should be the underlying digit-generation function. | |
450 | /// You probably would want `strategy::grisu::format_shortest` for this. | |
451 | /// | |
452 | /// `frac_digits` can be less than the number of actual fractional digits in `v`; | |
453 | /// it will be ignored and full digits will be printed. It is only used to print | |
454 | /// additional zeroes after rendered digits. Thus `frac_digits` of 0 means that | |
455 | /// it will only print given digits and nothing else. | |
456 | /// | |
457 | /// The byte buffer should be at least `MAX_SIG_DIGITS` bytes long. | |
7cac9316 XL |
458 | /// There should be at least 4 parts available, due to the worst case like |
459 | /// `[+][0.][0000][2][0000]` with `frac_digits = 10`. | |
60c5eb7d XL |
460 | pub fn to_shortest_str<'a, T, F>( |
461 | mut format_shortest: F, | |
462 | v: T, | |
463 | sign: Sign, | |
464 | frac_digits: usize, | |
465 | _upper: bool, | |
466 | buf: &'a mut [u8], | |
467 | parts: &'a mut [Part<'a>], | |
468 | ) -> Formatted<'a> | |
469 | where | |
470 | T: DecodableFloat, | |
471 | F: FnMut(&Decoded, &mut [u8]) -> (usize, i16), | |
472 | { | |
d9579d0f AL |
473 | assert!(parts.len() >= 4); |
474 | assert!(buf.len() >= MAX_SIG_DIGITS); | |
475 | ||
476 | let (negative, full_decoded) = decode(v); | |
477 | let sign = determine_sign(sign, &full_decoded, negative); | |
478 | match full_decoded { | |
479 | FullDecoded::Nan => { | |
480 | parts[0] = Part::Copy(b"NaN"); | |
b7449926 | 481 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
482 | } |
483 | FullDecoded::Infinite => { | |
484 | parts[0] = Part::Copy(b"inf"); | |
b7449926 | 485 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
486 | } |
487 | FullDecoded::Zero => { | |
60c5eb7d XL |
488 | if frac_digits > 0 { |
489 | // [0.][0000] | |
d9579d0f AL |
490 | parts[0] = Part::Copy(b"0."); |
491 | parts[1] = Part::Zero(frac_digits); | |
b7449926 | 492 | Formatted { sign, parts: &parts[..2] } |
d9579d0f AL |
493 | } else { |
494 | parts[0] = Part::Copy(b"0"); | |
b7449926 | 495 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
496 | } |
497 | } | |
498 | FullDecoded::Finite(ref decoded) => { | |
499 | let (len, exp) = format_shortest(decoded, buf); | |
60c5eb7d | 500 | Formatted { sign, parts: digits_to_dec_str(&buf[..len], exp, frac_digits, parts) } |
d9579d0f AL |
501 | } |
502 | } | |
503 | } | |
504 | ||
532ac7d7 | 505 | /// Formats the given floating point number into the decimal form or |
d9579d0f AL |
506 | /// the exponential form, depending on the resulting exponent. The result is |
507 | /// stored to the supplied parts array while utilizing given byte buffer | |
508 | /// as a scratch. `upper` is used to determine the case of non-finite values | |
509 | /// (`inf` and `nan`) or the case of the exponent prefix (`e` or `E`). | |
510 | /// The first part to be rendered is always a `Part::Sign` (which can be | |
511 | /// an empty string if no sign is rendered). | |
512 | /// | |
513 | /// `format_shortest` should be the underlying digit-generation function. | |
514 | /// You probably would want `strategy::grisu::format_shortest` for this. | |
515 | /// | |
516 | /// The `dec_bounds` is a tuple `(lo, hi)` such that the number is formatted | |
b039eaaf | 517 | /// as decimal only when `10^lo <= V < 10^hi`. Note that this is the *apparent* `V` |
d9579d0f AL |
518 | /// instead of the actual `v`! Thus any printed exponent in the exponential form |
519 | /// cannot be in this range, avoiding any confusion. | |
520 | /// | |
521 | /// The byte buffer should be at least `MAX_SIG_DIGITS` bytes long. | |
7cac9316 XL |
522 | /// There should be at least 6 parts available, due to the worst case like |
523 | /// `[+][1][.][2345][e][-][6]`. | |
60c5eb7d XL |
524 | pub fn to_shortest_exp_str<'a, T, F>( |
525 | mut format_shortest: F, | |
526 | v: T, | |
527 | sign: Sign, | |
528 | dec_bounds: (i16, i16), | |
529 | upper: bool, | |
530 | buf: &'a mut [u8], | |
531 | parts: &'a mut [Part<'a>], | |
532 | ) -> Formatted<'a> | |
533 | where | |
534 | T: DecodableFloat, | |
535 | F: FnMut(&Decoded, &mut [u8]) -> (usize, i16), | |
536 | { | |
d9579d0f AL |
537 | assert!(parts.len() >= 6); |
538 | assert!(buf.len() >= MAX_SIG_DIGITS); | |
539 | assert!(dec_bounds.0 <= dec_bounds.1); | |
540 | ||
541 | let (negative, full_decoded) = decode(v); | |
542 | let sign = determine_sign(sign, &full_decoded, negative); | |
543 | match full_decoded { | |
544 | FullDecoded::Nan => { | |
545 | parts[0] = Part::Copy(b"NaN"); | |
b7449926 | 546 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
547 | } |
548 | FullDecoded::Infinite => { | |
549 | parts[0] = Part::Copy(b"inf"); | |
b7449926 | 550 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
551 | } |
552 | FullDecoded::Zero => { | |
553 | parts[0] = if dec_bounds.0 <= 0 && 0 < dec_bounds.1 { | |
554 | Part::Copy(b"0") | |
555 | } else { | |
556 | Part::Copy(if upper { b"0E0" } else { b"0e0" }) | |
557 | }; | |
b7449926 | 558 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
559 | } |
560 | FullDecoded::Finite(ref decoded) => { | |
561 | let (len, exp) = format_shortest(decoded, buf); | |
562 | let vis_exp = exp as i32 - 1; | |
563 | let parts = if dec_bounds.0 as i32 <= vis_exp && vis_exp < dec_bounds.1 as i32 { | |
564 | digits_to_dec_str(&buf[..len], exp, 0, parts) | |
565 | } else { | |
566 | digits_to_exp_str(&buf[..len], exp, 0, upper, parts) | |
567 | }; | |
b7449926 | 568 | Formatted { sign, parts } |
d9579d0f AL |
569 | } |
570 | } | |
571 | } | |
572 | ||
532ac7d7 | 573 | /// Returns a rather crude approximation (upper bound) for the maximum buffer size |
d9579d0f AL |
574 | /// calculated from the given decoded exponent. |
575 | /// | |
576 | /// The exact limit is: | |
577 | /// | |
578 | /// - when `exp < 0`, the maximum length is `ceil(log_10 (5^-exp * (2^64 - 1)))`. | |
579 | /// - when `exp >= 0`, the maximum length is `ceil(log_10 (2^exp * (2^64 - 1)))`. | |
580 | /// | |
581 | /// `ceil(log_10 (x^exp * (2^64 - 1)))` is less than `ceil(log_10 (2^64 - 1)) + | |
582 | /// ceil(exp * log_10 x)`, which is in turn less than `20 + (1 + exp * log_10 x)`. | |
583 | /// We use the facts that `log_10 2 < 5/16` and `log_10 5 < 12/16`, which is | |
584 | /// enough for our purposes. | |
585 | /// | |
586 | /// Why do we need this? `format_exact` functions will fill the entire buffer | |
587 | /// unless limited by the last digit restriction, but it is possible that | |
588 | /// the number of digits requested is ridiculously large (say, 30,000 digits). | |
589 | /// The vast majority of buffer will be filled with zeroes, so we don't want to | |
590 | /// allocate all the buffer beforehand. Consequently, for any given arguments, | |
591 | /// 826 bytes of buffer should be sufficient for `f64`. Compare this with | |
592 | /// the actual number for the worst case: 770 bytes (when `exp = -1074`). | |
593 | fn estimate_max_buf_len(exp: i16) -> usize { | |
594 | 21 + ((if exp < 0 { -12 } else { 5 } * exp as i32) as usize >> 4) | |
595 | } | |
596 | ||
597 | /// Formats given floating point number into the exponential form with | |
598 | /// exactly given number of significant digits. The result is stored to | |
599 | /// the supplied parts array while utilizing given byte buffer as a scratch. | |
600 | /// `upper` is used to determine the case of the exponent prefix (`e` or `E`). | |
601 | /// The first part to be rendered is always a `Part::Sign` (which can be | |
602 | /// an empty string if no sign is rendered). | |
603 | /// | |
604 | /// `format_exact` should be the underlying digit-generation function. | |
605 | /// You probably would want `strategy::grisu::format_exact` for this. | |
606 | /// | |
607 | /// The byte buffer should be at least `ndigits` bytes long unless `ndigits` is | |
608 | /// so large that only the fixed number of digits will be ever written. | |
609 | /// (The tipping point for `f64` is about 800, so 1000 bytes should be enough.) | |
7cac9316 XL |
610 | /// There should be at least 6 parts available, due to the worst case like |
611 | /// `[+][1][.][2345][e][-][6]`. | |
60c5eb7d XL |
612 | pub fn to_exact_exp_str<'a, T, F>( |
613 | mut format_exact: F, | |
614 | v: T, | |
615 | sign: Sign, | |
616 | ndigits: usize, | |
617 | upper: bool, | |
618 | buf: &'a mut [u8], | |
619 | parts: &'a mut [Part<'a>], | |
620 | ) -> Formatted<'a> | |
621 | where | |
622 | T: DecodableFloat, | |
623 | F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16), | |
624 | { | |
d9579d0f AL |
625 | assert!(parts.len() >= 6); |
626 | assert!(ndigits > 0); | |
627 | ||
628 | let (negative, full_decoded) = decode(v); | |
629 | let sign = determine_sign(sign, &full_decoded, negative); | |
630 | match full_decoded { | |
631 | FullDecoded::Nan => { | |
632 | parts[0] = Part::Copy(b"NaN"); | |
b7449926 | 633 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
634 | } |
635 | FullDecoded::Infinite => { | |
636 | parts[0] = Part::Copy(b"inf"); | |
b7449926 | 637 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
638 | } |
639 | FullDecoded::Zero => { | |
60c5eb7d XL |
640 | if ndigits > 1 { |
641 | // [0.][0000][e0] | |
d9579d0f AL |
642 | parts[0] = Part::Copy(b"0."); |
643 | parts[1] = Part::Zero(ndigits - 1); | |
644 | parts[2] = Part::Copy(if upper { b"E0" } else { b"e0" }); | |
b7449926 | 645 | Formatted { sign, parts: &parts[..3] } |
d9579d0f AL |
646 | } else { |
647 | parts[0] = Part::Copy(if upper { b"0E0" } else { b"0e0" }); | |
b7449926 | 648 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
649 | } |
650 | } | |
651 | FullDecoded::Finite(ref decoded) => { | |
652 | let maxlen = estimate_max_buf_len(decoded.exp); | |
653 | assert!(buf.len() >= ndigits || buf.len() >= maxlen); | |
654 | ||
655 | let trunc = if ndigits < maxlen { ndigits } else { maxlen }; | |
656 | let (len, exp) = format_exact(decoded, &mut buf[..trunc], i16::MIN); | |
60c5eb7d | 657 | Formatted { sign, parts: digits_to_exp_str(&buf[..len], exp, ndigits, upper, parts) } |
d9579d0f AL |
658 | } |
659 | } | |
660 | } | |
661 | ||
662 | /// Formats given floating point number into the decimal form with exactly | |
663 | /// given number of fractional digits. The result is stored to the supplied parts | |
664 | /// array while utilizing given byte buffer as a scratch. `upper` is currently | |
665 | /// unused but left for the future decision to change the case of non-finite values, | |
0731742a | 666 | /// i.e., `inf` and `nan`. The first part to be rendered is always a `Part::Sign` |
d9579d0f AL |
667 | /// (which can be an empty string if no sign is rendered). |
668 | /// | |
669 | /// `format_exact` should be the underlying digit-generation function. | |
670 | /// You probably would want `strategy::grisu::format_exact` for this. | |
671 | /// | |
672 | /// The byte buffer should be enough for the output unless `frac_digits` is | |
673 | /// so large that only the fixed number of digits will be ever written. | |
674 | /// (The tipping point for `f64` is about 800, and 1000 bytes should be enough.) | |
7cac9316 XL |
675 | /// There should be at least 4 parts available, due to the worst case like |
676 | /// `[+][0.][0000][2][0000]` with `frac_digits = 10`. | |
60c5eb7d XL |
677 | pub fn to_exact_fixed_str<'a, T, F>( |
678 | mut format_exact: F, | |
679 | v: T, | |
680 | sign: Sign, | |
681 | frac_digits: usize, | |
682 | _upper: bool, | |
683 | buf: &'a mut [u8], | |
684 | parts: &'a mut [Part<'a>], | |
685 | ) -> Formatted<'a> | |
686 | where | |
687 | T: DecodableFloat, | |
688 | F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16), | |
689 | { | |
d9579d0f AL |
690 | assert!(parts.len() >= 4); |
691 | ||
692 | let (negative, full_decoded) = decode(v); | |
693 | let sign = determine_sign(sign, &full_decoded, negative); | |
694 | match full_decoded { | |
695 | FullDecoded::Nan => { | |
696 | parts[0] = Part::Copy(b"NaN"); | |
b7449926 | 697 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
698 | } |
699 | FullDecoded::Infinite => { | |
700 | parts[0] = Part::Copy(b"inf"); | |
b7449926 | 701 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
702 | } |
703 | FullDecoded::Zero => { | |
60c5eb7d XL |
704 | if frac_digits > 0 { |
705 | // [0.][0000] | |
d9579d0f AL |
706 | parts[0] = Part::Copy(b"0."); |
707 | parts[1] = Part::Zero(frac_digits); | |
b7449926 | 708 | Formatted { sign, parts: &parts[..2] } |
d9579d0f AL |
709 | } else { |
710 | parts[0] = Part::Copy(b"0"); | |
b7449926 | 711 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
712 | } |
713 | } | |
714 | FullDecoded::Finite(ref decoded) => { | |
715 | let maxlen = estimate_max_buf_len(decoded.exp); | |
716 | assert!(buf.len() >= maxlen); | |
717 | ||
718 | // it *is* possible that `frac_digits` is ridiculously large. | |
719 | // `format_exact` will end rendering digits much earlier in this case, | |
720 | // because we are strictly limited by `maxlen`. | |
721 | let limit = if frac_digits < 0x8000 { -(frac_digits as i16) } else { i16::MIN }; | |
722 | let (len, exp) = format_exact(decoded, &mut buf[..maxlen], limit); | |
723 | if exp <= limit { | |
724 | // the restriction couldn't been met, so this should render like zero no matter | |
725 | // `exp` was. this does not include the case that the restriction has been met | |
726 | // only after the final rounding-up; it's a regular case with `exp = limit + 1`. | |
727 | debug_assert_eq!(len, 0); | |
60c5eb7d XL |
728 | if frac_digits > 0 { |
729 | // [0.][0000] | |
d9579d0f AL |
730 | parts[0] = Part::Copy(b"0."); |
731 | parts[1] = Part::Zero(frac_digits); | |
b7449926 | 732 | Formatted { sign, parts: &parts[..2] } |
d9579d0f AL |
733 | } else { |
734 | parts[0] = Part::Copy(b"0"); | |
b7449926 | 735 | Formatted { sign, parts: &parts[..1] } |
d9579d0f AL |
736 | } |
737 | } else { | |
60c5eb7d | 738 | Formatted { sign, parts: digits_to_dec_str(&buf[..len], exp, frac_digits, parts) } |
d9579d0f AL |
739 | } |
740 | } | |
741 | } | |
742 | } |