plotters/data/
float.rs
1fn find_minimal_repr(n: f64, eps: f64) -> (f64, usize) {
3 if eps >= 1.0 {
4 return (n, 0);
5 }
6 if n - n.floor() < eps {
7 (n.floor(), 0)
8 } else if n.ceil() - n < eps {
9 (n.ceil(), 0)
10 } else {
11 let (rem, pre) = find_minimal_repr((n - n.floor()) * 10.0, eps * 10.0);
12 (n.floor() + rem / 10.0, pre + 1)
13 }
14}
15
16fn float_to_string(n: f64, max_precision: usize, min_decimal: usize) -> String {
17 let (mut result, mut count) = loop {
18 let (sign, n) = if n < 0.0 { ("-", -n) } else { ("", n) };
19 let int_part = n.floor();
20
21 let dec_part =
22 ((n.abs() - int_part.abs()) * (10.0f64).powi(max_precision as i32)).round() as u64;
23
24 if dec_part == 0 || max_precision == 0 {
25 break (format!("{}{:.0}", sign, int_part), 0);
26 }
27
28 let mut leading = "".to_string();
29 let mut dec_result = format!("{}", dec_part);
30
31 for _ in 0..(max_precision - dec_result.len()) {
32 leading.push('0');
33 }
34
35 while let Some(c) = dec_result.pop() {
36 if c != '0' {
37 dec_result.push(c);
38 break;
39 }
40 }
41
42 break (
43 format!("{}{:.0}.{}{}", sign, int_part, leading, dec_result),
44 leading.len() + dec_result.len(),
45 );
46 };
47
48 if count == 0 && min_decimal > 0 {
49 result.push('.');
50 }
51
52 while count < min_decimal {
53 result.push('0');
54 count += 1;
55 }
56 result
57}
58
59pub struct FloatPrettyPrinter {
60 pub allow_scientific: bool,
61 pub min_decimal: i32,
62 pub max_decimal: i32,
63}
64
65impl FloatPrettyPrinter {
66 pub fn print(&self, n: f64) -> String {
67 let (tn, p) = find_minimal_repr(n, (10f64).powi(-self.max_decimal));
68 let d_repr = float_to_string(tn, p, self.min_decimal as usize);
69 if !self.allow_scientific {
70 d_repr
71 } else {
72 if n == 0.0 {
73 return "0".to_string();
74 }
75
76 let mut idx = n.abs().log10().floor();
77 let mut exp = (10.0f64).powf(idx);
78
79 if n.abs() / exp + 1e-5 >= 10.0 {
80 idx += 1.0;
81 exp *= 10.0;
82 }
83
84 if idx.abs() < 3.0 {
85 return d_repr;
86 }
87
88 let (sn, sp) = find_minimal_repr(n / exp, 1e-5);
89 let s_repr = format!(
90 "{}e{}",
91 float_to_string(sn, sp, self.min_decimal as usize),
92 float_to_string(idx, 0, 0)
93 );
94 if s_repr.len() + 1 < d_repr.len() || (tn == 0.0 && n != 0.0) {
95 s_repr
96 } else {
97 d_repr
98 }
99 }
100 }
101}
102
103pub fn pretty_print_float(n: f64, allow_sn: bool) -> String {
112 (FloatPrettyPrinter {
113 allow_scientific: allow_sn,
114 min_decimal: 0,
115 max_decimal: 10,
116 })
117 .print(n)
118}
119
120#[cfg(test)]
121mod test {
122 use super::*;
123 #[test]
124 fn test_pretty_printing() {
125 assert_eq!(pretty_print_float(0.99999999999999999999, false), "1");
126 assert_eq!(pretty_print_float(0.9999, false), "0.9999");
127 assert_eq!(
128 pretty_print_float(-1e-5 - 0.00000000000000001, true),
129 "-1e-5"
130 );
131 assert_eq!(
132 pretty_print_float(-1e-5 - 0.00000000000000001, false),
133 "-0.00001"
134 );
135 assert_eq!(pretty_print_float(1e100, true), "1e100");
136 assert_eq!(pretty_print_float(1234567890f64, true), "1234567890");
137 assert_eq!(pretty_print_float(1000000001f64, true), "1e9");
138 }
139}