本文整理汇总了Golang中math/big.Rat.SetFrac64方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.SetFrac64方法的具体用法?Golang Rat.SetFrac64怎么用?Golang Rat.SetFrac64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.SetFrac64方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PutFrac64
func (me *StatisticalAccumulator) PutFrac64(a, b int64) {
xx := new(big.Rat)
xx.SetFrac64(a, b)
me.PutRat(xx)
}
示例2: ParseTimeDuration
// 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
}
示例3: ParseTimeDuration
// 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
}
示例4: main
func main() {
var recip big.Rat
max := int64(1 << 19)
for candidate := int64(2); candidate < max; candidate++ {
sum := big.NewRat(1, candidate)
max2 := int64(math.Sqrt(float64(candidate)))
for factor := int64(2); factor <= max2; factor++ {
if candidate%factor == 0 {
sum.Add(sum, recip.SetFrac64(1, factor))
if f2 := candidate / factor; f2 != factor {
sum.Add(sum, recip.SetFrac64(1, f2))
}
}
}
if sum.Denom().Int64() == 1 {
perfectstring := ""
if sum.Num().Int64() == 1 {
perfectstring = "perfect!"
}
fmt.Printf("Sum of recipr. factors of %d = %d exactly %s\n",
candidate, sum.Num().Int64(), perfectstring)
}
}
}