Example #1
0
// hostconfigcmd is the handler for the command `siac host config [setting] [value]`.
// Modifies host settings.
func hostconfigcmd(param, value string) {
	switch param {
	case "price":
		// convert price to hastings/byte/block
		p, ok := new(big.Rat).SetString(value)
		if !ok {
			die("Could not parse price")
		}
		p.Mul(p, big.NewRat(1e24/1e9, 4320))
		value = new(big.Int).Div(p.Num(), p.Denom()).String()
	case "totalstorage":
		// parse sizes of form 10GB, 10TB, 1TiB etc
		var err error
		value, err = parseSize(value)
		if err != nil {
			die("Could not parse totalstorage:", err)
		}
	case "minduration", "maxduration", "windowsize", "acceptingcontracts": // Other valid settings.
	default:
		// Reject invalid host config commands.
		die("\"" + param + "\" is not a host setting")
	}
	err := post("/host", param+"="+value)
	if err != nil {
		die("Could not update host settings:", err)
	}
	fmt.Println("Host settings updated.")
}
Example #2
0
File: types.go Project: scauios/oh
func ratmod(x, y *big.Rat) *big.Rat {
	if x.IsInt() && y.IsInt() {
		return new(big.Rat).SetInt(new(big.Int).Mod(x.Num(), y.Num()))
	}

	panic("operation not permitted")
}
Example #3
0
func floor(n *big.Rat) *big.Rat {
	f := &big.Rat{}
	z := new(big.Int)
	z.Div(n.Num(), n.Denom())
	f.SetInt(z)
	return f
}
Example #4
0
func hostconfigcmd(param, value string) {
	// convert price to hastings/byte/block
	if param == "price" {
		p, ok := new(big.Rat).SetString(value)
		if !ok {
			fmt.Println("could not parse price")
			return
		}
		p.Mul(p, big.NewRat(1e24/1e9, 4320))
		value = new(big.Int).Div(p.Num(), p.Denom()).String()
	}
	// parse sizes of form 10GB, 10TB, 1TiB etc
	if param == "totalstorage" {
		var err error
		value, err = parseSize(value)
		if err != nil {
			fmt.Println("could not parse " + param)
		}
	}
	err := post("/host", param+"="+value)
	if err != nil {
		fmt.Println("Could not update host settings:", err)
		return
	}
	fmt.Println("Host settings updated.")
}
Example #5
0
// Returns a new big.Int set to the ceiling of x.
func ratCeil(x *big.Rat) *big.Int {
	z := new(big.Int)
	m := new(big.Int)
	z.DivMod(x.Num(), x.Denom(), m)
	if m.Cmp(intZero) == 1 {
		z.Add(z, intOne)
	}
	return z
}
Example #6
0
// MulRat returns a new Currency value c = x * y, where y is a big.Rat.
func (x Currency) MulRat(y *big.Rat) (c Currency) {
	if y.Sign() < 0 {
		build.Critical(ErrNegativeCurrency)
	} else {
		c.i.Mul(&x.i, y.Num())
		c.i.Div(&c.i, y.Denom())
	}
	return
}
Example #7
0
func Round(r *big.Rat) *big.Rat {
	d := new(big.Int).Set(r.Denom())
	n := new(big.Int).Set(r.Num())
	n.Mod(n, d)
	if new(big.Int).Mul(n, big.NewInt(2)).Cmp(d) >= 0 {
		r.Add(r, new(big.Rat).SetInt64(1))
	}
	r.Sub(r, new(big.Rat).SetFrac(n, d))
	return r
}
Example #8
0
func CalcGasLimit(parent *types.Block) *big.Int {
	// ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024
	previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit())
	current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5))
	curInt := new(big.Int).Div(current.Num(), current.Denom())

	result := new(big.Int).Add(previous, curInt)
	result.Div(result, big.NewInt(1024))
	return common.BigMax(params.GenesisGasLimit, result)
}
Example #9
0
// MulRat returns a new Currency value c = x * y, where y is a big.Rat.
func (x Currency) MulRat(y *big.Rat) (c Currency) {
	if y.Sign() < 0 {
		if build.DEBUG {
			panic(ErrNegativeCurrency)
		}
	} else {
		c.i.Mul(&x.i, y.Num())
		c.i.Div(&c.i, y.Denom())
	}
	return
}
Example #10
0
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd', 'W', 'M', and 'Y' suffixes.
func ParseTimeDuration(value string) (int64, error) {
	var constant time.Duration

	prefixSize := 1

	switch value[len(value)-1] {
	case 'u':
		constant = time.Microsecond
	case 's':
		constant = time.Second
	case 'm':
		constant = time.Minute
	case 'h':
		constant = time.Hour
	case 'd':
		constant = 24 * time.Hour
	case 'w', 'W':
		constant = Week
	case 'M':
		constant = Month
	case 'y', 'Y':
		constant = Year
	default:
		prefixSize = 0
	}

	if value[len(value)-2:] == "ms" {
		constant = time.Millisecond
		prefixSize = 2
	}

	t := big.Rat{}
	timeString := value
	if prefixSize > 0 {
		timeString = value[:len(value)-prefixSize]
	}

	_, err := fmt.Sscan(timeString, &t)
	if err != nil {
		return 0, err
	}

	if prefixSize > 0 {
		c := big.Rat{}
		c.SetFrac64(int64(constant), 1)
		t.Mul(&t, &c)
	}

	if t.IsInt() {
		return t.Num().Int64(), nil
	}
	f, _ := t.Float64()
	return int64(f), nil
}
Example #11
0
// RatToTarget converts a big.Rat to a Target.
func RatToTarget(r *big.Rat) (t Target) {
	if r.Num().Sign() < 0 {
		if build.DEBUG {
			panic(ErrNegativeTarget)
		}
	} else {
		i := new(big.Int).Div(r.Num(), r.Denom())
		t = IntToTarget(i)
	}
	return
}
Example #12
0
// Do the numerator and denominator of r provide a solution to the equation
// x**2 - D*(y**2) = 1?
func isSolution(D int, r *big.Rat) bool {
	S := big.NewInt(int64(D))
	x := r.Num()
	y := r.Denom()

	one := big.NewInt(1)
	two := big.NewInt(2)

	a := new(big.Int).Exp(x, two, nil)
	b := new(big.Int).Exp(y, two, nil)
	return new(big.Int).Sub(a, new(big.Int).Mul(S, b)).Cmp(one) == 0
}
Example #13
0
// COMPATv0.4.0 - until the first 10e3 blocks have been archived, MulFloat is
// needed while verifying the first set of blocks.
//
// MulFloat returns a new Currency value y = c * x, where x is a float64.
// Behavior is undefined when x is negative.
func (x Currency) MulFloat(y float64) (c Currency) {
	if y < 0 {
		build.Critical(ErrNegativeCurrency)
	} else {
		cRat := new(big.Rat).Mul(
			new(big.Rat).SetInt(&x.i),
			new(big.Rat).SetFloat64(y),
		)
		c.i.Div(cRat.Num(), cRat.Denom())
	}
	return
}
Example #14
0
File: value.go Project: 2thetop/go
func makeRat(x *big.Rat) Value {
	a := x.Num()
	b := x.Denom()
	if a.BitLen() < maxExp && b.BitLen() < maxExp {
		// ok to remain fraction
		return ratVal{x}
	}
	// components too large => switch to float
	fa := newFloat().SetInt(a)
	fb := newFloat().SetInt(b)
	return floatVal{fa.Quo(fa, fb)}
}
Example #15
0
File: types.go Project: scauios/oh
func NewRational(r *big.Rat) Rational {
	if !r.IsInt() || r.Cmp(min) < 0 || r.Cmp(max) > 0 {
		return Rational{r}
	}

	n := r.Num().Int64()
	i := n + 256
	p := rat[i]

	if p.v == nil {
		p = Rational{r}
		rat[i] = p
	}

	return p
}
Example #16
0
func (v value) toInt() *big.Int {
	switch v := v.v.(type) {
	case *big.Int:
		return v
	case *big.Rat:
		// TODO rounding?
		return new(big.Int).Quo(v.Num(), v.Denom())
	case float64:
		r := new(big.Rat).SetFloat64(v)
		if r == nil {
			fatalf("cannot convert %v to float64", v)
		}
		// TODO rounding?
		return new(big.Int).Quo(r.Num(), r.Denom())
	}
	panic(fmt.Errorf("unexpected type %T", v.v))
}
Example #17
0
func parseResourceCPU(flags *pflag.FlagSet, resources *api.Resources, name string) error {
	cpu, err := flags.GetString(name)
	if err != nil {
		return err
	}

	nanoCPUs, ok := new(big.Rat).SetString(cpu)
	if !ok {
		return fmt.Errorf("invalid cpu: %s", cpu)
	}
	cpuRat := new(big.Rat).Mul(nanoCPUs, big.NewRat(1e9, 1))
	if !cpuRat.IsInt() {
		return fmt.Errorf("CPU value cannot have more than 9 decimal places: %s", cpu)
	}
	resources.NanoCPUs = cpuRat.Num().Int64()
	return nil
}
Example #18
0
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd' and 'w' suffixes.
func ParseTimeDuration(value string) (int64, error) {
	var constant time.Duration
	hasPrefix := true

	switch value[len(value)-1] {
	case 'u':
		constant = time.Microsecond
	case 's':
		constant = time.Second
	case 'm':
		constant = time.Minute
	case 'h':
		constant = time.Hour
	case 'd':
		constant = 24 * time.Hour
	case 'w':
		constant = 7 * 24 * time.Hour
	case 'y':
		constant = 365 * 24 * time.Hour
	default:
		hasPrefix = false
	}

	t := big.Rat{}
	timeString := value
	if hasPrefix {
		timeString = value[:len(value)-1]
	}

	_, err := fmt.Sscan(timeString, &t)
	if err != nil {
		return 0, err
	}

	if hasPrefix {
		c := big.Rat{}
		c.SetFrac64(int64(constant), 1)
		t.Mul(&t, &c)
	}
	if t.IsInt() {
		return t.Num().Int64(), nil
	}
	f, _ := t.Float64()
	return int64(f), nil
}
Example #19
0
func hostconfigcmd(param, value string) {
	// convert price to hastings/byte/block
	if param == "price" {
		p, ok := new(big.Rat).SetString(value)
		if !ok {
			fmt.Println("could not parse price")
			return
		}
		p.Mul(p, big.NewRat(1e24/1e9, 4320))
		value = new(big.Int).Div(p.Num(), p.Denom()).String()
	}
	err := post("/host/configure", param+"="+value)
	if err != nil {
		fmt.Println("Could not update host settings:", err)
		return
	}
	fmt.Println("Host settings updated.")
}
Example #20
0
func (block *Block) CalcGasLimit(parent *Block) *big.Int {
	if block.Number.Cmp(big.NewInt(0)) == 0 {
		return ethutil.BigPow(10, 6)
	}

	// ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024

	previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit)
	current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed), big.NewRat(6, 5))
	curInt := new(big.Int).Div(current.Num(), current.Denom())

	result := new(big.Int).Add(previous, curInt)
	result.Div(result, big.NewInt(1024))

	min := big.NewInt(125000)

	return ethutil.BigMax(min, result)
}
Example #21
0
func NewRational(r *big.Rat) *Rational {
	if !r.IsInt() || r.Cmp(min) < 0 || r.Cmp(max) > 0 {
		return (*Rational)(r)
	}

	n := r.Num().Int64()
	i := n + 256

	ratl.RLock()
	p := rat[i]
	ratl.RUnlock()

	if p == nil {
		p = (*Rational)(r)

		ratl.Lock()
		rat[i] = p
		ratl.Unlock()
	}

	return p
}
Example #22
0
// bigRatToValue converts 'number' to an SQL value with SQL type: valueType.
// If valueType is integral it truncates 'number' to the integer part according to the
// semantics of the big.Rat.Int method.
func bigRatToValue(number *big.Rat, valueType querypb.Type) sqltypes.Value {
	var numberAsBytes []byte
	switch {
	case sqltypes.IsIntegral(valueType):
		// 'number.Num()' returns a reference to the numerator of 'number'.
		// We copy it here to avoid changing 'number'.
		truncatedNumber := new(big.Int).Set(number.Num())
		truncatedNumber.Quo(truncatedNumber, number.Denom())
		numberAsBytes = bigIntToSliceOfBytes(truncatedNumber)
	case sqltypes.IsFloat(valueType):
		// Truncate to the closest 'float'.
		// There's not much we can do if there isn't an exact representation.
		numberAsFloat64, _ := number.Float64()
		numberAsBytes = strconv.AppendFloat([]byte{}, numberAsFloat64, 'f', -1, 64)
	default:
		panic(fmt.Sprintf("Unsupported type: %v", valueType))
	}
	result, err := sqltypes.ValueFromBytes(valueType, numberAsBytes)
	if err != nil {
		panic(fmt.Sprintf("sqltypes.ValueFromBytes failed with: %v", err))
	}
	return result
}
Example #23
0
File: bigrat.go Project: zzn01/ivy
// ratExponent returns the power of ten that x would display in scientific notation.
func ratExponent(x *big.Rat) int {
	if x.Sign() < 0 {
		x.Neg(x)
	}
	e := 0
	invert := false
	if x.Num().Cmp(x.Denom()) < 0 {
		invert = true
		x.Inv(x)
		e++
	}
	for x.Cmp(bigRatBillion) >= 0 {
		e += 9
		x.Quo(x, bigRatBillion)
	}
	for x.Cmp(bigRatTen) > 0 {
		e++
		x.Quo(x, bigRatTen)
	}
	if invert {
		return -e
	}
	return e
}
Example #24
0
// See https://en.wikipedia.org/wiki/Farey_sequence
// The value of the new term in between neighbours 2/5 and 3/7 is found by
// computing the mediant of those neighbours. We can take result to be the next
// left-hand neighbour of 3/7 iteratively until the denominator reaches 1e6.
func mediant(x, y *big.Rat) *big.Rat {
	num := new(big.Int).Add(x.Num(), y.Num())
	denom := new(big.Int).Add(x.Denom(), y.Denom())
	return new(big.Rat).SetFrac(num, denom)
}
Example #25
0
// normalizeRatConst returns the smallest constant representation
// for the specific value of x; either an int64, *big.Int value,
// or *big.Rat value.
//
func normalizeRatConst(x *big.Rat) interface{} {
	if x.IsInt() {
		return normalizeIntConst(x.Num())
	}
	return x
}
Example #26
0
// Creates a new fraction from a rational number (big.Rat)
func NewFractionFromRat(rat *big.Rat) *Fraction {
	// Ignore error since *big.Rat denom can't be 0
	frac, _ := NewFraction(rat.Num().Int64(), rat.Denom().Int64())
	return frac
}
Example #27
0
func savePiState(iteration int, sum *big.Rat) {
	file, _ := os.OpenFile("pi_cache", os.O_WRONLY|os.O_CREATE, 400)
	enc := json.NewEncoder(file)
	enc.Encode(PiState{iteration, sum.Num().String(), sum.Denom().String()})
	file.Close()
}
Example #28
0
func normFloat(x *big.Rat) Value {
	if x.IsInt() {
		return normInt(x.Num())
	}
	return floatVal{x}
}
Example #29
0
// Sqrt returns a new Currency value y = sqrt(c). Result is rounded down to the
// nearest integer.
func (x Currency) Sqrt() (c Currency) {
	f, _ := new(big.Rat).SetInt(&x.i).Float64()
	sqrt := new(big.Rat).SetFloat64(math.Sqrt(f))
	c.i.Div(sqrt.Num(), sqrt.Denom())
	return
}
Example #30
0
func (f *frac) fromRat(r *big.Rat) {
	*f.Numerator = int32(r.Num().Int64())
	*f.Denominator = int32(r.Denom().Int64())
}