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