Skip to content

Commit 8a195f0

Browse files
committed
core: fixed typos and revised comments in flt2dec.
1 parent 85424c4 commit 8a195f0

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/libcore/num/flt2dec/bignum.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl_full_ops! {
8888
u8: add(intrinsics::u8_add_with_overflow), mul/div(u16);
8989
u16: add(intrinsics::u16_add_with_overflow), mul/div(u32);
9090
u32: add(intrinsics::u32_add_with_overflow), mul/div(u64);
91-
// u64: add(intrinsics::u64_add_with_overflow), mul/div(u128); // damn!
91+
// u64: add(intrinsics::u64_add_with_overflow), mul/div(u128); // see RFC #521 for enabling this.
9292
}
9393

9494
macro_rules! define_bignum {
@@ -103,11 +103,12 @@ macro_rules! define_bignum {
103103
/// All operations available to bignums panic in the case of over/underflows.
104104
/// The caller is responsible to use large enough bignum types.
105105
pub struct $name {
106-
/// One plus the offset to the maximum "digit" in the use.
106+
/// One plus the offset to the maximum "digit" in use.
107107
/// This does not decrease, so be aware of the computation order.
108108
/// `base[size..]` should be zero.
109109
size: usize,
110-
/// Digits. `[a, b, c, ...]` represents `a + b*n + c*n^2 + ...`.
110+
/// Digits. `[a, b, c, ...]` represents `a + b*2^W + c*2^(2W) + ...`
111+
/// where `W` is the number of bits in the digit type.
111112
base: [$ty; $n]
112113
}
113114

@@ -215,7 +216,7 @@ macro_rules! define_bignum {
215216
self.base[i] = 0;
216217
}
217218

218-
// shift by `nbits` bits
219+
// shift by `bits` bits
219220
let mut sz = self.size + digits;
220221
if bits > 0 {
221222
let last = sz;
@@ -236,8 +237,9 @@ macro_rules! define_bignum {
236237
self
237238
}
238239

239-
/// Multiplies itself by a number described by `other[0] + other[1] * n +
240-
/// other[2] * n^2 + ...` and returns its own mutable reference.
240+
/// Multiplies itself by a number described by `other[0] + other[1] * 2^W +
241+
/// other[2] * 2^(2W) + ...` (where `W` is the number of bits in the digit type)
242+
/// and returns its own mutable reference.
241243
pub fn mul_digits<'a>(&'a mut self, other: &[$ty]) -> &'a mut $name {
242244
// the internal routine. works best when aa.len() <= bb.len().
243245
fn mul_inner(ret: &mut [$ty; $n], aa: &[$ty], bb: &[$ty]) -> usize {

src/libcore/num/flt2dec/estimator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
1919
// 2^(nbits-1) < mant <= 2^nbits if mant > 0
2020
let nbits = 64 - (mant - 1).leading_zeros() as i64;
21+
// 1292913986 = floor(2^32 * log_10 2)
22+
// therefore this always underestimates (or is exact), but not much.
2123
(((nbits + exp as i64) * 1292913986) >> 32) as i16
2224
}
2325

src/libcore/num/flt2dec/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Both implementations expose two public functions:
9393
9494
They try to fill the `u8` buffer with digits and returns the number of digits
9595
written and the exponent `k`. They are total for all finite `f32` and `f64`
96-
inputs (Grisu internally falls back to Dragon if possible).
96+
inputs (Grisu internally falls back to Dragon if necessary).
9797
9898
The rendered digits are formatted into the actual string form with
9999
four functions:
@@ -114,8 +114,8 @@ four functions:
114114
They all return a slice of preallocated `Part` array, which corresponds to
115115
the individual part of strings: a fixed string, a part of rendered digits,
116116
a number of zeroes or a small (`u16`) number. The caller is expected to
117-
provide an enough buffer and `Part` array, and to assemble the final
118-
string from parts itself.
117+
provide a large enough buffer and `Part` array, and to assemble the final
118+
string from resulting `Part`s itself.
119119
120120
All algorithms and formatting functions are accompanied by extensive tests
121121
in `coretest::num::flt2dec` module. It also shows how to use individual
@@ -274,7 +274,7 @@ fn digits_to_dec_str<'a>(buf: &'a [u8], exp: i16, frac_digits: usize,
274274

275275
// if there is the restriction on the last digit position, `buf` is assumed to be
276276
// left-padded with the virtual zeroes. the number of virtual zeroes, `nzeroes`,
277-
// equals to `max(0, exp + frag_digits - buf.len())`, so that the position of
277+
// equals to `max(0, exp + frac_digits - buf.len())`, so that the position of
278278
// the last digit `exp - buf.len() - nzeroes` is no more than `-frac_digits`:
279279
//
280280
// |<-virtual->|
@@ -373,7 +373,7 @@ pub enum Sign {
373373
/// Prints `-` only for the negative non-zero values.
374374
Minus, // -inf -1 0 0 1 inf nan
375375
/// Prints `-` only for any negative values (including the negative zero).
376-
MinusRaw, // -inf -1 0 0 1 inf nan
376+
MinusRaw, // -inf -1 -0 0 1 inf nan
377377
/// Prints `-` for the negative non-zero values, or `+` otherwise.
378378
MinusPlus, // -inf -1 +0 +0 +1 +inf nan
379379
/// Prints `-` for any negative values (including the negative zero), or `+` otherwise.
@@ -639,9 +639,8 @@ pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
639639
let limit = if frac_digits < 0x8000 { -(frac_digits as i16) } else { i16::MIN };
640640
let (len, exp) = format_exact(decoded, &mut buf[..maxlen], limit);
641641
if exp <= limit {
642-
// `format_exact` always returns at least one digit even though the restriction
643-
// hasn't been met, so we catch this condition and treats as like zeroes.
644-
// this does not include the case that the restriction has been met
642+
// the restriction couldn't been met, so this should render like zero no matter
643+
// `exp` was. this does not include the case that the restriction has been met
645644
// only after the final rounding-up; it's a regular case with `exp = limit + 1`.
646645
debug_assert_eq!(len, 0);
647646
if frac_digits > 0 { // [0.][0000]

src/libcoretest/num/flt2dec/strategy/grisu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn shortest_f32_exhaustive_equivalence_test() {
6666
f32_exhaustive_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS);
6767
}
6868

69-
#[test] #[ignore] // is is too expensive
69+
#[test] #[ignore] // it is too expensive
7070
fn shortest_f64_hard_random_equivalence_test() {
7171
// this again probably has to use appropriate rustc flags.
7272

0 commit comments

Comments
 (0)