Black Lives Matter. Support the Equal Justice Initiative.

Source file src/math/example_test.go

Documentation: math

     1  // Copyright 2017 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 math_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  )
    11  
    12  func ExampleAcos() {
    13  	fmt.Printf("%.2f", math.Acos(1))
    14  	// Output: 0.00
    15  }
    16  
    17  func ExampleAcosh() {
    18  	fmt.Printf("%.2f", math.Acosh(1))
    19  	// Output: 0.00
    20  }
    21  
    22  func ExampleAsin() {
    23  	fmt.Printf("%.2f", math.Asin(0))
    24  	// Output: 0.00
    25  }
    26  
    27  func ExampleAsinh() {
    28  	fmt.Printf("%.2f", math.Asinh(0))
    29  	// Output: 0.00
    30  }
    31  
    32  func ExampleAtan() {
    33  	fmt.Printf("%.2f", math.Atan(0))
    34  	// Output: 0.00
    35  }
    36  
    37  func ExampleAtan2() {
    38  	fmt.Printf("%.2f", math.Atan2(0, 0))
    39  	// Output: 0.00
    40  }
    41  
    42  func ExampleAtanh() {
    43  	fmt.Printf("%.2f", math.Atanh(0))
    44  	// Output: 0.00
    45  }
    46  
    47  func ExampleCopysign() {
    48  	fmt.Printf("%.2f", math.Copysign(3.2, -1))
    49  	// Output: -3.20
    50  }
    51  
    52  func ExampleCos() {
    53  	fmt.Printf("%.2f", math.Cos(math.Pi/2))
    54  	// Output: 0.00
    55  }
    56  
    57  func ExampleCosh() {
    58  	fmt.Printf("%.2f", math.Cosh(0))
    59  	// Output: 1.00
    60  }
    61  
    62  func ExampleSin() {
    63  	fmt.Printf("%.2f", math.Sin(math.Pi))
    64  	// Output: 0.00
    65  }
    66  
    67  func ExampleSincos() {
    68  	sin, cos := math.Sincos(0)
    69  	fmt.Printf("%.2f, %.2f", sin, cos)
    70  	// Output: 0.00, 1.00
    71  }
    72  
    73  func ExampleSinh() {
    74  	fmt.Printf("%.2f", math.Sinh(0))
    75  	// Output: 0.00
    76  }
    77  
    78  func ExampleTan() {
    79  	fmt.Printf("%.2f", math.Tan(0))
    80  	// Output: 0.00
    81  }
    82  
    83  func ExampleTanh() {
    84  	fmt.Printf("%.2f", math.Tanh(0))
    85  	// Output: 0.00
    86  }
    87  
    88  func ExampleSqrt() {
    89  	const (
    90  		a = 3
    91  		b = 4
    92  	)
    93  	c := math.Sqrt(a*a + b*b)
    94  	fmt.Printf("%.1f", c)
    95  	// Output: 5.0
    96  }
    97  
    98  func ExampleCeil() {
    99  	c := math.Ceil(1.49)
   100  	fmt.Printf("%.1f", c)
   101  	// Output: 2.0
   102  }
   103  
   104  func ExampleFloor() {
   105  	c := math.Floor(1.51)
   106  	fmt.Printf("%.1f", c)
   107  	// Output: 1.0
   108  }
   109  
   110  func ExamplePow() {
   111  	c := math.Pow(2, 3)
   112  	fmt.Printf("%.1f", c)
   113  	// Output: 8.0
   114  }
   115  
   116  func ExamplePow10() {
   117  	c := math.Pow10(2)
   118  	fmt.Printf("%.1f", c)
   119  	// Output: 100.0
   120  }
   121  
   122  func ExampleRound() {
   123  	p := math.Round(10.5)
   124  	fmt.Printf("%.1f\n", p)
   125  
   126  	n := math.Round(-10.5)
   127  	fmt.Printf("%.1f\n", n)
   128  	// Output:
   129  	// 11.0
   130  	// -11.0
   131  }
   132  
   133  func ExampleRoundToEven() {
   134  	u := math.RoundToEven(11.5)
   135  	fmt.Printf("%.1f\n", u)
   136  
   137  	d := math.RoundToEven(12.5)
   138  	fmt.Printf("%.1f\n", d)
   139  	// Output:
   140  	// 12.0
   141  	// 12.0
   142  }
   143  
   144  func ExampleLog() {
   145  	x := math.Log(1)
   146  	fmt.Printf("%.1f\n", x)
   147  
   148  	y := math.Log(2.7183)
   149  	fmt.Printf("%.1f\n", y)
   150  	// Output:
   151  	// 0.0
   152  	// 1.0
   153  }
   154  
   155  func ExampleLog2() {
   156  	fmt.Printf("%.1f", math.Log2(256))
   157  	// Output: 8.0
   158  }
   159  
   160  func ExampleLog10() {
   161  	fmt.Printf("%.1f", math.Log10(100))
   162  	// Output: 2.0
   163  }
   164  
   165  func ExampleMod() {
   166  	c := math.Mod(7, 4)
   167  	fmt.Printf("%.1f", c)
   168  	// Output: 3.0
   169  }
   170  
   171  func ExampleAbs() {
   172  	x := math.Abs(-2)
   173  	fmt.Printf("%.1f\n", x)
   174  
   175  	y := math.Abs(2)
   176  	fmt.Printf("%.1f\n", y)
   177  	// Output:
   178  	// 2.0
   179  	// 2.0
   180  }
   181  func ExampleDim() {
   182  	fmt.Printf("%.2f\n", math.Dim(4, -2))
   183  	fmt.Printf("%.2f\n", math.Dim(-4, 2))
   184  	// Output:
   185  	// 6.00
   186  	// 0.00
   187  }
   188  
   189  func ExampleExp() {
   190  	fmt.Printf("%.2f\n", math.Exp(1))
   191  	fmt.Printf("%.2f\n", math.Exp(2))
   192  	fmt.Printf("%.2f\n", math.Exp(-1))
   193  	// Output:
   194  	// 2.72
   195  	// 7.39
   196  	// 0.37
   197  }
   198  
   199  func ExampleExp2() {
   200  	fmt.Printf("%.2f\n", math.Exp2(1))
   201  	fmt.Printf("%.2f\n", math.Exp2(-3))
   202  	// Output:
   203  	// 2.00
   204  	// 0.12
   205  }
   206  
   207  func ExampleExpm1() {
   208  	fmt.Printf("%.6f\n", math.Expm1(0.01))
   209  	fmt.Printf("%.6f\n", math.Expm1(-1))
   210  	// Output:
   211  	// 0.010050
   212  	// -0.632121
   213  }
   214  
   215  func ExampleTrunc() {
   216  	fmt.Printf("%.2f\n", math.Trunc(math.Pi))
   217  	fmt.Printf("%.2f\n", math.Trunc(-1.2345))
   218  	// Output:
   219  	// 3.00
   220  	// -1.00
   221  }
   222  
   223  func ExampleCbrt() {
   224  	fmt.Printf("%.2f\n", math.Cbrt(8))
   225  	fmt.Printf("%.2f\n", math.Cbrt(27))
   226  	// Output:
   227  	// 2.00
   228  	// 3.00
   229  }
   230  
   231  func ExampleModf() {
   232  	int, frac := math.Modf(3.14)
   233  	fmt.Printf("%.2f, %.2f\n", int, frac)
   234  
   235  	int, frac = math.Modf(-2.71)
   236  	fmt.Printf("%.2f, %.2f\n", int, frac)
   237  	// Output:
   238  	// 3.00, 0.14
   239  	// -2.00, -0.71
   240  }
   241  

View as plain text