本文整理汇总了Golang中math/big.Rat.SetFloat64方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.SetFloat64方法的具体用法?Golang Rat.SetFloat64怎么用?Golang Rat.SetFloat64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.SetFloat64方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ratProb
func ratProb(mode int) func(*big.Rat) *big.Rat {
return func(x *big.Rat) *big.Rat {
lo := big.NewInt(0)
hi := new(big.Int).Set(big2p63)
n := 0
for lo.Cmp(hi) != 0 {
m := new(big.Int).Add(lo, hi)
m = m.Rsh(m, 1)
if n++; n > 100 {
fmt.Printf("??? %v %v %v\n", lo, hi, m)
break
}
v := new(big.Rat).SetFrac(m, big2p63)
f, _ := v.Float64()
v.SetFloat64(f)
if v.Cmp(x) < 0 {
lo.Add(m, bigOne)
} else {
hi.Set(m)
}
}
switch mode {
default: // case 0
return new(big.Rat).SetFrac(lo, big2p63)
case 1:
if lo.Cmp(big.NewInt(cutoff1)) <= 0 {
lo.Add(lo, big.NewInt(1<<63-cutoff1))
}
return new(big.Rat).SetFrac(lo, big2p63)
case 2:
return new(big.Rat).SetFrac(lo, big.NewInt(cutoff1))
}
}
}
示例2: getBalance
func getBalance(balance string) (bool, *big.Rat) {
rationalNum := new(big.Rat)
if strings.Contains(balance, "(") {
rationalNum.SetFloat64(calc.Solve(balance))
return true, rationalNum
}
_, isValid := rationalNum.SetString(balance)
return isValid, rationalNum
}
示例3: OFXTransactionCallback
//export OFXTransactionCallback
func OFXTransactionCallback(transaction_data C.struct_OfxTransactionData, data unsafe.Pointer) C.int {
iobj := (*ImportObject)(data)
itl := iobj.TransactionList
transaction := new(Transaction)
if transaction_data.name_valid != 0 {
transaction.Description = C.GoString(&transaction_data.name[0])
}
// if transaction_data.reference_number_valid != 0 {
// fmt.Println("reference_number: ", C.GoString(&transaction_data.reference_number[0]))
// }
if transaction_data.date_posted_valid != 0 {
transaction.Date = time.Unix(int64(transaction_data.date_posted), 0)
} else if transaction_data.date_initiated_valid != 0 {
transaction.Date = time.Unix(int64(transaction_data.date_initiated), 0)
}
if transaction_data.fi_id_valid != 0 {
transaction.RemoteId = C.GoString(&transaction_data.fi_id[0])
}
if transaction_data.amount_valid != 0 {
split := new(Split)
r := new(big.Rat)
r.SetFloat64(float64(transaction_data.amount))
security, err := GetSecurity(itl.Account.SecurityId, itl.Account.UserId)
if err != nil {
if iobj.Error == nil {
iobj.Error = err
}
return 1
}
split.Amount = r.FloatString(security.Precision)
if transaction_data.memo_valid != 0 {
split.Memo = C.GoString(&transaction_data.memo[0])
}
if transaction_data.check_number_valid != 0 {
split.Number = C.GoString(&transaction_data.check_number[0])
}
split.SecurityId = -1
split.AccountId = itl.Account.AccountId
transaction.Splits = append(transaction.Splits, split)
} else {
if iobj.Error == nil {
iobj.Error = errors.New("OFX transaction amount invalid")
}
return 1
}
var security *Security
var err error
split := new(Split)
units := new(big.Rat)
if transaction_data.units_valid != 0 {
units.SetFloat64(float64(transaction_data.units))
if transaction_data.security_data_valid != 0 {
security_data := transaction_data.security_data_ptr
if security_data.ticker_valid != 0 {
s, err := GetSecurityByName(C.GoString(&security_data.ticker[0]))
if err != nil {
if iobj.Error == nil {
iobj.Error = errors.New("Failed to find OFX transaction security: " + C.GoString(&security_data.ticker[0]))
}
return 1
}
security = s
} else {
if iobj.Error == nil {
iobj.Error = errors.New("OFX security ticker invalid")
}
return 1
}
if security.Type == Stock && security_data.unique_id_valid != 0 && security_data.unique_id_type_valid != 0 && C.GoString(&security_data.unique_id_type[0]) == "CUSIP" {
// Validate the security CUSIP, if possible
if security.AlternateId != C.GoString(&security_data.unique_id[0]) {
if iobj.Error == nil {
iobj.Error = errors.New("OFX transaction security CUSIP failed to validate")
}
return 1
}
}
} else {
security, err = GetSecurity(itl.Account.SecurityId, itl.Account.UserId)
if err != nil {
if iobj.Error == nil {
iobj.Error = err
}
return 1
}
}
} else {
// Calculate units from other available fields if its not present
// units = - (amount + various fees) / unitprice
units.SetFloat64(float64(transaction_data.amount))
fees := new(big.Rat)
if transaction_data.fees_valid != 0 {
fees.SetFloat64(float64(-transaction_data.fees))
}
if transaction_data.commission_valid != 0 {
//.........这里部分代码省略.........
示例4: Process
//.........这里部分代码省略.........
}
case oMYADDRESS:
vm.stack.Push(ethutil.BigD(addr))
case oTXSENDER:
vm.stack.Push(ethutil.BigD(vars.sender))
case oTXVALUE:
vm.stack.Push(vars.txValue)
case oTXDATAN:
vm.stack.Push(big.NewInt(int64(len(vars.txData))))
case oTXDATA:
v := vm.stack.Pop()
// v >= len(data)
if v.Cmp(big.NewInt(int64(len(vars.txData)))) >= 0 {
vm.stack.Push(ethutil.Big("0"))
} else {
vm.stack.Push(ethutil.Big(vars.txData[v.Uint64()]))
}
case oBLK_PREVHASH:
vm.stack.Push(ethutil.BigD(vars.prevHash))
case oBLK_COINBASE:
vm.stack.Push(ethutil.BigD(vars.coinbase))
case oBLK_TIMESTAMP:
vm.stack.Push(big.NewInt(vars.time))
case oBLK_NUMBER:
vm.stack.Push(big.NewInt(int64(vars.blockNumber)))
case oBLK_DIFFICULTY:
vm.stack.Push(vars.diff)
case oBASEFEE:
// e = 10^21
e := big.NewInt(0).Exp(big.NewInt(10), big.NewInt(21), big.NewInt(0))
d := new(big.Rat)
d.SetInt(vars.diff)
c := new(big.Rat)
c.SetFloat64(0.5)
// d = diff / 0.5
d.Quo(d, c)
// base = floor(d)
base.Div(d.Num(), d.Denom())
x := new(big.Int)
x.Div(e, base)
// x = floor(10^21 / floor(diff^0.5))
vm.stack.Push(x)
case oSHA256, oSHA3, oRIPEMD160:
// This is probably save
// ceil(pop / 32)
length := int(math.Ceil(float64(vm.stack.Pop().Uint64()) / 32.0))
// New buffer which will contain the concatenated popped items
data := new(bytes.Buffer)
for i := 0; i < length; i++ {
// Encode the number to bytes and have it 32bytes long
num := ethutil.NumberToBytes(vm.stack.Pop().Bytes(), 256)
data.WriteString(string(num))
}
if op == oSHA256 {
vm.stack.Push(base.SetBytes(ethutil.Sha256Bin(data.Bytes())))
} else if op == oSHA3 {
vm.stack.Push(base.SetBytes(ethutil.Sha3Bin(data.Bytes())))
} else {
vm.stack.Push(base.SetBytes(ethutil.Ripemd160(data.Bytes())))
}
case oECMUL:
y := vm.stack.Pop()
x := vm.stack.Pop()
示例5: FloatToRat
func FloatToRat(v float64) *big.Rat {
rat := new(big.Rat)
rat.SetFloat64(v)
return rat
}
示例6: ProcContract
//.........这里部分代码省略.........
// ints. TODO :)
case oMYADDRESS:
bm.stack.Push(ethutil.BigD(tx.Hash()))
case oTXSENDER:
bm.stack.Push(ethutil.BigD(tx.Sender()))
case oTXVALUE:
bm.stack.Push(tx.Value)
case oTXDATAN:
bm.stack.Push(big.NewInt(int64(len(tx.Data))))
case oTXDATA:
v := bm.stack.Pop()
// v >= len(data)
if v.Cmp(big.NewInt(int64(len(tx.Data)))) >= 0 {
bm.stack.Push(ethutil.Big("0"))
} else {
bm.stack.Push(ethutil.Big(tx.Data[v.Uint64()]))
}
case oBLK_PREVHASH:
bm.stack.Push(ethutil.Big(block.PrevHash))
case oBLK_COINBASE:
bm.stack.Push(ethutil.Big(block.Coinbase))
case oBLK_TIMESTAMP:
bm.stack.Push(big.NewInt(block.Time))
case oBLK_NUMBER:
bm.stack.Push(blockInfo.Number)
case oBLK_DIFFICULTY:
bm.stack.Push(block.Difficulty)
case oBASEFEE:
// e = 10^21
e := big.NewInt(0).Exp(big.NewInt(10), big.NewInt(21), big.NewInt(0))
d := new(big.Rat)
d.SetInt(block.Difficulty)
c := new(big.Rat)
c.SetFloat64(0.5)
// d = diff / 0.5
d.Quo(d, c)
// base = floor(d)
base.Div(d.Num(), d.Denom())
x := new(big.Int)
x.Div(e, base)
// x = floor(10^21 / floor(diff^0.5))
bm.stack.Push(x)
case oSHA256, oSHA3, oRIPEMD160:
// This is probably save
// ceil(pop / 32)
length := int(math.Ceil(float64(bm.stack.Pop().Uint64()) / 32.0))
// New buffer which will contain the concatenated popped items
data := new(bytes.Buffer)
for i := 0; i < length; i++ {
// Encode the number to bytes and have it 32bytes long
num := ethutil.NumberToBytes(bm.stack.Pop().Bytes(), 256)
data.WriteString(string(num))
}
if op == oSHA256 {
bm.stack.Push(base.SetBytes(ethutil.Sha256Bin(data.Bytes())))
} else if op == oSHA3 {
bm.stack.Push(base.SetBytes(ethutil.Sha3Bin(data.Bytes())))
} else {
bm.stack.Push(base.SetBytes(ethutil.Ripemd160(data.Bytes())))
}
case oECMUL:
y := bm.stack.Pop()
x := bm.stack.Pop()