stream_float.go 2.7 KB

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