MCPcopy Create free account
hub / github.com/prometheus/common / writeFloat

Function writeFloat

expfmt/text_create.go:476–497  ·  view source on GitHub ↗

writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes a few common cases for increased efficiency. For non-hardcoded cases, it uses strconv.AppendFloat to avoid allocations, similar to writeInt.

(w enhancedWriter, f float64)

Source from the content-addressed store, hash-verified

474// a few common cases for increased efficiency. For non-hardcoded cases, it uses
475// strconv.AppendFloat to avoid allocations, similar to writeInt.
476func writeFloat(w enhancedWriter, f float64) (int, error) {
477 switch {
478 case f == 1:
479 return 1, w.WriteByte('1')
480 case f == 0:
481 return 1, w.WriteByte('0')
482 case f == -1:
483 return w.WriteString("-1")
484 case math.IsNaN(f):
485 return w.WriteString("NaN")
486 case math.IsInf(f, +1):
487 return w.WriteString("+Inf")
488 case math.IsInf(f, -1):
489 return w.WriteString("-Inf")
490 default:
491 bp := numBufPool.Get().(*[]byte)
492 *bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64)
493 written, err := w.Write(*bp)
494 numBufPool.Put(bp)
495 return written, err
496 }
497}
498
499// writeInt is equivalent to fmt.Fprint with an int64 argument but uses
500// strconv.AppendInt with a byte slice taken from a sync.Pool to avoid

Callers 2

writeSampleFunction · 0.85
writeNameAndLabelPairsFunction · 0.85

Calls 4

WriteByteMethod · 0.80
WriteStringMethod · 0.80
GetMethod · 0.80
PutMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…