Skip to main content

get_str_digit_count

Function get_str_digit_count 

Source
pub fn get_str_digit_count(base: u64, prec: u64) -> usize
Expand description

Returns the number of significant digits that suffice to losslessly represent any Float of precision prec in base base: printing such a Float to this many digits with rounding to nearest (for example with get_str), then reading the digits back at precision prec, again rounding to nearest, recovers the original value exactly.

The count is $1 + \lceil p \log 2 / \log b \rceil$, except that for a power-of-2 base $b = 2^k$ it is $1 + \lceil (p - 1) / k \rceil$.

This function depends only on the precision, not on any particular Float value. It is the digit count get_str uses when its digit_len argument is 0, and the number of significant digits Display shows for a Float of precision prec.

§Worst-case complexity

Constant time and additional memory.

§Panics

Panics if base is less than 2 or greater than 62, or if prec is 0.

§Examples

use malachite_float::float::conversion::string::get_str::get_str_digit_count;

// 17 significant decimal digits distinguish every double-precision (53-bit) value...
assert_eq!(get_str_digit_count(10, 53), 17);
// ...and 9 suffice for single precision (24 bits)
assert_eq!(get_str_digit_count(10, 24), 9);
// in base 16, 14 digits: 1 + ceil(52 / 4)
assert_eq!(get_str_digit_count(16, 53), 14);
// in base 2 the digits are just the bits
assert_eq!(get_str_digit_count(2, 53), 53);

This is mpfr_get_str_ndigits from get_str.c, MPFR 4.2.2.