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

Function writeFloat

expfmt/text_create.go:432–453  ·  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

430// a few common cases for increased efficiency. For non-hardcoded cases, it uses
431// strconv.AppendFloat to avoid allocations, similar to writeInt.
432func writeFloat(w enhancedWriter, f float64) (int, error) {
433 switch {
434 case f == 1:
435 return 1, w.WriteByte('1')
436 case f == 0:
437 return 1, w.WriteByte('0')
438 case f == -1:
439 return w.WriteString("-1")
440 case math.IsNaN(f):
441 return w.WriteString("NaN")
442 case math.IsInf(f, +1):
443 return w.WriteString("+Inf")
444 case math.IsInf(f, -1):
445 return w.WriteString("-Inf")
446 default:
447 bp := numBufPool.Get().(*[]byte)
448 *bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64)
449 written, err := w.Write(*bp)
450 numBufPool.Put(bp)
451 return written, err
452 }
453}
454
455// writeInt is equivalent to fmt.Fprint with an int64 argument but uses
456// strconv.AppendInt with a byte slice taken from a sync.Pool to avoid

Callers 2

writeSampleFunction · 0.85
writeLabelPairsFunction · 0.85

Calls 4

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

Tested by

no test coverage detected