github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/math/big/example_rat_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package big_test
     6  
     7  import (
     8  	"github.com/shogo82148/std/fmt"
     9  )
    10  
    11  // この例では、big.Ratを使用して、自然対数の基数である定数eの
    12  // 有理数収束のシーケンスの最初の15項を計算する方法を示します。
    13  func Example_eConvergents() {
    14  	for i := 1; i <= 15; i++ {
    15  		r := recur(0, int64(i))
    16  
    17  		// rを分数と浮動小数点数の両方として印刷します。
    18  		// big.Ratはfmt.Formatterを実装しているので、%-13sを使用して
    19  		// 分数の左揃えの文字列表現を取得することができます。
    20  		fmt.Printf("%-13s = %s\n", r, r.FloatString(8))
    21  	}
    22  
    23  	// Output:
    24  	// 2/1           = 2.00000000
    25  	// 3/1           = 3.00000000
    26  	// 8/3           = 2.66666667
    27  	// 11/4          = 2.75000000
    28  	// 19/7          = 2.71428571
    29  	// 87/32         = 2.71875000
    30  	// 106/39        = 2.71794872
    31  	// 193/71        = 2.71830986
    32  	// 1264/465      = 2.71827957
    33  	// 1457/536      = 2.71828358
    34  	// 2721/1001     = 2.71828172
    35  	// 23225/8544    = 2.71828184
    36  	// 25946/9545    = 2.71828182
    37  	// 49171/18089   = 2.71828183
    38  	// 517656/190435 = 2.71828183
    39  }