package big
import (
)
const ratGobVersion byte = 1
func ( *Rat) () ([]byte, error) {
if == nil {
return nil, nil
}
:= make([]byte, 1+4+(len(.a.abs)+len(.b.abs))*_S)
:= .b.abs.bytes()
:= .a.abs.bytes([:])
:= -
if int(uint32()) != {
return nil, errors.New("Rat.GobEncode: numerator too large")
}
byteorder.BEPutUint32([-4:], uint32())
-= 1 + 4
:= ratGobVersion << 1
if .a.neg {
|= 1
}
[] =
return [:], nil
}
func ( *Rat) ( []byte) error {
if len() == 0 {
* = Rat{}
return nil
}
if len() < 5 {
return errors.New("Rat.GobDecode: buffer too small")
}
:= [0]
if >>1 != ratGobVersion {
return fmt.Errorf("Rat.GobDecode: encoding version %d not supported", >>1)
}
const = 1 + 4
:= byteorder.BEUint32([-4 : ])
if uint64() > math.MaxInt- {
return errors.New("Rat.GobDecode: invalid length")
}
:= + int()
if len() < {
return errors.New("Rat.GobDecode: buffer too small")
}
.a.neg = &1 != 0
.a.abs = .a.abs.setBytes([:])
.b.abs = .b.abs.setBytes([:])
return nil
}
func ( *Rat) ( []byte) ([]byte, error) {
if .IsInt() {
return .a.AppendText()
}
return .marshal(), nil
}
func ( *Rat) () ( []byte, error) {
return .AppendText(nil)
}
func ( *Rat) ( []byte) error {
if , := .SetString(string()); ! {
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", )
}
return nil
}