Skip to main content

py_float_repr

Function py_float_repr 

Source
pub fn py_float_repr(x: f64) -> String
Expand description

repr(float): the shortest digits that round-trip, positional when the decimal exponent is in -4..16, else d.ddde+XX (PyOS_double_to_string with mode 'r'); nan, inf and -inf.

Of the shortest digits that round-trip, Python’s repr (David Gay’s dtoa in mode 0) gives those nearest the double, and of two equally near the even one. Rust’s shortest {:e} breaks that tie upwards (1551563132437320.25: Python …320.2, {:e} …320.3; review round 8), so its length only is taken: the double rounded to that many digits ({:.*e}: exact, ties to even) is the nearest of that length, and is Python’s when it round-trips. When it does not (a power of two, whose doubles that round-trip reach half as far below it as above, can have its nearest digits below them), {:e}’s digits are kept: they are then the nearest that round-trip, as Python’s are, with no tie between two (crates/unityrs-dump/tests/fixtures/dump/index.json “str” holds ties and such powers, which the tests here repeat).