sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame] | 1 | package jsoniter |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "strconv" |
| 6 | ) |
| 7 | |
| 8 | var pow10 []uint64 |
| 9 | |
| 10 | func init() { |
| 11 | pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000} |
| 12 | } |
| 13 | |
| 14 | // WriteFloat32 write float32 to stream |
| 15 | func (stream *Stream) WriteFloat32(val float32) { |
| 16 | abs := math.Abs(float64(val)) |
| 17 | fmt := byte('f') |
| 18 | // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. |
| 19 | if abs != 0 { |
| 20 | if float32(abs) < 1e-6 || float32(abs) >= 1e21 { |
| 21 | fmt = 'e' |
| 22 | } |
| 23 | } |
| 24 | stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32) |
| 25 | } |
| 26 | |
| 27 | // WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster |
| 28 | func (stream *Stream) WriteFloat32Lossy(val float32) { |
| 29 | if val < 0 { |
| 30 | stream.writeByte('-') |
| 31 | val = -val |
| 32 | } |
| 33 | if val > 0x4ffffff { |
| 34 | stream.WriteFloat32(val) |
| 35 | return |
| 36 | } |
| 37 | precision := 6 |
| 38 | exp := uint64(1000000) // 6 |
| 39 | lval := uint64(float64(val)*float64(exp) + 0.5) |
| 40 | stream.WriteUint64(lval / exp) |
| 41 | fval := lval % exp |
| 42 | if fval == 0 { |
| 43 | return |
| 44 | } |
| 45 | stream.writeByte('.') |
| 46 | for p := precision - 1; p > 0 && fval < pow10[p]; p-- { |
| 47 | stream.writeByte('0') |
| 48 | } |
| 49 | stream.WriteUint64(fval) |
| 50 | for stream.buf[len(stream.buf)-1] == '0' { |
| 51 | stream.buf = stream.buf[:len(stream.buf)-1] |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // WriteFloat64 write float64 to stream |
| 56 | func (stream *Stream) WriteFloat64(val float64) { |
| 57 | abs := math.Abs(val) |
| 58 | fmt := byte('f') |
| 59 | // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. |
| 60 | if abs != 0 { |
| 61 | if abs < 1e-6 || abs >= 1e21 { |
| 62 | fmt = 'e' |
| 63 | } |
| 64 | } |
| 65 | stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64) |
| 66 | } |
| 67 | |
| 68 | // WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster |
| 69 | func (stream *Stream) WriteFloat64Lossy(val float64) { |
| 70 | if val < 0 { |
| 71 | stream.writeByte('-') |
| 72 | val = -val |
| 73 | } |
| 74 | if val > 0x4ffffff { |
| 75 | stream.WriteFloat64(val) |
| 76 | return |
| 77 | } |
| 78 | precision := 6 |
| 79 | exp := uint64(1000000) // 6 |
| 80 | lval := uint64(val*float64(exp) + 0.5) |
| 81 | stream.WriteUint64(lval / exp) |
| 82 | fval := lval % exp |
| 83 | if fval == 0 { |
| 84 | return |
| 85 | } |
| 86 | stream.writeByte('.') |
| 87 | for p := precision - 1; p > 0 && fval < pow10[p]; p-- { |
| 88 | stream.writeByte('0') |
| 89 | } |
| 90 | stream.WriteUint64(fval) |
| 91 | for stream.buf[len(stream.buf)-1] == '0' { |
| 92 | stream.buf = stream.buf[:len(stream.buf)-1] |
| 93 | } |
| 94 | } |