MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / format_float

Function format_float

src/repr/src/strconv.rs:290–330  ·  view source on GitHub ↗
(buf: &mut F, f: Fl)

Source from the content-addressed store, hash-verified

288}
289
290fn format_float<F, Fl>(buf: &mut F, f: Fl) -> Nestable
291where
292 F: FormatBuffer,
293 Fl: NumFloat + RyuFloat,
294{
295 // Use ryu rather than the standard library. ryu uses scientific notation
296 // when possible, which better matches PostgreSQL. The standard library's
297 // `ToString` implementations print all available digits, which is rather
298 // verbose.
299 //
300 // Note that we have to fix up ryu's formatting in a few cases to match
301 // PostgreSQL. PostgreSQL spells out "Infinity" in full, never emits a
302 // trailing ".0", formats positive exponents as e.g. "1e+10" rather than
303 // "1e10", and emits a negative sign for negative zero. If we need to speed
304 // up float formatting, we can look into forking ryu and making these edits
305 // directly, but for now it doesn't seem worth it.
306
307 match f.classify() {
308 FpCategory::Infinite if f.is_sign_negative() => buf.write_str("-Infinity"),
309 FpCategory::Infinite => buf.write_str("Infinity"),
310 FpCategory::Nan => buf.write_str("NaN"),
311 FpCategory::Zero if f.is_sign_negative() => buf.write_str("-0"),
312 _ => {
313 debug_assert!(f.is_finite());
314 let mut ryu_buf = ryu::Buffer::new();
315 let mut s = ryu_buf.format_finite(f);
316 if let Some(trimmed) = s.strip_suffix(".0") {
317 s = trimmed;
318 }
319 let mut chars = s.chars().peekable();
320 while let Some(ch) = chars.next() {
321 buf.write_char(ch);
322 if ch == 'e' && chars.peek() != Some(&'-') {
323 buf.write_char('+');
324 }
325 }
326 }
327 }
328
329 Nestable::Yes
330}
331
332/// Parses an `f32` from `s`.
333pub fn parse_float32(s: &str) -> Result<f32, ParseError> {

Callers 2

format_float32Function · 0.85
format_float64Function · 0.85

Calls 4

write_charMethod · 0.80
write_strMethod · 0.45
nextMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected