flt2dec front of format_short & format_fixed#154505
flt2dec front of format_short & format_fixed#154505pascaldekloe wants to merge 8 commits intorust-lang:mainfrom
Conversation
|
Some changes occurred in float parsing cc @tgross35 |
|
r? @scottmcm rustbot has assigned @scottmcm. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
Thank you for preparing this!
I only had time to go through the first commit here, which mostly looks pretty good to me with a few small requests. If you'd like to fix those then put only the first commit in a separate PR, I'd be happy to approve that bit and shrink the remaining todo here.
As a noncritical note, I'm having a bit of trouble understanding the commit summaries. The messages usually have details, but I'm not entirely sure what something like "write out of flt2dec/random.rs" or "test flt2dec front of format_short & format_fixed" means. We don't really have a guideline but sticking to the usual style with changes described in imperative mood would help a bit (e.g. something like flt2dec: test: Don't use `write!` in random tests or flt2dec: test: Test `format_{short,fixed}` via the main entrypoint).
I'll review the rest a little later.
| # Problem statement | ||
|
|
||
| We are given the floating-point number `v = f * 2^e` with an integer `f`, | ||
| and its bounds `minus` and `plus` such that any number between `v - minus` and | ||
| `v + plus` will be rounded to `v`. For the simplicity we assume that | ||
| this range is exclusive. Then we would like to get the unique decimal | ||
| representation `V = 0.d[0..n-1] * 10^k` such that: | ||
|
|
||
| - `d[0]` is non-zero. | ||
|
|
||
| - It's correctly rounded when parsed back: `v - minus < V < v + plus`. | ||
| Furthermore it is shortest such one, i.e., there is no representation | ||
| with less than `n` digits that is correctly rounded. | ||
|
|
||
| - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Note that | ||
| there might be two representations satisfying this uniqueness requirement, | ||
| in which case some tie-breaking mechanism is used. | ||
|
|
||
| We will call this mode of operation as to the *shortest* mode. This mode is used | ||
| when there is no additional constraint, and can be thought as a "natural" mode | ||
| as it matches the ordinary intuition (it at least prints `0.1f32` as "0.1"). | ||
|
|
||
| We have two more modes of operation closely related to each other. In these modes | ||
| we are given either the number of significant digits `n` or the last-digit | ||
| limitation `limit` (which determines the actual `n`), and we would like to get | ||
| the representation `V = 0.d[0..n-1] * 10^k` such that: | ||
|
|
||
| - `d[0]` is non-zero, unless `n` was zero in which case only `k` is returned. | ||
|
|
||
| - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Again, | ||
| there might be some tie-breaking mechanism. | ||
|
|
||
| When `limit` is given but not `n`, we set `n` such that `k - n = limit` | ||
| so that the last digit `d[n-1]` is scaled by `10^(k-n) = 10^limit`. | ||
| If such `n` is negative, we clip it to zero so that we will only get `k`. | ||
| We are also limited by the supplied buffer. This limitation is used to print | ||
| the number up to given number of fractional digits without knowing | ||
| the correct `k` beforehand. | ||
|
|
||
| We will call the mode of operation requiring `n` as to the *exact* mode, | ||
| and one requiring `limit` as to the *fixed* mode. The exact mode is a subset of | ||
| the fixed mode: the sufficiently large last-digit limitation will eventually fill | ||
| the supplied buffer and let the algorithm to return. | ||
|
|
||
| # Implementation overview | ||
|
|
There was a problem hiding this comment.
Commit 1: It's fine to delete the short vs. fixed info here , but having an overview of how float formatting works is still nice. Also it's helpful to introduce notation, something like https://github.com/pascaldekloe/rust/blob/40811931b23b70db03103c851ca9ce3290a3201e/library/core/src/num/imp/dec2flt/mod.rs#L71-L81 would be better yet than what's preexisting.
There was a problem hiding this comment.
It is documented on the two functions themselves now. We will lose the "helper" functions thus the remainder will be very readable, i.e., they no longer require overviews once organised.
I'll follow the Lemire notation, because why not.
There was a problem hiding this comment.
Could you at least restore the removed portion of the module-level documentation, with changes to naming/notation as you see fit?
Organization isn't at odds with a module description, I don't want to lose the "readme" effect that can give somebody an understanding of the algorithms without needing to look at its API. There is also some helpful information here that we just don't gain anything by deleting.
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_small_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(3.141592f64); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_big_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(f64::MAX); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_one_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(1.0); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_trailing_zero_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(250.000000000000000000000000); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_halfway_point_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(1.00000000000000011102230246251565404236316680908203125); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| format_fixed(black_box(&decoded), &mut buf, i16::MIN).unwrap(); |
There was a problem hiding this comment.
Commit 1: Please explain what's going on with the removals here in the commit message
There was a problem hiding this comment.
That is what I ment in point 4 of the commit message "Drop of Grisu benchmarks which's value caused Dragon". The numeric value of the deleted benchmarks doesn't have a correct representation in Grisu, so they silently tested Dragon instead.
There was a problem hiding this comment.
Rather than deleting them, could you move them to the dragon benchmarks if they aren't already covered there?
Might be worth a FIXME here or both files to just call the grisu::format* directly if that's what this is intended to bench.
There was a problem hiding this comment.
They are already in dragon. No fixing needed.
There was a problem hiding this comment.
Okay, makes sense. Please just make a note in the commit message for posterity that they are already tested in dragon.
* Fallback from Grisu to Dragon in flt2dec front * Together with name change to ensure all use checked * Drop of Grisu benchmarks which tested Dragon fallback * Documentation at functions instead of module level
|
Pushed the commit explainer plus distinct summary lines in Rust docs @tgross35. :-) I'll wait for the rest of the review. Some urgency would be nice because I have time to do this now. 🙏 The git messages ain't readable sections on their own indeed. You need the code in conjunction to comprehend the listings. I'll work on that. |
20351fd to
aaf4362
Compare
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_small_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(3.141592f64); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_big_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(f64::MAX); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_one_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(1.0); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_trailing_zero_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(250.000000000000000000000000); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_halfway_point_exact_inf(b: &mut Bencher) { | ||
| let decoded = decode_finite(1.00000000000000011102230246251565404236316680908203125); | ||
| let mut buf = [MaybeUninit::new(0); 1024]; | ||
| b.iter(|| { | ||
| format_exact(black_box(&decoded), &mut buf, i16::MIN); | ||
| format_fixed(black_box(&decoded), &mut buf, i16::MIN).unwrap(); |
There was a problem hiding this comment.
Rather than deleting them, could you move them to the dragon benchmarks if they aren't already covered there?
Might be worth a FIXME here or both files to just call the grisu::format* directly if that's what this is intended to bench.
There was a problem hiding this comment.
Commit "rounding of leading digit tested in fmt": would it be possible to cover f16 and f32 as well?
There was a problem hiding this comment.
There's a ton of work pending. I'm trying to make progress rather than picking up every possible thing on the way. 😅
There was a problem hiding this comment.
This is a new test, it should at least cover f32. Dropping this commit to a later PR is also fine if that's easier.
"A little copying is better than a little dependency." — Rob Pike * Equivalence comparison explicit instead of generic * Direct test failure instead of counting with summary * Bit patterns specified as ops::Range constants * No panic on invalid UTF-8 in equivalence test
* Test functionality as provided instead of inners * No generics abstraction of Grisu and Dragon * The stringify! of input in assertion message * TestableFloat abstraction omitted with macro * MSVC omission central, direct on respective test Unified signature plus description on assertion tools: * check_shortest! -> check_short! * try_exact! -> check_match! * try_fixed! -> check_resolution! * check_exact & check_exact! -> check_fixed_mix! * check_exact_one -> check_coef_pow2!
Fix the number of infinite zeroes tested (to seven). It looks like the spaces were used as text alignment anyway.
aaf4362 to
894fa1e
Compare
There was a problem hiding this comment.
Commit 2 "write out of flt2dec/random.rs": Please reword the commit summary line, I'm not sure what it's intending to say. Otherwise LGTM at 2f7b015.
| #[cfg(not(target_env = "msvc"))] | ||
| #[cfg_attr(miri, ignore)] // Miri is too slow |
There was a problem hiding this comment.
Commit 3: Use cfg_attr(..., ignore) for MSVC as well rather than configuring it out, to make it easier to opt in if somebody wants to check this locally.
| # Problem statement | ||
|
|
||
| We are given the floating-point number `v = f * 2^e` with an integer `f`, | ||
| and its bounds `minus` and `plus` such that any number between `v - minus` and | ||
| `v + plus` will be rounded to `v`. For the simplicity we assume that | ||
| this range is exclusive. Then we would like to get the unique decimal | ||
| representation `V = 0.d[0..n-1] * 10^k` such that: | ||
|
|
||
| - `d[0]` is non-zero. | ||
|
|
||
| - It's correctly rounded when parsed back: `v - minus < V < v + plus`. | ||
| Furthermore it is shortest such one, i.e., there is no representation | ||
| with less than `n` digits that is correctly rounded. | ||
|
|
||
| - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Note that | ||
| there might be two representations satisfying this uniqueness requirement, | ||
| in which case some tie-breaking mechanism is used. | ||
|
|
||
| We will call this mode of operation as to the *shortest* mode. This mode is used | ||
| when there is no additional constraint, and can be thought as a "natural" mode | ||
| as it matches the ordinary intuition (it at least prints `0.1f32` as "0.1"). | ||
|
|
||
| We have two more modes of operation closely related to each other. In these modes | ||
| we are given either the number of significant digits `n` or the last-digit | ||
| limitation `limit` (which determines the actual `n`), and we would like to get | ||
| the representation `V = 0.d[0..n-1] * 10^k` such that: | ||
|
|
||
| - `d[0]` is non-zero, unless `n` was zero in which case only `k` is returned. | ||
|
|
||
| - It's closest to the original value: `abs(V - v) <= 10^(k-n) / 2`. Again, | ||
| there might be some tie-breaking mechanism. | ||
|
|
||
| When `limit` is given but not `n`, we set `n` such that `k - n = limit` | ||
| so that the last digit `d[n-1]` is scaled by `10^(k-n) = 10^limit`. | ||
| If such `n` is negative, we clip it to zero so that we will only get `k`. | ||
| We are also limited by the supplied buffer. This limitation is used to print | ||
| the number up to given number of fractional digits without knowing | ||
| the correct `k` beforehand. | ||
|
|
||
| We will call the mode of operation requiring `n` as to the *exact* mode, | ||
| and one requiring `limit` as to the *fixed* mode. The exact mode is a subset of | ||
| the fixed mode: the sufficiently large last-digit limitation will eventually fill | ||
| the supplied buffer and let the algorithm to return. | ||
|
|
||
| # Implementation overview | ||
|
|
There was a problem hiding this comment.
Could you at least restore the removed portion of the module-level documentation, with changes to naming/notation as you see fit?
Organization isn't at odds with a module description, I don't want to lose the "readme" effect that can give somebody an understanding of the algorithms without needing to look at its API. There is also some helpful information here that we just don't gain anything by deleting.
| ($coef:expr, $pow2:expr => $digits:expr, $pow10:expr) => ( | ||
| check_match!($coef * $pow2.exp2() => $digits, $pow10); | ||
| check_resolution!($coef * $pow2.exp2(), $pow10 - $digits.len() as i16 => $digits, $pow10); | ||
| ) |
There was a problem hiding this comment.
C3: Nit: store $coef * $pow2.exp2() to a local to save a (potentially slow) exp2 in debug mode
There was a problem hiding this comment.
Commit 3 "test flt2dec front of format_short & format_fixed": the message says:
- No generics abstraction of Grisu and Dragon
How are the individual algorithms being tested now?
Other than that, the changes here look great to me aside from a few requests.
| let resolution: i16 = $resolution; | ||
| let want: (&[u8], i16) = ($digits, $pow10); | ||
|
|
||
| let mut large_buf = [MaybeUninit::new(b'_'); 1024]; |
There was a problem hiding this comment.
C3: Could [MaybeUninit::new(b'_'); 1024] be pulled out to a constant? Since it's used in a few places.
| check_fixed_mix!(f32::MIN_POSITIVE => b"1175494350822287507968736537222245677818", -37); | ||
| check_fixed_mix!(f32::MIN_POSITIVE => b"1175494350822287507968736537222245677819", -37); |
There was a problem hiding this comment.
C4 "FIX faulty test values in flt2dec; last digit was not tested": These values weren't correct before right? Just add a note in the commit message that it's the values that are wrong, not our implementation, and how you validated if applicable.
Otherwise this commit LGTM.
There was a problem hiding this comment.
C6 "typo in test value from round-to-even in fmt": Add a note in the commit message that the values are equivalent when rounded to three decimals, then LGTM.
| start = 0; | ||
| } | ||
| for i in start..-10 { | ||
| for i in (skip_first as i16)..-10 { |
There was a problem hiding this comment.
C8 "elaborate on digit loss due rounding in flt2dec": Nit: i16::from(skip_first) (avoiding potentially lossy as casts)
| if exp <= limit { | ||
| // the restriction couldn't been met, so this should render like zero no matter | ||
| // `exp` was. this does not include the case that the restriction has been met | ||
| // only after the final rounding-up; it's a regular case with `exp = limit + 1`. | ||
| debug_assert_eq!(buf.len(), 0); | ||
| if buf.len() == 0 { | ||
| // The number rounds down to zero at the given resolution. | ||
| debug_assert!(exp <= limit); |
There was a problem hiding this comment.
C8: Since these should be equivalent, what's the motivation to swap?
There was a problem hiding this comment.
C1: Please clarify "Together with name change to ensure all use checked" in the commit message
|
Let's make sure there's no perf effect. @bors try @rust-timer queue @rustbot author |
This comment has been minimized.
This comment has been minimized.
|
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
flt2dec front of format_short & format_fixed
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (cb7a7f2): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (secondary 3.9%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesThis perf run didn't have relevant results for this metric. Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 488.817s -> 487.773s (-0.21%) |
View all comments
The fallback to Dragon was placed in the Grisu module. As a result tests don't test what they appear to test, and some benchmarks from Grisu measured Dragon instead.
The corrected values in the tests haven been verified against the Go implementation.