Beispiel #1
0
func TestDecScan(t *testing.T) {
	tmp := new(inf.Dec)
	for i, test := range decStringTests {
		if test.scale < 0 {
			// SetString only supports scale >= 0
			continue
		}
		// initialize to a non-zero value so that issues with parsing
		// 0 are detected
		tmp.Set(inf.NewDec(1234567890, 123))
		n1, n2 := new(inf.Dec), tmp
		nn1, err1 := fmt.Sscan(test.in, n1)
		nn2, err2 := fmt.Sscan(test.in, n2)
		if !test.scanOk {
			if err1 == nil || err2 == nil {
				t.Errorf("#%d (input '%s') ok incorrect, should be %t", i, test.in, test.scanOk)
			}
			continue
		}
		expected := inf.NewDec(test.val, test.scale)
		if nn1 != 1 || err1 != nil || nn2 != 1 || err2 != nil {
			t.Errorf("#%d (input '%s') error %d %v, %d %v", i, test.in, nn1, err1, nn2, err2)
			continue
		}
		if n1.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
		}
		if n2.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
		}
	}
}
func ExampleDec_QuoExact_fail() {
	// 1 / 3 is an infinite decimal; it has no exact Dec representation
	x, y := inf.NewDec(1, 0), inf.NewDec(3, 0)
	z := new(inf.Dec).QuoExact(x, y)
	fmt.Println(z)
	// Output: <nil>
}
func ExampleDec_QuoExact_ok() {
	// 1 / 25 is a finite decimal; it has exact Dec representation
	x, y := inf.NewDec(1, 0), inf.NewDec(25, 0)
	z := new(inf.Dec).QuoExact(x, y)
	fmt.Println(z)
	// Output: 0.04
}
func ExampleDec_QuoRound_scale2RoundCeil() {
	// -42 / 400 is an finite decimal with 3 digits beyond the decimal point
	x, y := inf.NewDec(-42, 0), inf.NewDec(400, 0)
	// use 2 digits beyond decimal point, round towards positive infinity
	z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundCeil)
	fmt.Println(z)
	// Output: -0.10
}
func ExampleDec_QuoRound_scale2RoundDown() {
	// 10 / 3 is an infinite decimal; it has no exact Dec representation
	x, y := inf.NewDec(10, 0), inf.NewDec(3, 0)
	// use 2 digits beyond the decimal point, round towards 0
	z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundDown)
	fmt.Println(z)
	// Output: 3.33
}
Beispiel #6
0
func TestDecSetString(t *testing.T) {
	tmp := new(inf.Dec)
	for i, test := range decStringTests {
		if test.scale < 0 {
			// SetString only supports scale >= 0
			continue
		}
		// initialize to a non-zero value so that issues with parsing
		// 0 are detected
		tmp.Set(inf.NewDec(1234567890, 123))
		n1, ok1 := new(inf.Dec).SetString(test.in)
		n2, ok2 := tmp.SetString(test.in)
		expected := inf.NewDec(test.val, test.scale)
		if ok1 != test.ok || ok2 != test.ok {
			t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok)
			continue
		}
		if !ok1 {
			if n1 != nil {
				t.Errorf("#%d (input '%s') n1 != nil", i, test.in)
			}
			continue
		}
		if !ok2 {
			if n2 != nil {
				t.Errorf("#%d (input '%s') n2 != nil", i, test.in)
			}
			continue
		}

		if n1.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
		}
		if n2.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
		}
	}
}
func dec(i int64, exponent int) *inf.Dec {
	// See the below test-- scale is the negative of an exponent.
	return inf.NewDec(i, inf.Scale(-exponent))
}
Beispiel #8
0
// NewMilliQuantity returns a new Quantity representing the given
// value * 1/1000 in the given format. Note that BinarySI formatting
// will round fractional values, and will be changed to DecimalSI for
// values x where (-1 < x < 1) && (x != 0).
func NewMilliQuantity(value int64, format Format) *Quantity {
	return &Quantity{
		Amount: inf.NewDec(value, 3),
		Format: format,
	}
}
Beispiel #9
0
	splitRE = regexp.MustCompile(splitREString)

	// Errors that could happen while parsing a string.
	ErrFormatWrong = errors.New("quantities must match the regular expression '" + splitREString + "'")
	ErrNumeric     = errors.New("unable to parse numeric part of quantity")
	ErrSuffix      = errors.New("unable to parse quantity's suffix")

	// Commonly needed big.Int values-- treat as read only!
	bigTen      = big.NewInt(10)
	bigZero     = big.NewInt(0)
	bigOne      = big.NewInt(1)
	bigThousand = big.NewInt(1000)
	big1024     = big.NewInt(1024)

	// Commonly needed inf.Dec values-- treat as read only!
	decZero      = inf.NewDec(0, 0)
	decOne       = inf.NewDec(1, 0)
	decMinusOne  = inf.NewDec(-1, 0)
	decThousand  = inf.NewDec(1000, 0)
	dec1024      = inf.NewDec(1024, 0)
	decMinus1024 = inf.NewDec(-1024, 0)

	// Largest (in magnitude) number allowed.
	maxAllowed = inf.NewDec((1<<63)-1, 0) // == max int64

	// The maximum value we can represent milli-units for.
	// Compare with the return value of Quantity.Value() to
	// see if it's safe to use Quantity.MilliValue().
	MaxMilliValue = int64(((1 << 63) - 1) / 1000)
)
Beispiel #10
0
	"encoding/gob"
	"fmt"
	"math/big"
	"strings"
	"testing"

	"github.com/coreos/rkt/Godeps/_workspace/src/speter.net/go/exp/math/dec/inf"
)

