本文整理汇总了Golang中math/big.Rat.Neg方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.Neg方法的具体用法?Golang Rat.Neg怎么用?Golang Rat.Neg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.Neg方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: quo
func quo(x, y *complexRat) *complexRat {
z := newComplexRat()
denominator := new(big.Rat)
t := new(big.Rat)
t.Mul(y.r, y.r)
denominator.Mul(y.i, y.i)
denominator.Add(denominator, t)
if denominator.Cmp(zero) == 0 {
return newComplexRat()
}
ac := new(big.Rat)
bd := new(big.Rat)
ac.Mul(x.r, y.r)
bd.Mul(x.i, y.i)
bc := new(big.Rat)
ad := new(big.Rat)
bc.Mul(x.i, y.r)
ad.Mul(x.r, y.i)
z.r.Add(ac, bd)
z.r.Quo(z.r, denominator)
z.i.Add(bc, ad.Neg(ad))
z.i.Quo(z.i, denominator)
return z
}
示例2: floatString
func (r BigRat) floatString(verb byte, prec int) string {
switch verb {
case 'f', 'F':
return r.Rat.FloatString(prec)
case 'e', 'E':
// The exponent will alway be >= 0.
sign := ""
var x, t big.Rat
x.Set(r.Rat)
if x.Sign() < 0 {
sign = "-"
x.Neg(&x)
}
t.Set(&x)
exp := ratExponent(&x)
ratScale(&t, exp)
str := t.FloatString(prec + 1) // +1 because first digit might be zero.
// Drop the decimal.
if str[0] == '0' {
str = str[2:]
exp--
} else if len(str) > 1 && str[1] == '.' {
str = str[0:1] + str[2:]
}
return eFormat(verb, prec, sign, str, exp)
case 'g', 'G':
var x big.Rat
x.Set(r.Rat)
exp := ratExponent(&x)
// Exponent could be positive or negative
if exp < -4 || prec <= exp {
// Use e format.
verb -= 2 // g becomes e.
return trimEZeros(verb, r.floatString(verb, prec-1))
}
// Use f format.
// If it's got zeros right of the decimal, they count as digits in the precision.
// If it's got digits left of the decimal, they count as digits in the precision.
// Both are handled by adjusting prec by exp.
str := r.floatString(verb-1, prec-exp-1) // -1 for the one digit left of the decimal.
// Trim trailing decimals.
point := strings.IndexByte(str, '.')
if point > 0 {
n := len(str)
for str[n-1] == '0' {
n--
}
str = str[:n]
if str[n-1] == '.' {
str = str[:n-1]
}
}
return str
default:
Errorf("can't handle verb %c for rational", verb)
}
return ""
}
示例3: unaryFloatOp
func unaryFloatOp(x *big.Rat, op token.Token) interface{} {
var z big.Rat
switch op {
case token.ADD:
return z.Set(x)
case token.SUB:
return z.Neg(x)
}
panic("unreachable")
}
示例4: sexToDec
func sexToDec(deg, min, sec *big.Rat, dir string) *big.Rat {
// sexagesimal (base 60) to decimal
// https://imm.dtf.wa.gov.au/helpfiles/Latitude_Longitude_conversion_hlp.htm
deg.Add(deg, min.Quo(min, big.NewRat(60, 1)))
deg.Add(deg, sec.Quo(sec, big.NewRat(3600, 1)))
// N and E are the positive directions (like on an x,y axis)
if dir == "S" || dir == "W" {
deg.Neg(deg)
}
return deg
}
示例5: balanceTransaction
// Takes a transaction and balances it. This is mainly to fill in the empty part
// with the remaining balance.
func balanceTransaction(input *Transaction) error {
balance := new(big.Rat)
var emptyAccPtr *Account
var emptyAccIndex int
for accIndex, accChange := range input.AccountChanges {
if accChange.Balance == nil {
if emptyAccPtr != nil {
return fmt.Errorf("More than one account change empty!")
}
emptyAccPtr = &accChange
emptyAccIndex = accIndex
} else {
balance = balance.Add(balance, accChange.Balance)
}
}
if balance.Sign() != 0 {
if emptyAccPtr == nil {
return fmt.Errorf("No empty account change to place extra balance!")
}
input.AccountChanges[emptyAccIndex].Balance = balance.Neg(balance)
}
return nil
}
示例6: ratExponent
// 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
}
示例7: OFXTransactionCallback
//.........这里部分代码省略.........
}
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 {
commission := new(big.Rat)
commission.SetFloat64(float64(-transaction_data.commission))
fees.Add(fees, commission)
}
units.Add(units, fees)
units.Neg(units)
if transaction_data.unitprice_valid != 0 && transaction_data.unitprice != 0 {
unitprice := new(big.Rat)
unitprice.SetFloat64(float64(transaction_data.unitprice))
units.Quo(units, unitprice)
}
// If 'units' wasn't present, assume we're using the account's security
security, err = GetSecurity(itl.Account.SecurityId, itl.Account.UserId)
if err != nil {
if iobj.Error == nil {
iobj.Error = err
}
return 1
}
}
split.Amount = units.FloatString(security.Precision)
split.SecurityId = security.SecurityId
split.AccountId = -1
transaction.Splits = append(transaction.Splits, split)
if transaction_data.fees_valid != 0 {
split := new(Split)
r := new(big.Rat)
r.SetFloat64(float64(-transaction_data.fees))
security, err := GetSecurity(itl.Account.SecurityId, itl.Account.UserId)
if err != nil {
if iobj.Error == nil {
iobj.Error = err
}
return 1
}
示例8: Negate
// Negate returns a *BigFloat that is the negative of n.
func (n *BigFloat) Negate() Numeric {
rat := new(big.Rat)
return (*BigFloat)(rat.Neg((*big.Rat)(n)))
}