MCPcopy
hub / github.com/pquerna/ffjson / AppendFloat

Function AppendFloat

fflib/v1/ftoa.go:41–145  ·  view source on GitHub ↗

AppendFloat appends the string form of the floating-point number f, as generated by FormatFloat

(dst EncodingBuffer, val float64, fmt byte, prec, bitSize int)

Source from the content-addressed store, hash-verified

39// AppendFloat appends the string form of the floating-point number f,
40// as generated by FormatFloat
41func AppendFloat(dst EncodingBuffer, val float64, fmt byte, prec, bitSize int) {
42 var bits uint64
43 var flt *floatInfo
44 switch bitSize {
45 case 32:
46 bits = uint64(math.Float32bits(float32(val)))
47 flt = &float32info
48 case 64:
49 bits = math.Float64bits(val)
50 flt = &float64info
51 default:
52 panic("strconv: illegal AppendFloat/FormatFloat bitSize")
53 }
54
55 neg := bits>>(flt.expbits+flt.mantbits) != 0
56 exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
57 mant := bits & (uint64(1)<<flt.mantbits - 1)
58
59 switch exp {
60 case 1<<flt.expbits - 1:
61 // Inf, NaN
62 var s string
63 switch {
64 case mant != 0:
65 s = "NaN"
66 case neg:
67 s = "-Inf"
68 default:
69 s = "+Inf"
70 }
71 dst.WriteString(s)
72 return
73
74 case 0:
75 // denormalized
76 exp++
77
78 default:
79 // add implicit top bit
80 mant |= uint64(1) << flt.mantbits
81 }
82 exp += flt.bias
83
84 // Pick off easy binary format.
85 if fmt == 'b' {
86 fmtB(dst, neg, mant, exp, flt)
87 return
88 }
89
90 if !optimize {
91 bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
92 return
93 }
94
95 var digs decimalSlice
96 ok := false
97 // Negative precision means "only as much as needed to be exact."
98 shortest := prec < 0

Callers

nothing calls this directly

Calls 8

AssignComputeBoundsMethod · 0.95
ShortestDecimalMethod · 0.95
FixedDecimalMethod · 0.95
fmtBFunction · 0.70
bigFtoaFunction · 0.70
maxFunction · 0.70
formatDigitsFunction · 0.70
WriteStringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…