Ejemplo n.º 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)
		}
	}
}
Ejemplo n.º 2
0
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>
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 4
0
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
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 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)
		}
	}
}
Ejemplo n.º 7
0
	"encoding/gob"
	"fmt"
	"math/big"
	"strings"
	"testing"

	"github.com/coreos/fuze/third_party/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)},
Ejemplo n.º 8
0
package inf_test

import (
	"math/big"
	"testing"

	"github.com/coreos/fuze/third_party/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)},