package strconv
import (
)
const (
lowerhex = "0123456789abcdef"
upperhex = "0123456789ABCDEF"
)
const (
float32MantBits = 23
float32ExpBits = 8
float32Bias = -127
float32MinExp = -189
float64MantBits = 52
float64ExpBits = 11
float64Bias = -1023
float64MinExp = -1085
)
func ( float64, byte, , int) string {
if == 32 {
return string(ftoa32(make([]byte, 0, max(+4, 24)), float32(), , ))
}
if == 64 {
return string(ftoa64(make([]byte, 0, max(+4, 24)), , , ))
}
panic("strconv: illegal FormatFloat bitSize")
}
func ( []byte, float64, byte, , int) []byte {
if == 32 {
return ftoa32(, float32(), , )
}
if == 64 {
return ftoa64(, , , )
}
panic("strconv: illegal AppendFloat bitSize")
}
func ( []byte, float32, byte, int) []byte {
type = float32
var uint64
var , , int
switch 8 * unsafe.Sizeof() {
case 32:
= uint64(float32bits(float32()))
= float32ExpBits
= float32MantBits
= float32Bias
case 64:
= float64bits(float64())
= float64ExpBits
= float64MantBits
= float64Bias
}
:= >>(+) != 0
:= int(>>) & (1<< - 1)
:= & (1<< - 1)
if == 1<<-1 {
if != 0 {
return append(, "NaN"...)
}
if {
return append(, "-Inf"...)
}
return append(, "+Inf"...)
}
if == 0 {
++
} else {
|= 1 <<
}
+=
if == 'b' {
return fmtB(, , , -)
}
if == 'x' || == 'X' {
return fmtX(, , , , , , )
}
if == 0 {
return fmtEFG(, , nil, 0, 0, , , < 0)
}
if < 0 {
var [32]byte
:= 64 - bits.Len64()
:= <<
:= -
, := shortFloat[](, -)
, := setDigits([:], , , numDigits())
switch {
case 'e', 'E':
= max(-1, 0)
case 'f':
= max(-, 0)
case 'g', 'G':
=
}
return fmtEFG(, , [:], , , , , true)
}
if optimize {
:=
switch {
case 'f':
if >= 0 {
= 1 + log10Pow2(1+) +
} else {
= 1 + - log10Pow2(-)
}
case 'e', 'E':
++
case 'g', 'G':
if == 0 {
= 1
}
=
default:
= 1
}
if <= 18 {
var [24]byte
var , int
if > 0 {
:= 64 - bits.Len64()
:= <<
:= -
, := fixedWidthFloat(, -, , , )
if != 0 {
, = setDigits([:], , , numDigits())
}
}
return fmtEFG(, , [:], , , , , false)
}
}
:= new(decimal)
.Assign()
.Shift( - )
switch {
case 'e', 'E':
.Round( + 1)
case 'f':
.Round(.dp + )
case 'g', 'G':
if == 0 {
= 1
}
.Round()
}
return fmtEFG(, , .d[:], .dp, .nd, , , false)
}
func ( []byte, float64, byte, int) []byte {
type = float64
var uint64
var , , int
switch 8 * unsafe.Sizeof() {
case 32:
= uint64(float32bits(float32()))
= float32ExpBits
= float32MantBits
= float32Bias
case 64:
= float64bits(float64())
= float64ExpBits
= float64MantBits
= float64Bias
}
:= >>(+) != 0
:= int(>>) & (1<< - 1)
:= & (1<< - 1)
if == 1<<-1 {
if != 0 {
return append(, "NaN"...)
}
if {
return append(, "-Inf"...)
}
return append(, "+Inf"...)
}
if == 0 {
++
} else {
|= 1 <<
}
+=
if == 'b' {
return fmtB(, , , -)
}
if == 'x' || == 'X' {
return fmtX(, , , , , , )
}
if == 0 {
return fmtEFG(, , nil, 0, 0, , , < 0)
}
if < 0 {
var [32]byte
:= 64 - bits.Len64()
:= <<
:= -
, := shortFloat[](, -)
, := setDigits([:], , , numDigits())
switch {
case 'e', 'E':
= max(-1, 0)
case 'f':
= max(-, 0)
case 'g', 'G':
=
}
return fmtEFG(, , [:], , , , , true)
}
if optimize {
:=
switch {
case 'f':
if >= 0 {
= 1 + log10Pow2(1+) +
} else {
= 1 + - log10Pow2(-)
}
case 'e', 'E':
++
case 'g', 'G':
if == 0 {
= 1
}
=
default:
= 1
}
if <= 18 {
var [24]byte
var , int
if > 0 {
:= 64 - bits.Len64()
:= <<
:= -
, := fixedWidthFloat(, -, , , )
if != 0 {
, = setDigits([:], , , numDigits())
}
}
return fmtEFG(, , [:], , , , , false)
}
}
:= new(decimal)
.Assign()
.Shift( - )
switch {
case 'e', 'E':
.Round( + 1)
case 'f':
.Round(.dp + )
case 'g', 'G':
if == 0 {
= 1
}
.Round()
}
return fmtEFG(, , .d[:], .dp, .nd, , , false)
}
func ( []byte, bool, []byte, , , int, byte, bool) []byte {
if == 'g' || == 'G' {
:=
if > && >= {
=
}
if {
= 6
}
:= - 1
if < -4 || >= {
if > {
=
}
--
= + 'e' - 'g'
} else {
if > {
=
}
= max(-, 0)
= 'f'
}
}
switch {
case 'e', 'E':
if {
= append(, '-')
}
:= byte('0')
if != 0 {
= [0]
}
= append(, )
if > 0 {
= append(, '.')
:= 1
:= min(, +1)
if < {
= append(, [:]...)
=
}
for range + 1 - {
= append(, '0')
}
}
= append(, )
:= - 1
if == 0 {
= 0
}
if < 0 {
= '-'
= -
} else {
= '+'
}
= append(, )
switch {
case < 10:
= append(, '0', byte()+'0')
case < 100:
= append(, byte(/10)+'0', byte(%10)+'0')
default:
= append(, byte(/100)+'0', byte(/10)%10+'0', byte(%10)+'0')
}
return
case 'f':
if {
= append(, '-')
}
if > 0 {
:= min(, )
for , := range [:] {
= append(, )
}
for range - {
= append(, '0')
}
} else {
= append(, '0')
}
if > 0 {
= append(, '.')
:= min(, max(0, -))
:= +
:= min(-, max(0, -))
:= max(0, --)
for range {
= append(, '0')
}
for := range {
= append(, [+])
}
for range {
= append(, '0')
}
}
return
}
return append(, '%', )
}
func ( []byte, bool, uint64, int) []byte {
if {
= append(, '-')
}
= AppendUint(, , 10)
= append(, 'p')
if >= 0 {
= append(, '+')
}
= AppendInt(, int64(), 10)
return
}
func ( []byte, int, byte, bool, uint64, , int) []byte {
if == 0 {
= 0
}
<<= 60 -
for != 0 && &(1<<60) == 0 {
<<= 1
--
}
if >= 0 && < 15 {
:= uint( * 4)
:= ( << ) & (1<<60 - 1)
>>= 60 -
if |(&1) > 1<<59 {
++
}
<<= 60 -
if &(1<<61) != 0 {
>>= 1
++
}
}
:= lowerhex
if == 'X' {
= upperhex
}
if {
= append(, '-')
}
= append(, '0', , '0'+byte((>>60)&1))
<<= 4
if < 0 && != 0 {
= append(, '.')
for != 0 {
= append(, [(>>60)&15])
<<= 4
}
} else if > 0 {
= append(, '.')
for := 0; < ; ++ {
= append(, [(>>60)&15])
<<= 4
}
}
:= byte('P')
if == lower() {
= 'p'
}
= append(, )
if < 0 {
= '-'
= -
} else {
= '+'
}
= append(, )
switch {
case < 100:
= append(, byte(/10)+'0', byte(%10)+'0')
case < 1000:
= append(, byte(/100)+'0', byte((/10)%10)+'0', byte(%10)+'0')
default:
= append(, byte(/1000)+'0', byte(/100)%10+'0', byte((/10)%10)+'0', byte(%10)+'0')
}
return
}