1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
pub use self::ExponentFormat::*;
pub use self::SignificantDigits::*;
use prelude::*;
use char;
use fmt;
use num::Float;
use num::FpCategory as Fp;
use ops::{Div, Rem, Mul};
use slice;
use str;
pub enum ExponentFormat {
ExpNone,
ExpDec
}
pub enum SignificantDigits {
DigMax(usize),
DigExact(usize)
}
#[doc(hidden)]
pub trait MyFloat: Float + PartialEq + PartialOrd + Div<Output=Self> +
Mul<Output=Self> + Rem<Output=Self> + Copy {
fn from_u32(u: u32) -> Self;
fn to_i32(&self) -> i32;
}
macro_rules! doit {
($($t:ident)*) => ($(impl MyFloat for $t {
fn from_u32(u: u32) -> $t { u as $t }
fn to_i32(&self) -> i32 { *self as i32 }
})*)
}
doit! { f32 f64 }
pub fn float_to_str_bytes_common<T: MyFloat, U, F>(
num: T,
digits: SignificantDigits,
exp_format: ExponentFormat,
exp_upper: bool,
f: F
) -> U where
F: FnOnce(&str) -> U,
{
let _0: T = T::zero();
let _1: T = T::one();
let radix: u32 = 10;
let radix_f = T::from_u32(radix);
assert!(num.is_nan() || num >= _0, "float_to_str_bytes_common: number is negative");
match num.classify() {
Fp::Nan => return f("NaN"),
Fp::Infinite if num > _0 => {
return f("inf");
}
Fp::Infinite if num < _0 => {
return f("-inf");
}
_ => {}
}
let mut buf = [0; 462];
let mut end = 0;
let (num, exp) = match exp_format {
ExpDec if num != _0 => {
let exp = num.log10().floor();
(num / radix_f.powf(exp), exp.to_i32())
}
_ => (num, 0)
};
let mut deccum = num.trunc();
loop {
let current_digit = deccum % radix_f;
deccum = deccum / radix_f;
deccum = deccum.trunc();
let c = char::from_digit(current_digit.to_i32() as u32, radix);
buf[end] = c.unwrap() as u8;
end += 1;
if deccum == _0 { break; }
}
let (limit_digits, digit_count, exact) = match digits {
DigMax(count) => (true, count + 1, false),
DigExact(count) => (true, count + 1, true)
};
buf[..end].reverse();
let start_fractional_digits = end;
deccum = num.fract();
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
buf[end] = b'.';
end += 1;
let mut dig = 0;
while (!limit_digits && deccum != _0)
|| (limit_digits && dig < digit_count && (
exact
|| (!exact && deccum != _0)
)
) {
deccum = deccum * radix_f;
let current_digit = deccum.trunc();
let c = char::from_digit(current_digit.to_i32() as u32, radix);
buf[end] = c.unwrap() as u8;
end += 1;
deccum = deccum.fract();
dig += 1;
}
if limit_digits && dig == digit_count {
let ascii2value = |chr: u8| {
(chr as char).to_digit(radix).unwrap()
};
let value2ascii = |val: u32| {
char::from_digit(val, radix).unwrap() as u8
};
let extra_digit = ascii2value(buf[end - 1]);
end -= 1;
if extra_digit >= radix / 2 {
let mut i: isize = end as isize - 1;
loop {
if i < 0
|| buf[i as usize] == b'-'
|| buf[i as usize] == b'+' {
for j in ((i + 1) as usize..end).rev() {
buf[j + 1] = buf[j];
}
buf[(i + 1) as usize] = value2ascii(1);
end += 1;
break;
}
if buf[i as usize] == b'.' { i -= 1; continue; }
let current_digit = ascii2value(buf[i as usize]);
if current_digit < (radix - 1) {
buf[i as usize] = value2ascii(current_digit+1);
break;
} else {
buf[i as usize] = value2ascii(0);
i -= 1;
}
}
}
}
}
if !exact {
let buf_max_i = end - 1;
let mut i = buf_max_i;
while i > start_fractional_digits && buf[i] == b'0' {
i -= 1;
}
if i >= start_fractional_digits {
if buf[i] == b'.' { i -= 1 }
if i < buf_max_i {
end = i + 1;
}
}
}
else {
let max_i = end - 1;
if buf[max_i] == b'.' {
end = max_i;
}
}
match exp_format {
ExpNone => {},
ExpDec => {
buf[end] = if exp_upper { b'E' } else { b'e' };
end += 1;
struct Filler<'a> {
buf: &'a mut [u8],
end: &'a mut usize,
}
impl<'a> fmt::Write for Filler<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
slice::bytes::copy_memory(s.as_bytes(),
&mut self.buf[(*self.end)..]);
*self.end += s.len();
Ok(())
}
}
let mut filler = Filler { buf: &mut buf, end: &mut end };
let _ = fmt::write(&mut filler, format_args!("{:-}", exp));
}
}
f(unsafe { str::from_utf8_unchecked(&buf[..end]) })
}