]> Repositorios git - scryer-prolog.git/commitdiff
use ryu to print floats with a modification for compatible syntax (#1368, #1372)
authorMark Thom <[email protected]>
Sun, 27 Mar 2022 17:28:10 +0000 (11:28 -0600)
committerMark Thom <[email protected]>
Sun, 27 Mar 2022 17:28:10 +0000 (11:28 -0600)
Cargo.lock
Cargo.toml
src/heap_print.rs

index f6a5430f67fd16c6b2ebdf8c0fe2d96d6c3ee364..ac766ca55a6929cd0c080a3448a41c1db0f38be8 100644 (file)
@@ -1532,6 +1532,7 @@ dependencies = [
  "roxmltree",
  "rug",
  "rustyline",
+ "ryu",
  "select",
  "serial_test",
  "sha3",
index e74616d7a808527ab18074c29ad9db35879b8e02..62ef48f2c2bc4af0d738a5bff7838383830d759e 100644 (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"
index 030e7c737bd615ed95325520f8e8ba3aad6378cf..8fc2bbe2f50c8f373092d2129a406916d7af45bc 100644 (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)]