X7ROOT File Manager
Current Path:
/opt/golang/1.22.0/src/math/big
opt
/
golang
/
1.22.0
/
src
/
math
/
big
/
📁
..
📄
accuracy_string.go
(647 B)
📄
alias_test.go
(8.81 KB)
📄
arith.go
(8.28 KB)
📄
arith_386.s
(4.04 KB)
📄
arith_amd64.go
(278 B)
📄
arith_amd64.s
(9.07 KB)
📄
arith_arm.s
(4 KB)
📄
arith_arm64.s
(11.85 KB)
📄
arith_decl.go
(686 B)
📄
arith_decl_pure.go
(1.01 KB)
📄
arith_decl_s390x.go
(503 B)
📄
arith_loong64.s
(749 B)
📄
arith_mips64x.s
(763 B)
📄
arith_mipsx.s
(759 B)
📄
arith_ppc64x.s
(16.4 KB)
📄
arith_riscv64.s
(750 B)
📄
arith_s390x.s
(20.29 KB)
📄
arith_s390x_test.go
(770 B)
📄
arith_test.go
(19.88 KB)
📄
arith_wasm.s
(613 B)
📄
bits_test.go
(5.07 KB)
📄
calibrate_test.go
(4.63 KB)
📄
decimal.go
(6.63 KB)
📄
decimal_test.go
(3.33 KB)
📄
doc.go
(3.8 KB)
📄
example_rat_test.go
(1.68 KB)
📄
example_test.go
(4.05 KB)
📄
float.go
(44.55 KB)
📄
float_test.go
(51.94 KB)
📄
floatconv.go
(8.35 KB)
📄
floatconv_test.go
(24.27 KB)
📄
floatexample_test.go
(3.63 KB)
📄
floatmarsh.go
(3.65 KB)
📄
floatmarsh_test.go
(4.51 KB)
📄
ftoa.go
(13.5 KB)
📄
gcd_test.go
(2.16 KB)
📄
hilbert_test.go
(2.88 KB)
📄
int.go
(33.21 KB)
📄
int_test.go
(58.47 KB)
📄
intconv.go
(6.7 KB)
📄
intconv_test.go
(10.01 KB)
📄
intmarsh.go
(2.19 KB)
📄
intmarsh_test.go
(3.07 KB)
📄
link_test.go
(1.4 KB)
📄
nat.go
(31.81 KB)
📄
nat_test.go
(26.22 KB)
📄
natconv.go
(14.56 KB)
📄
natconv_test.go
(16.85 KB)
📄
natdiv.go
(34.36 KB)
📄
prime.go
(10.39 KB)
📄
prime_test.go
(7.1 KB)
📄
rat.go
(13.48 KB)
📄
rat_test.go
(18.89 KB)
📄
ratconv.go
(12.35 KB)
📄
ratconv_test.go
(19.31 KB)
📄
ratmarsh.go
(2.23 KB)
📄
ratmarsh_test.go
(3.34 KB)
📄
roundingmode_string.go
(819 B)
📄
sqrt.go
(2.79 KB)
📄
sqrt_test.go
(4.81 KB)
Editing: example_test.go
// Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package big_test import ( "fmt" "log" "math" "math/big" ) func ExampleRat_SetString() { r := new(big.Rat) r.SetString("355/113") fmt.Println(r.FloatString(3)) // Output: 3.142 } func ExampleInt_SetString() { i := new(big.Int) i.SetString("644", 8) // octal fmt.Println(i) // Output: 420 } func ExampleFloat_SetString() { f := new(big.Float) f.SetString("3.14159") fmt.Println(f) // Output: 3.14159 } func ExampleRat_Scan() { // The Scan function is rarely used directly; // the fmt package recognizes it as an implementation of fmt.Scanner. r := new(big.Rat) _, err := fmt.Sscan("1.5000", r) if err != nil { log.Println("error scanning value:", err) } else { fmt.Println(r) } // Output: 3/2 } func ExampleInt_Scan() { // The Scan function is rarely used directly; // the fmt package recognizes it as an implementation of fmt.Scanner. i := new(big.Int) _, err := fmt.Sscan("18446744073709551617", i) if err != nil { log.Println("error scanning value:", err) } else { fmt.Println(i) } // Output: 18446744073709551617 } func ExampleFloat_Scan() { // The Scan function is rarely used directly; // the fmt package recognizes it as an implementation of fmt.Scanner. f := new(big.Float) _, err := fmt.Sscan("1.19282e99", f) if err != nil { log.Println("error scanning value:", err) } else { fmt.Println(f) } // Output: 1.19282e+99 } // This example demonstrates how to use big.Int to compute the smallest // Fibonacci number with 100 decimal digits and to test whether it is prime. func Example_fibonacci() { // Initialize two big ints with the first two numbers in the sequence. a := big.NewInt(0) b := big.NewInt(1) // Initialize limit as 10^99, the smallest integer with 100 digits. var limit big.Int limit.Exp(big.NewInt(10), big.NewInt(99), nil) // Loop while a is smaller than 1e100. for a.Cmp(&limit) < 0 { // Compute the next Fibonacci number, storing it in a. a.Add(a, b) // Swap a and b so that b is the next number in the sequence. a, b = b, a } fmt.Println(a) // 100-digit Fibonacci number // Test a for primality. // (ProbablyPrimes' argument sets the number of Miller-Rabin // rounds to be performed. 20 is a good value.) fmt.Println(a.ProbablyPrime(20)) // Output: // 1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757 // false } // This example shows how to use big.Float to compute the square root of 2 with // a precision of 200 bits, and how to print the result as a decimal number. func Example_sqrt2() { // We'll do computations with 200 bits of precision in the mantissa. const prec = 200 // Compute the square root of 2 using Newton's Method. We start with // an initial estimate for sqrt(2), and then iterate: // x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) ) // Since Newton's Method doubles the number of correct digits at each // iteration, we need at least log_2(prec) steps. steps := int(math.Log2(prec)) // Initialize values we need for the computation. two := new(big.Float).SetPrec(prec).SetInt64(2) half := new(big.Float).SetPrec(prec).SetFloat64(0.5) // Use 1 as the initial estimate. x := new(big.Float).SetPrec(prec).SetInt64(1) // We use t as a temporary variable. There's no need to set its precision // since big.Float values with unset (== 0) precision automatically assume // the largest precision of the arguments when used as the result (receiver) // of a big.Float operation. t := new(big.Float) // Iterate. for i := 0; i <= steps; i++ { t.Quo(two, x) // t = 2.0 / x_n t.Add(x, t) // t = x_n + (2.0 / x_n) x.Mul(half, t) // x_{n+1} = 0.5 * t } // We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter fmt.Printf("sqrt(2) = %.50f\n", x) // Print the error between 2 and x*x. t.Mul(x, x) // t = x*x fmt.Printf("error = %e\n", t.Sub(two, t)) // Output: // sqrt(2) = 1.41421356237309504880168872420969807856967187537695 // error = 0.000000e+00 }
Upload File
Create Folder