// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file implements encoding/decoding of Floats.

package big

import (
	
	
	
)

// Gob codec version. Permits backward-compatible changes to the encoding.
const floatGobVersion byte = 1

// GobEncode implements the gob.GobEncoder interface.
// The Float value and all its attributes (precision,
// rounding mode, accuracy) are marshaled.
func ( *Float) () ([]byte, error) {
	if  == nil {
		return nil, nil
	}

	// determine max. space (bytes) required for encoding
	 := 1 + 1 + 4 // version + mode|acc|form|neg (3+2+2+1bit) + prec
	 := 0          // number of mantissa words
	if .form == finite {
		// add space for mantissa and exponent
		 = int((.prec + (_W - 1)) / _W) // required mantissa length in words for given precision
		// actual mantissa slice could be shorter (trailing 0's) or longer (unused bits):
		// - if shorter, only encode the words present
		// - if longer, cut off unused words when encoding in bytes
		//   (in practice, this should never happen since rounding
		//   takes care of it, but be safe and do it always)
		if len(.mant) <  {
			 = len(.mant)
		}
		// len(x.mant) >= n
		 += 4 + *_S // exp + mant
	}
	 := make([]byte, )

	[0] = floatGobVersion
	 := byte(.mode&7)<<5 | byte((.acc+1)&3)<<3 | byte(.form&3)<<1
	if .neg {
		 |= 1
	}
	[1] = 
	binary.BigEndian.PutUint32([2:], .prec)

	if .form == finite {
		binary.BigEndian.PutUint32([6:], uint32(.exp))
		.mant[len(.mant)-:].bytes([10:]) // cut off unused trailing words
	}

	return , nil
}

// GobDecode implements the gob.GobDecoder interface.
// The result is rounded per the precision and rounding mode of
// z unless z's precision is 0, in which case z is set exactly
// to the decoded value.
func ( *Float) ( []byte) error {
	if len() == 0 {
		// Other side sent a nil or default value.
		* = Float{}
		return nil
	}
	if len() < 6 {
		return errors.New("Float.GobDecode: buffer too small")
	}

	if [0] != floatGobVersion {
		return fmt.Errorf("Float.GobDecode: encoding version %d not supported", [0])
	}

	 := .prec
	 := .mode

	 := [1]
	.mode = RoundingMode(( >> 5) & 7)
	.acc = Accuracy((>>3)&3) - 1
	.form = form(( >> 1) & 3)
	.neg = &1 != 0
	.prec = binary.BigEndian.Uint32([2:])

	if .form == finite {
		if len() < 10 {
			return errors.New("Float.GobDecode: buffer too small for finite form float")
		}
		.exp = int32(binary.BigEndian.Uint32([6:]))
		.mant = .mant.setBytes([10:])
	}

	if  != 0 {
		.mode = 
		.SetPrec(uint())
	}

	if  := .validate0();  != "" {
		return errors.New("Float.GobDecode: " + )
	}

	return nil
}

// MarshalText implements the encoding.TextMarshaler interface.
// Only the Float value is marshaled (in full precision), other
// attributes such as precision or accuracy are ignored.
func ( *Float) () ( []byte,  error) {
	if  == nil {
		return []byte("<nil>"), nil
	}
	var  []byte
	return .Append(, 'g', -1), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The result is rounded per the precision and rounding mode of z.
// If z's precision is 0, it is changed to 64 before rounding takes
// effect.
func ( *Float) ( []byte) error {
	// TODO(gri): get rid of the []byte/string conversion
	, ,  := .Parse(string(), 0)
	if  != nil {
		 = fmt.Errorf("math/big: cannot unmarshal %q into a *big.Float (%v)", , )
	}
	return 
}