/rust/registry/src/index.crates.io-1949cf8c6b5b557f/plotters-0.3.7/src/data/float.rs
Line | Count | Source |
1 | | // The code that is related to float number handling |
2 | 0 | fn find_minimal_repr(n: f64, eps: f64) -> (f64, usize) { |
3 | 0 | if eps >= 1.0 { |
4 | 0 | return (n, 0); |
5 | 0 | } |
6 | 0 | if n - n.floor() < eps { |
7 | 0 | (n.floor(), 0) |
8 | 0 | } else if n.ceil() - n < eps { |
9 | 0 | (n.ceil(), 0) |
10 | | } else { |
11 | 0 | let (rem, pre) = find_minimal_repr((n - n.floor()) * 10.0, eps * 10.0); |
12 | 0 | (n.floor() + rem / 10.0, pre + 1) |
13 | | } |
14 | 0 | } |
15 | | |
16 | | #[allow(clippy::never_loop)] |
17 | 0 | fn float_to_string(n: f64, max_precision: usize, min_decimal: usize) -> String { |
18 | 0 | let (mut result, mut count) = loop { |
19 | 0 | let (sign, n) = if n < 0.0 { ("-", -n) } else { ("", n) }; |
20 | 0 | let int_part = n.floor(); |
21 | | |
22 | 0 | let dec_part = |
23 | 0 | ((n.abs() - int_part.abs()) * (10.0f64).powi(max_precision as i32)).round() as u64; |
24 | | |
25 | 0 | if dec_part == 0 || max_precision == 0 { |
26 | 0 | break (format!("{}{:.0}", sign, int_part), 0); |
27 | 0 | } |
28 | | |
29 | 0 | let mut leading = "".to_string(); |
30 | 0 | let mut dec_result = format!("{}", dec_part); |
31 | | |
32 | 0 | for _ in 0..(max_precision - dec_result.len()) { |
33 | 0 | leading.push('0'); |
34 | 0 | } |
35 | | |
36 | 0 | while let Some(c) = dec_result.pop() { |
37 | 0 | if c != '0' { |
38 | 0 | dec_result.push(c); |
39 | 0 | break; |
40 | 0 | } |
41 | | } |
42 | | |
43 | 0 | break ( |
44 | 0 | format!("{}{:.0}.{}{}", sign, int_part, leading, dec_result), |
45 | 0 | leading.len() + dec_result.len(), |
46 | 0 | ); |
47 | | }; |
48 | | |
49 | 0 | if count == 0 && min_decimal > 0 { |
50 | 0 | result.push('.'); |
51 | 0 | } |
52 | | |
53 | 0 | while count < min_decimal { |
54 | 0 | result.push('0'); |
55 | 0 | count += 1; |
56 | 0 | } |
57 | 0 | result |
58 | 0 | } |
59 | | |
60 | | /// Handles printing of floating point numbers |
61 | | pub struct FloatPrettyPrinter { |
62 | | /// Whether scientific notation is allowed |
63 | | pub allow_scientific: bool, |
64 | | /// Minimum allowed number of decimal digits |
65 | | pub min_decimal: i32, |
66 | | /// Maximum allowed number of decimal digits |
67 | | pub max_decimal: i32, |
68 | | } |
69 | | |
70 | | impl FloatPrettyPrinter { |
71 | | /// Handles printing of floating point numbers |
72 | 0 | pub fn print(&self, n: f64) -> String { |
73 | 0 | let (tn, p) = find_minimal_repr(n, (10f64).powi(-self.max_decimal)); |
74 | 0 | let d_repr = float_to_string(tn, p, self.min_decimal as usize); |
75 | 0 | if !self.allow_scientific { |
76 | 0 | d_repr |
77 | | } else { |
78 | 0 | if n == 0.0 { |
79 | 0 | return "0".to_string(); |
80 | 0 | } |
81 | | |
82 | 0 | let mut idx = n.abs().log10().floor(); |
83 | 0 | let mut exp = (10.0f64).powf(idx); |
84 | | |
85 | 0 | if n.abs() / exp + 1e-5 >= 10.0 { |
86 | 0 | idx += 1.0; |
87 | 0 | exp *= 10.0; |
88 | 0 | } |
89 | | |
90 | 0 | if idx.abs() < 3.0 { |
91 | 0 | return d_repr; |
92 | 0 | } |
93 | | |
94 | 0 | let (sn, sp) = find_minimal_repr(n / exp, 1e-5); |
95 | 0 | let s_repr = format!( |
96 | 0 | "{}e{}", |
97 | 0 | float_to_string(sn, sp, self.min_decimal as usize), |
98 | 0 | float_to_string(idx, 0, 0) |
99 | | ); |
100 | 0 | if s_repr.len() + 1 < d_repr.len() || (tn == 0.0 && n != 0.0) { |
101 | 0 | s_repr |
102 | | } else { |
103 | 0 | d_repr |
104 | | } |
105 | | } |
106 | 0 | } |
107 | | } |
108 | | |
109 | | /// The function that pretty prints the floating number |
110 | | /// Since rust doesn't have anything that can format a float with out appearance, so we just |
111 | | /// implement a float pretty printing function, which finds the shortest representation of a |
112 | | /// floating point number within the allowed error range. |
113 | | /// |
114 | | /// - `n`: The float number to pretty-print |
115 | | /// - `allow_sn`: Should we use scientific notation when possible |
116 | | /// - **returns**: The pretty printed string |
117 | 0 | pub fn pretty_print_float(n: f64, allow_sn: bool) -> String { |
118 | 0 | (FloatPrettyPrinter { |
119 | 0 | allow_scientific: allow_sn, |
120 | 0 | min_decimal: 0, |
121 | 0 | max_decimal: 10, |
122 | 0 | }) |
123 | 0 | .print(n) |
124 | 0 | } |
125 | | |
126 | | #[cfg(test)] |
127 | | mod test { |
128 | | use super::*; |
129 | | #[test] |
130 | | fn test_pretty_printing() { |
131 | | assert_eq!(pretty_print_float(0.99999999999999999999, false), "1"); |
132 | | assert_eq!(pretty_print_float(0.9999, false), "0.9999"); |
133 | | assert_eq!( |
134 | | pretty_print_float(-1e-5 - 0.00000000000000001, true), |
135 | | "-1e-5" |
136 | | ); |
137 | | assert_eq!( |
138 | | pretty_print_float(-1e-5 - 0.00000000000000001, false), |
139 | | "-0.00001" |
140 | | ); |
141 | | assert_eq!(pretty_print_float(1e100, true), "1e100"); |
142 | | assert_eq!(pretty_print_float(1234567890f64, true), "1234567890"); |
143 | | assert_eq!(pretty_print_float(1000000001f64, true), "1e9"); |
144 | | } |
145 | | } |