func TestFormatMoney(t *testing.T) {
	accounting := Accounting{Symbol: "$", Precision: 2}
	AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123,456,789.21")
	AssertEqual(t, accounting.FormatMoney(12345678), "$12,345,678.00")
	AssertEqual(t, accounting.FormatMoney(-12345678), "$-12,345,678.00")
	AssertEqual(t, accounting.FormatMoney(0), "$0.00")
	AssertEqual(t, accounting.FormatMoney(big.NewRat(77777777, 3)), "$25,925,925.67")
	AssertEqual(t, accounting.FormatMoney(big.NewRat(-77777777, 3)), "$-25,925,925.67")

	accounting = Accounting{Symbol: "$", Precision: 0, Format: "%s %v"}
	AssertEqual(t, accounting.FormatMoney(123456789.213123), "$ 123,456,789")
	AssertEqual(t, accounting.FormatMoney(12345678), "$ 12,345,678")
	AssertEqual(t, accounting.FormatMoney(-12345678), "$ -12,345,678")
	AssertEqual(t, accounting.FormatMoney(0), "$ 0")

	accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
	AssertEqual(t, accounting.FormatMoney(4999.99), "€4.999,99")
	AssertEqual(t, accounting.FormatMoney(-4999.99), "€-4.999,99")

	accounting = Accounting{Symbol: "£ ", Precision: 0}
	AssertEqual(t, accounting.FormatMoney(-500000), "£ -500,000")

	accounting = Accounting{Symbol: "GBP", Precision: 0,
		Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
	AssertEqual(t, accounting.FormatMoney(1000000), "GBP 1,000,000")
	AssertEqual(t, accounting.FormatMoney(-5000), "GBP (5,000)")
	AssertEqual(t, accounting.FormatMoney(0), "GBP --")
}
Exemple #2
0
// TestTargetMul probes the Mul function of the target type.
func TestTargetMul(t *testing.T) {
	var target2, target6, target10, target14, target20 Target
	target2[crypto.HashSize-1] = 2
	target6[crypto.HashSize-1] = 6
	target10[crypto.HashSize-1] = 10
	target14[crypto.HashSize-1] = 14
	target20[crypto.HashSize-1] = 20

	// Multiplying the difficulty of a target at '10' by 5 will yeild a target
	// of '2'. Similar math follows for the remaining checks.
	expect2 := target10.MulDifficulty(big.NewRat(5, 1))
	if expect2 != target2 {
		t.Error(expect2)
		t.Error(target2)
		t.Error("Target.Mul did not work as expected")
	}
	expect6 := target10.MulDifficulty(big.NewRat(3, 2))
	if expect6 != target6 {
		t.Error("Target.Mul did not work as expected")
	}
	expect14 := target10.MulDifficulty(big.NewRat(7, 10))
	if expect14 != target14 {
		t.Error("Target.Mul did not work as expected")
	}
	expect20 := target10.MulDifficulty(big.NewRat(1, 2))
	if expect20 != target20 {
		t.Error("Target.Mul did not work as expected")
	}
}
Exemple #3
0
func (self *Server) SubmitHashrate(mr *Miner) error {
	claim := mr.getClaimedHashrate()
	actual := mr.getTrueHashrate()

	uiHashrate := big.NewRat(1, 1)
	uiHashrate.SetFrac(claim, big.NewInt(1))

	upperBound := big.NewRat(2, 1)
	lowerBound := big.NewRat(1, 2)

	actualRat := big.NewRat(1, 1)
	actualRat.SetFrac(actual, big.NewInt(1))
	upperBound.Mul(upperBound, actualRat)
	lowerBound.Mul(lowerBound, actualRat)

	if uiHashrate.Cmp(upperBound) > 0 {
		uiHashrate.Set(upperBound)
	}

	if uiHashrate.Cmp(lowerBound) < 0 {
		uiHashrate.Set(lowerBound)
	}

	fhashrate, _ := uiHashrate.Float64()

	msg := get_jsonHashrate(mr.address, big.NewInt(int64(fhashrate)))

	self.SendMessage("addHashes", msg)

	return error(nil)
}
Exemple #4
0
func computeMul(lst []int) *big.Rat {
	res := big.NewRat(1, 1)
	for _, v := range lst {
		res.Mul(res, big.NewRat(int64(v), 1))
	}
	return res
}
Exemple #5
0
// for PPLNS, not used
func getAccountShare(divvy, hashes, totalHashes *big.Int) *big.Int {
	zero := big.NewInt(0)

	rat := big.NewRat(1.0, 1.0)
	divvyRat := big.NewRat(1.0, 1.0)
	share := big.NewRat(1.0, 1.0)
	intShare := big.NewInt(0)
	cut := big.NewRat(1.0, 1.0)
	cut.SetFloat64(1.0 - HOUSE_RAKE)

	if hashes.Cmp(zero) == 0 {
		return zero
	}

	if totalHashes.Cmp(big.NewInt(0)) >= 0 {
		rat.SetFrac(hashes, totalHashes)
		rat.Mul(rat, cut)
		divvyRat.SetInt(divvy)
		share.Mul(rat, divvyRat)

		intShare.Set(share.Num())
		intShare.Div(intShare, share.Denom())
	}

	return intShare
}
Exemple #6
0
func main() {
	const (
		xmin, ymin, xmax, ymax = -2, -2, +2, +2
		width, height          = 1024, 1024
	)

	img := image.NewRGBA(image.Rect(0, 0, width, height))
	for py := 0; py < height; py++ {
		y := float64(py)/height*(ymax-ymin) + ymin
		for px := 0; px < width; px++ {
			x := float64(px)/width*(xmax-xmin) + xmin
			//			z := complex(x, y)
			// Image point (px, py) represents complex value z.
			//			img.Set(px, py, mandelbrot128(z))
			k := mandelbrotRat(big.NewRat(0, 1).SetFloat64(x), big.NewRat(0, 1).SetFloat64(y))
			fmt.Printf("%v,%v,%v\n", py, px, k)
			img.Set(px, py, k)
		}
	}
	fmt.Printf("Create\n")
	out, _ := os.OpenFile("out8.png", os.O_WRONLY|os.O_CREATE, 0600)
	defer out.Close()
	png.Encode(out, img)
	fmt.Printf("Done\n")
}
Exemple #7
0
func TestNewUsageByPathWithQuantity(t *testing.T) {
	Convey("Testing NewUsageByPathWithQuantity()", t, func() {
		usage := NewUsageByPathWithQuantity("/compute/c1/run", big.NewRat(1, 1))
		So(usage.PricingObject.Path, ShouldEqual, "/compute/c1/run")
		So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
	})
}
Exemple #8
0
func calcBalances(calcAccts []calculatedAccount, balances []*ledger.Account) (results []*ledger.Account) {
	accVals := make(map[string]*big.Rat)
	for _, calcAccount := range calcAccts {
		for _, bal := range balances {
			for _, acctOp := range calcAccount.AccountOperations {
				if acctOp.Name == bal.Name {
					fval := big.NewRat(1, 1).Abs(bal.Balance)
					aval, found := accVals[calcAccount.Name]
					if !found {
						aval = big.NewRat(0, 1)
					}
					switch acctOp.Operation {
					case "+":
						aval.Add(aval, fval)
					case "-":
						aval.Sub(aval, fval)
					}
					accVals[calcAccount.Name] = aval
				}
			}
		}
	}

	for _, calcAccount := range calcAccts {
		results = append(results, &ledger.Account{Name: calcAccount.Name, Balance: accVals[calcAccount.Name]})
	}

	return
}
Exemple #9
0
// targetAdjustmentBase returns the magnitude that the target should be
// adjusted by before a clamp is applied.
func (bn *blockNode) targetAdjustmentBase() *big.Rat {
	// Target only adjusts twice per window.
	if bn.height%(types.TargetWindow/2) != 0 {
		return big.NewRat(1, 1)
	}

	// Grab the block that was generated 'TargetWindow' blocks prior to the
	// parent. If there are not 'TargetWindow' blocks yet, stop at the genesis
	// block.
	var windowSize types.BlockHeight
	windowStart := bn
	for windowSize = 0; windowSize < types.TargetWindow && windowStart.parent != nil; windowSize++ {
		windowStart = windowStart.parent
	}

	// The target of a child is determined by the amount of time that has
	// passed between the generation of its immediate parent and its
	// TargetWindow'th parent. The expected amount of seconds to have passed is
	// TargetWindow*BlockFrequency. The target is adjusted in proportion to how
	// time has passed vs. the expected amount of time to have passed.
	//
	// The target is converted to a big.Rat to provide infinite precision
	// during the calculation. The big.Rat is just the int representation of a
	// target.
	timePassed := bn.block.Timestamp - windowStart.block.Timestamp
	expectedTimePassed := types.BlockFrequency * windowSize
	return big.NewRat(int64(timePassed), int64(expectedTimePassed))
}
Exemple #10
0
func TestMaxMin(t *testing.T) {
	a := big.NewRat(3, 1)
	minA := big.NewRat(1, 1)
	maxA := big.NewRat(10, 1)
	expected := big.NewRat(2, 9)
	result := MaxMin(maxA, minA, a)
	checkRats(expected, result, t)
}
Exemple #11
0
func TestSub(t *testing.T) {
	x := big.NewRat(24, 1)
	y := big.NewRat(6, 1)
	z := big.NewRat(18, 1)
	result := Sub(x, y)
	checkRats(z, result, t)

}
Exemple #12
0
func TestMul(t *testing.T) {
	x := big.NewRat(4, 1)
	y := big.NewRat(6, 1)
	z := big.NewRat(24, 1)
	result := Mul(x, y)
	checkRats(z, result, t)

}
func TestNewUsageWithQuantity(t *testing.T) {
	Convey("Testing NewUsageWithQuantity()", t, func() {
		object := CurrentPricing.GetByPath("/compute/c1/run")
		usage := NewUsageWithQuantity(object, big.NewRat(1, 1))
		So(usage.Object.Path, ShouldEqual, "/compute/c1/run")
		So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
	})
}
Exemple #14
0
func TestNormalizeSymbol(t *testing.T) {
	symbolMax := big.NewRat(1, 1)
	symbolMin := big.NewRat(0, 1)
	symbol := big.NewRat(10, 100)
	expected := MaxMin(symbolMax, symbolMin, symbol)
	result := NormalizeSymbol(symbol)
	checkRats(expected, result, t)
}
Exemple #15
0
func parseConditionalMultiplier(s string) (*big.Rat, error) {
	rat := big.NewRat(0, 1)
	rat.SetString(s)
	if rat.Cmp(big.NewRat(1, 1)) > 0 {
		return rat, errors.New("conditional multiplier is larger than 1")
	}
	return rat, nil
}
Exemple #16
0
func TestNormalizeTimestamp(t *testing.T) {
	timestampMax := big.NewRat(2284438026, 1)
	timestampMin := big.NewRat(1116828426, 1)
	timestamp := big.NewRat(1365812301, 1)
	expected := MaxMin(timestampMax, timestampMin, timestamp)
	result := NormalizeTimestamp(timestamp)
	checkRats(expected, result, t)
}
Exemple #17
0
func TestNormalizeVolume(t *testing.T) {
	volumeMax := big.NewRat(1000000, 1)
	volumeMin := big.NewRat(1, 1000000)
	volume := big.NewRat(4, 1)
	expected := MaxMin(volumeMax, volumeMin, volume)
	result := NormalizeVolume(volume)
	checkRats(expected, result, t)
}
Exemple #18
0
// Recursively return the tail end of the continued fraction for sqrt(D).
func tail(values []int) *big.Rat {
	next, values := values[0], values[1:]
	x := big.NewRat(int64(next), 1)
	if len(values) == 0 {
		return x
	}
	one := big.NewRat(1, 1)
	return new(big.Rat).Add(x, new(big.Rat).Quo(one, tail(values)))
}
Exemple #19
0
// Check our Magic!
func TestNormalizePrice(t *testing.T) {
	price := big.NewRat(114, 1)
	priceMax := big.NewRat(1000000, 1)
	priceMin := big.NewRat(1, 1000000)
	expected := MaxMin(priceMax, priceMin, price)
	result := NormalizePrice(price)

	checkRats(expected, result, t)
}
Exemple #20
0
func dyINT2REAL() {
	putDyadic(types.REAL, types.INTEGER, ops.Quot,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Quo(l, r)
		}))))
	putDyadic(types.INTEGER, types.REAL, ops.Quot,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Quo(l, r)
		}))))
	putDyadic(types.REAL, types.INTEGER, ops.Pow,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			n := l.Num()
			d := l.Denom()
			assert.For(r.IsInt(), 40)
			p := r.Num()
			assert.For(p.Cmp(big.NewInt(0)) >= 0, 40, "positive only")
			n = n.Exp(n, p, nil)
			d = d.Exp(d, p, nil)
			ret := big.NewRat(0, 1)
			ret = ret.SetFrac(n, d)
			return ret
		}))))
	putDyadic(types.INTEGER, types.REAL, ops.Pow,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			assert.For(l.IsInt(), 40)
			n := l.Num()
			p := r.Num()
			q := r.Denom()
			assert.For(p.Cmp(big.NewInt(0)) >= 0, 40, "positive only")
			assert.For(q.Cmp(big.NewInt(1)) == 0, 41, "извлечение корня не поддерживается")
			n = n.Exp(n, p, q)
			ret := big.NewRat(0, 1)
			ret = ret.SetFrac(n, big.NewInt(1))
			return ret
		}))))
	putDyadic(types.INTEGER, types.REAL, ops.Prod,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Mul(l, r)
		}))))
	putDyadic(types.REAL, types.INTEGER, ops.Prod,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Mul(l, r)
		}))))
	putDyadic(types.REAL, types.INTEGER, ops.Sum,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Add(l, r)
		}))))
	putDyadic(types.REAL, types.INTEGER, ops.Diff,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Sub(l, r)
		}))))
	putDyadic(types.INTEGER, types.INTEGER, ops.Quot,
		r_(r_ir_(r_ir_ir_(func(l *big.Rat, r *big.Rat) *big.Rat {
			return l.Quo(l, r)
		}))))
}
Exemple #21
0
func TestSearchBigRats(t *testing.T) {
	const N = 1e1
	s := make(BigRatSlice, N)
	for i := range s {
		s[i] = big.NewRat(int64(2*i), 1)
	}
	if g, e := SearchBigRats(s, big.NewRat(12, 1)), 6; g != e {
		t.Fatal(g, e)
	}
}
// Returns hash if they intersect, otherwise nil
func Intersect(P1, Q1, P2, Q2 Vec) ([20]byte, bool) {
	// Vectors with same length and direction as line segments
	v1 := Q1.Sub(P1)
	v2 := Q2.Sub(P2)

	// Parallel vectors never intersect
	det := v2.X*v1.Y - v1.X*v2.Y
	if det == 0 {
		return sha1nil, false
	}

	// Intersection is calculated using linear algebra
	var k1, k2 big.Rat
	Pdx, Pdy := P2.X-P1.X, P2.Y-P1.Y
	invDet := big.NewRat(1, det)
	k1.Mul(invDet, big.NewRat(-v2.Y*Pdx+v2.X*Pdy, 1))
	k2.Mul(invDet, big.NewRat(-v1.Y*Pdx+v1.X*Pdy, 1))

	// Check if intersection is inside both segments
	zero := big.NewRat(0, 1)
	one := big.NewRat(1, 1)
	k1valid := k1.Cmp(zero) == 1 && k1.Cmp(one) == -1
	k2valid := k2.Cmp(zero) == 1 && k2.Cmp(one) == -1

	// Return hash of intersection coordinate if it was
	if k1valid && k2valid {
		var Ix, Iy big.Rat
		Ix.Mul(big.NewRat(v1.X, 1), &k1).Add(&Ix, big.NewRat(P1.X, 1))
		Iy.Mul(big.NewRat(v1.Y, 1), &k1).Add(&Iy, big.NewRat(P1.Y, 1))
		return sha1.Sum([]byte(fmt.Sprintf("%v,%v", Ix.String(), Iy.String()))), true
	} else {
		return sha1nil, false
	}
}
Exemple #23
0
func TestMedianOfSorted(t *testing.T) {
	for _, test := range []struct {
		l    []interface{}
		i    int
		want *big.Rat
		err  error
	}{
		// Test for errors.
		{[]interface{}{"x"}, 0, nil, ErrType},
		{[]interface{}{0, 2, 1, 3}, 0, nil, ErrSort},
		{[]interface{}{0, 1, 2}, -1, nil, ErrNode},

		// Test lists with cycle.
		{[]interface{}{1}, 0, big.NewRat(1, 1), nil},
		{[]interface{}{0, 1}, 0, big.NewRat(1, 2), nil},
		{[]interface{}{0, 1, 2}, 0, big.NewRat(1, 1), nil},
		{[]interface{}{0, 1, 2, 3}, 0, big.NewRat(1+2, 2), nil},
		{[]interface{}{10, 20, 30}, 1, big.NewRat(20, 1), nil},
		{[]interface{}{10, 20, 30, 40}, 2, big.NewRat(20+30, 2), nil},
		{[]interface{}{10, 20, 30, 40, 50}, 4, big.NewRat(30, 1), nil},

		// Test lists without cycle.
		{[]interface{}{}, -1, nil, nil},
		{[]interface{}{0, 1, 2, 3, 4, 5}, -1, big.NewRat(2+3, 2), nil},
		{[]interface{}{10, 20, 30, 40, 50}, -1, big.NewRat(30, 1), nil},
	} {
		l, n := lists.CreateCycle(test.l, test.i)
		if test.err == ErrNode { // Setup an unknown node for an ErrNode test.
			n = &lists.Node{}
		}
		if got, err := MedianOfSorted(l, n); !reflect.DeepEqual(got, test.want) || err != test.err {
			t.Errorf("MedianOfSorted(%v, %v) = %v, %v; want %v, %v", test.l, n, got, err, test.want, test.err)
		}
	}
}
Exemple #24
0
// TestCurrencyMulRat probes the MulRat function of the currency type.
func TestCurrencyMulRat(t *testing.T) {
	c5 := NewCurrency64(5)
	c7 := NewCurrency64(7)
	c10 := NewCurrency64(10)
	if c5.MulRat(big.NewRat(2, 1)).Cmp(c10) != 0 {
		t.Error("Multiplying 5 by 2 should return 10")
	}
	if c5.MulRat(big.NewRat(3, 2)).Cmp(c7) != 0 {
		t.Error("Multiplying 5 by 1.5 should return 7")
	}
}
Exemple #25
0
// assumes 5Eth block payout, calculates the worth of a share.
func calculateSharePayout(difficulty, poolDifficulty *big.Int) float64 {
	fiveEther := big.NewRat(1, 1)
	fiveEther.SetFrac(big.NewInt(5000000000000000000), big.NewInt(1))
	sharePrice := big.NewRat(1, 1)
	sharePrice.SetFrac(difficulty, poolDifficulty)
	sharePrice.Mul(sharePrice, fiveEther)
	shareFloat, _ := sharePrice.Float64()

	shareFloat *= (1.0 - HOUSE_RAKE)
	return shareFloat
}
Exemple #26
0
// Return the nth convergent of the continued fraction for e.
func e(n int) *big.Rat {
	two := big.NewRat(2, 1)
	if n == 1 {
		return two
	}
	// Collect the first n-1 partial values of e.
	values := partialValues(n - 1)
	// Construct the continued fraction, where 'tail' is the recursive component.
	one := big.NewRat(1, 1)
	return new(big.Rat).Add(two, new(big.Rat).Quo(one, tail(values)))
}
Exemple #27
0
func isCurious(ab, cd int) (is bool, ad *big.Rat) {
	a := ab / 10
	b := ab % 10
	c := cd / 10
	d := cd % 10
	if d != 0 && b == c {
		abcd := big.NewRat(int64(ab), int64(cd))
		ad = big.NewRat(int64(a), int64(d))
		is = abcd.Cmp(ad) == 0
	}
	return
}
Exemple #28
0
func Newt_Sqrt(a *big.Rat, x *big.Rat) *big.Rat {
	for i := 0; i < 12; i++ {
		var quot1 = big.NewRat(1, 1)
		quot1 = quot1.Quo(x, big.NewRat(2, 1))
		var mul = big.NewRat(1, 1)
		mul = mul.Mul(big.NewRat(2, 1), x)
		var quot2 = big.NewRat(1, 1)
		quot2 = quot2.Quo(a, mul)
		x = x.Add(quot1, quot2)
	}
	return x
}
Exemple #29
0
func problem71() *big.Int {
	limit := big.NewInt(1000000)
	left := big.NewRat(2, 5)
	right := big.NewRat(3, 7)
	for {
		med := mediant(left, right)
		if med.Denom().Cmp(limit) == 1 { // denom > limit
			return left.Num()
		}
		left = med
	}
}
Exemple #30
0
func main() {
	var total int64 = 0
	for i := 2; i <= 99; i++ {
		var a = big.NewRat(int64(i), 1)
		var x = big.NewRat(int64(i-1), 1)
		var sqrt = big.NewRat(1, 1)
		sqrt = Newt_Sqrt(a, x)
		dec_string := sqrt.FloatString(102)
		var partial_sum = Calc_Sum(dec_string)
		total += partial_sum
	}
	fmt.Println(total)
}