use ryu to print floats with a modification for compatible syntax (#1368, #1372)

This commit is contained in:
Mark Thom
2022-03-27 11:28:10 -06:00
parent 83def95383
commit 046a2175ab
3 changed files with 16 additions and 9 deletions

1
Cargo.lock generated
View File

@@ -1532,6 +1532,7 @@ dependencies = [
"roxmltree",
"rug",
"rustyline",
"ryu",
"select",
"serial_test",
"sha3",

View File

@@ -64,6 +64,7 @@ smallvec = "1.8.0"
sodiumoxide = "0.2.6"
static_assertions = "1.1.0"
slice-deque = "0.3.0"
ryu = "1.0.9"
[dev-dependencies]
assert_cmd = "1.0.3"

View File

@@ -484,17 +484,22 @@ pub fn fmt_float(mut fl: f64) -> String {
fl = 0f64;
}
if fl.fract() == 0f64 {
if fl.abs() >= 1.0e16 {
format!("{:.1e}", fl.trunc())
} else {
format!("{:.1}", fl.trunc())
let mut buffer = ryu::Buffer::new();
let fl_str = buffer.format(fl);
/* When printing floats with zero fractional parts in scientific notation, ryu
* prints "{integer part}e{exponent}" without a ".0" preceding "e",
* which is not valid ISO Prolog syntax. Add ".0" manually in this
* case.
*/
if let Some(e_index) = fl_str.find('e') {
if !fl_str[0 .. e_index].contains('.') {
return fl_str[0 .. e_index].to_string() + ".0" + &fl_str[e_index ..];
}
} else if 0f64 < fl.fract().abs() && fl.fract().abs() <= 1.0e-16 {
format!("{0:.15e}", fl)
} else {
format!("{}", fl)
}
fl_str.to_string()
}
#[derive(Debug)]