...

Source file src/math/big/example_test.go

Documentation: math/big

     1  // Copyright 2012 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  	"fmt"
     9  	"log"
    10  	"math"
    11  	"math/big"
    12  )
    13  
    14  func ExampleRat_SetString() {
    15  	r := new(big.Rat)
    16  	r.SetString("355/113")
    17  	fmt.Println(r.FloatString(3))
    18  	// Output: 3.142
    19  }
    20  
    21  func ExampleInt_SetString() {
    22  	i := new(big.Int)
    23  	i.SetString("644", 8) // octal
    24  	fmt.Println(i)
    25  	// Output: 420
    26  }
    27  
    28  func ExampleFloat_SetString() {
    29  	f := new(big.Float)
    30  	f.SetString("3.14159")
    31  	fmt.Println(f)
    32  	// Output: 3.14159
    33  }
    34  
    35  func ExampleRat_Scan() {
    36  	// The Scan function is rarely used directly;
    37  	// the fmt package recognizes it as an implementation of fmt.Scanner.
    38  	r := new(big.Rat)
    39  	_, err := fmt.Sscan("1.5000", r)
    40  	if err != nil {
    41  		log.Println("error scanning value:", err)
    42  	} else {
    43  		fmt.Println(r)
    44  	}
    45  	// Output: 3/2
    46  }
    47  
    48  func ExampleInt_Scan() {
    49  	// The Scan function is rarely used directly;
    50  	// the fmt package recognizes it as an implementation of fmt.Scanner.
    51  	i := new(big.Int)
    52  	_, err := fmt.Sscan("18446744073709551617", i)
    53  	if err != nil {
    54  		log.Println("error scanning value:", err)
    55  	} else {
    56  		fmt.Println(i)
    57  	}
    58  	// Output: 18446744073709551617
    59  }
    60  
    61  func ExampleFloat_Scan() {
    62  	// The Scan function is rarely used directly;
    63  	// the fmt package recognizes it as an implementation of fmt.Scanner.
    64  	f := new(big.Float)
    65  	_, err := fmt.Sscan("1.19282e99", f)
    66  	if err != nil {
    67  		log.Println("error scanning value:", err)
    68  	} else {
    69  		fmt.Println(f)
    70  	}
    71  	// Output: 1.19282e+99
    72  }
    73  
    74  // This example demonstrates how to use big.Int to compute the smallest
    75  // Fibonacci number with 100 decimal digits and to test whether it is prime.
    76  func Example_fibonacci() {
    77  	// Initialize two big ints with the first two numbers in the sequence.
    78  	a := big.NewInt(0)
    79  	b := big.NewInt(1)
    80  
    81  	// Initialize limit as 10^99, the smallest integer with 100 digits.
    82  	var limit big.Int
    83  	limit.Exp(big.NewInt(10), big.NewInt(99), nil)
    84  
    85  	// Loop while a is smaller than 1e100.
    86  	for a.Cmp(&limit) < 0 {
    87  		// Compute the next Fibonacci number, storing it in a.
    88  		a.Add(a, b)
    89  		// Swap a and b so that b is the next number in the sequence.
    90  		a, b = b, a
    91  	}
    92  	fmt.Println(a) // 100-digit Fibonacci number
    93  
    94  	// Test a for primality.
    95  	// (ProbablyPrimes' argument sets the number of Miller-Rabin
    96  	// rounds to be performed. 20 is a good value.)
    97  	fmt.Println(a.ProbablyPrime(20))
    98  
    99  	// Output:
   100  	// 1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757
   101  	// false
   102  }
   103  
   104  // This example shows how to use big.Float to compute the square root of 2 with
   105  // a precision of 200 bits, and how to print the result as a decimal number.
   106  func Example_sqrt2() {
   107  	// We'll do computations with 200 bits of precision in the mantissa.
   108  	const prec = 200
   109  
   110  	// Compute the square root of 2 using Newton's Method. We start with
   111  	// an initial estimate for sqrt(2), and then iterate:
   112  	//     x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) )
   113  
   114  	// Since Newton's Method doubles the number of correct digits at each
   115  	// iteration, we need at least log_2(prec) steps.
   116  	steps := int(math.Log2(prec))
   117  
   118  	// Initialize values we need for the computation.
   119  	two := new(big.Float).SetPrec(prec).SetInt64(2)
   120  	half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
   121  
   122  	// Use 1 as the initial estimate.
   123  	x := new(big.Float).SetPrec(prec).SetInt64(1)
   124  
   125  	// We use t as a temporary variable. There's no need to set its precision
   126  	// since big.Float values with unset (== 0) precision automatically assume
   127  	// the largest precision of the arguments when used as the result (receiver)
   128  	// of a big.Float operation.
   129  	t := new(big.Float)
   130  
   131  	// Iterate.
   132  	for i := 0; i <= steps; i++ {
   133  		t.Quo(two, x)  // t = 2.0 / x_n
   134  		t.Add(x, t)    // t = x_n + (2.0 / x_n)
   135  		x.Mul(half, t) // x_{n+1} = 0.5 * t
   136  	}
   137  
   138  	// We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter
   139  	fmt.Printf("sqrt(2) = %.50f\n", x)
   140  
   141  	// Print the error between 2 and x*x.
   142  	t.Mul(x, x) // t = x*x
   143  	fmt.Printf("error = %e\n", t.Sub(two, t))
   144  
   145  	// Output:
   146  	// sqrt(2) = 1.41421356237309504880168872420969807856967187537695
   147  	// error = 0.000000e+00
   148  }
   149  

View as plain text