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