Skip to content

Commit

Permalink
strconv: Implement Ryū algorithm for shortest float printing
Browse files Browse the repository at this point in the history
A lot of internal functions are exposed in export_test.go
in order to test and bench various levels of function calls.

Random tests have been run on a few billion values.

Implements golang#15672.

Change-Id: I028faa4f97c38f51709469f7314bfd7ec12f06dd
  • Loading branch information
Rémy Oudompheng authored and remyoudompheng committed Jan 16, 2019
1 parent a8fe241 commit f721b30
Show file tree
Hide file tree
Showing 7 changed files with 1,650 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/strconv/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,47 @@ var (
BitSizeError = bitSizeError
BaseError = baseError
)

type ExtFloat = extFloat
type ExtFloat128 = extfloat128
type ShortDecimal = decimalSlice
type Decimal = decimal

var Float64info = float64info

var BigFtoa = bigFtoa
var RoundShortest = roundShortest

func NewShortDecimal() decimalSlice {
var buf [32]byte
var d decimalSlice
d.d = buf[:]
return d
}

func ToShort(d *decimal) decimalSlice {
sd := NewShortDecimal()
copy(sd.d[:], d.d[:])
sd.nd, sd.dp = d.nd, d.dp
return sd
}

func (d1 *ShortDecimal) Equals(d2 *ShortDecimal) bool {
if d1.nd != d2.nd && d1.dp != d2.dp {
return false
}
for i := 0; i < d1.nd; i++ {
if d1.d[i] != d2.d[i] {
return false
}
}
return true
}

func ShowDecimal(d *decimalSlice) string {
if d.nd == 0 {
return "0"
}
exp := d.dp - 1
return string(d.d[0]) + "." + string(d.d[1:d.nd]) + "e" + Itoa(exp)
}
1 change: 1 addition & 0 deletions src/strconv/extfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var powersOfTen = [...]extFloat{
{0x9e19db92b4e31ba9, 1013, false}, // 10^324
{0xeb96bf6ebadf77d9, 1039, false}, // 10^332
{0xaf87023b9bf0ee6b, 1066, false}, // 10^340
{0x82c730bec1cac960, 1093, false}, // 10^348
}

// floatBits returns the bits of the float64 that best approximates
Expand Down
Loading

0 comments on commit f721b30

Please sign in to comment.