本文整理匯總了Golang中math/big.Rat.String方法的典型用法代碼示例。如果您正苦於以下問題:Golang Rat.String方法的具體用法?Golang Rat.String怎麽用?Golang Rat.String使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類math/big.Rat
的用法示例。
在下文中一共展示了Rat.String方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Intersect
// 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
}
}
示例2: TestDecimal_Rat
func TestDecimal_Rat(t *testing.T) {
tests := [...]struct {
v, s int64
a string
}{
{5, 0, "5/1"},
{5, -5, "500000/1"},
{1234, 3, "617/500"},
}
for i, v := range tests {
x := New(v.v, v.s)
if xs := x.Rat(nil).String(); xs != v.a {
t.Errorf("#%d: wanted %s, got %s",
i, v.a, xs)
}
}
// Test that the non-nil big.Rat is used.
x := New(5, 0)
r := new(big.Rat)
if xs := x.Rat(r).String(); xs != r.String() {
t.Errorf("wanted %s, got %s and %s", "5/1", xs, r.String())
}
}
示例3: num
func num(x *big.Rat) string {
if x.IsInt() {
return x.Num().String()
}
return x.String()
}
示例4: align
func align(b *big.Rat, w int) string {
s := b.String()
return strings.Repeat(" ", w-strings.Index(s, "/")) + s
}