type decFunZZ func(z, x, y *inf.Dec) *inf.Dec
type decArgZZ struct {
	z, x, y *inf.Dec
}

var decSumZZ = []decArgZZ{
	{inf.NewDec(0, 0), inf.NewDec(0, 0), inf.NewDec(0, 0)},
	{inf.NewDec(1, 0), inf.NewDec(1, 0), inf.NewDec(0, 0)},
	{inf.NewDec(1111111110, 0), inf.NewDec(123456789, 0), inf.NewDec(987654321, 0)},
	{inf.NewDec(-1, 0), inf.NewDec(-1, 0), inf.NewDec(0, 0)},
	{inf.NewDec(864197532, 0), inf.NewDec(-123456789, 0), inf.NewDec(987654321, 0)},
	{inf.NewDec(-1111111110, 0), inf.NewDec(-123456789, 0), inf.NewDec(-987654321, 0)},
	{inf.NewDec(12, 2), inf.NewDec(1, 1), inf.NewDec(2, 2)},
}

var decProdZZ = []decArgZZ{
	{inf.NewDec(0, 0), inf.NewDec(0, 0), inf.NewDec(0, 0)},
	{inf.NewDec(0, 0), inf.NewDec(1, 0), inf.NewDec(0, 0)},
	{inf.NewDec(1, 0), inf.NewDec(1, 0), inf.NewDec(1, 0)},
	{inf.NewDec(-991*991, 0), inf.NewDec(991, 0), inf.NewDec(-991, 0)},
	{inf.NewDec(2, 3), inf.NewDec(1, 1), inf.NewDec(2, 2)},
	{inf.NewDec(2, -3), inf.NewDec(1, -1), inf.NewDec(2, -2)},
Beispiel #11
0
package inf_test

import (
	"math/big"
	"testing"

	"github.com/coreos/rkt/Godeps/_workspace/src/speter.net/go/exp/math/dec/inf"
)

var decRounderInputs = [...]struct {
	quo    *inf.Dec
	rA, rB *big.Int
}{
	// examples from go language spec
	{inf.NewDec(1, 0), big.NewInt(2), big.NewInt(3)},   //  5 /  3
	{inf.NewDec(-1, 0), big.NewInt(-2), big.NewInt(3)}, // -5 /  3
	{inf.NewDec(-1, 0), big.NewInt(2), big.NewInt(-3)}, //  5 / -3
	{inf.NewDec(1, 0), big.NewInt(-2), big.NewInt(-3)}, // -5 / -3
	// examples from godoc
	{inf.NewDec(-1, 1), big.NewInt(-8), big.NewInt(10)},
	{inf.NewDec(-1, 1), big.NewInt(-5), big.NewInt(10)},
	{inf.NewDec(-1, 1), big.NewInt(-2), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(-8), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(-5), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(-2), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(0), big.NewInt(1)},
	{inf.NewDec(0, 1), big.NewInt(2), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(5), big.NewInt(10)},
	{inf.NewDec(0, 1), big.NewInt(8), big.NewInt(10)},
	{inf.NewDec(1, 1), big.NewInt(2), big.NewInt(10)},
	{inf.NewDec(1, 1), big.NewInt(5), big.NewInt(10)},