...
Source file
src/math/big/floatmarsh.go
1
2
3
4
5
6
7 package big
8
9 import (
10 "encoding/binary"
11 "errors"
12 "fmt"
13 )
14
15
16 const floatGobVersion byte = 1
17
18
19
20
21 func (x *Float) GobEncode() ([]byte, error) {
22 if x == nil {
23 return nil, nil
24 }
25
26
27 sz := 1 + 1 + 4
28 n := 0
29 if x.form == finite {
30
31 n = int((x.prec + (_W - 1)) / _W)
32
33
34
35
36
37 if len(x.mant) < n {
38 n = len(x.mant)
39 }
40
41 sz += 4 + n*_S
42 }
43 buf := make([]byte, sz)
44
45 buf[0] = floatGobVersion
46 b := byte(x.mode&7)<<5 | byte((x.acc+1)&3)<<3 | byte(x.form&3)<<1
47 if x.neg {
48 b |= 1
49 }
50 buf[1] = b
51 binary.BigEndian.PutUint32(buf[2:], x.prec)
52
53 if x.form == finite {
54 binary.BigEndian.PutUint32(buf[6:], uint32(x.exp))
55 x.mant[len(x.mant)-n:].bytes(buf[10:])
56 }
57
58 return buf, nil
59 }
60
61
62
63
64
65 func (z *Float) GobDecode(buf []byte) error {
66 if len(buf) == 0 {
67
68 *z = Float{}
69 return nil
70 }
71 if len(buf) < 6 {
72 return errors.New("Float.GobDecode: buffer too small")
73 }
74
75 if buf[0] != floatGobVersion {
76 return fmt.Errorf("Float.GobDecode: encoding version %d not supported", buf[0])
77 }
78
79 oldPrec := z.prec
80 oldMode := z.mode
81
82 b := buf[1]
83 z.mode = RoundingMode((b >> 5) & 7)
84 z.acc = Accuracy((b>>3)&3) - 1
85 z.form = form((b >> 1) & 3)
86 z.neg = b&1 != 0
87 z.prec = binary.BigEndian.Uint32(buf[2:])
88
89 if z.form == finite {
90 if len(buf) < 10 {
91 return errors.New("Float.GobDecode: buffer too small for finite form float")
92 }
93 z.exp = int32(binary.BigEndian.Uint32(buf[6:]))
94 z.mant = z.mant.setBytes(buf[10:])
95 }
96
97 if oldPrec != 0 {
98 z.mode = oldMode
99 z.SetPrec(uint(oldPrec))
100 }
101
102 return nil
103 }
104
105
106
107
108 func (x *Float) MarshalText() (text []byte, err error) {
109 if x == nil {
110 return []byte("<nil>"), nil
111 }
112 var buf []byte
113 return x.Append(buf, 'g', -1), nil
114 }
115
116
117
118
119
120 func (z *Float) UnmarshalText(text []byte) error {
121
122 _, _, err := z.Parse(string(text), 0)
123 if err != nil {
124 err = fmt.Errorf("math/big: cannot unmarshal %q into a *big.Float (%v)", text, err)
125 }
126 return err
127 }
128
View as plain text