Source file
src/math/big/floatmarsh.go
1
2
3
4
5
6
7 package big
8
9 import (
10 "errors"
11 "fmt"
12 "internal/byteorder"
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 byteorder.BePutUint32(buf[2:], x.prec)
52
53 if x.form == finite {
54 byteorder.BePutUint32(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 = byteorder.BeUint32(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(byteorder.BeUint32(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 if msg := z.validate0(); msg != "" {
103 return errors.New("Float.GobDecode: " + msg)
104 }
105
106 return nil
107 }
108
109
110
111
112 func (x *Float) AppendText(b []byte) ([]byte, error) {
113 if x == nil {
114 return append(b, "<nil>"...), nil
115 }
116 return x.Append(b, 'g', -1), nil
117 }
118
119
120
121
122 func (x *Float) MarshalText() (text []byte, err error) {
123 return x.AppendText(nil)
124 }
125
126
127
128
129
130 func (z *Float) UnmarshalText(text []byte) error {
131
132 _, _, err := z.Parse(string(text), 0)
133 if err != nil {
134 err = fmt.Errorf("math/big: cannot unmarshal %q into a *big.Float (%v)", text, err)
135 }
136 return err
137 }
138
View as plain text