# Math

Examples in Go
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%.2f", math.Tanh(0))
}``````
``````Last Run  :
0.00``````
``````package main

import (
"fmt"

"gonum.org/v1/gonum/mat"
)

func main() {

// Initialize two matrices, a and b.
a := mat.NewDense(2, 2, []float64{
4, 0,
0, 4,
})
b := mat.NewDense(2, 3, []float64{
4, 0, 0,
0, 0, 4,
})

// Take the matrix product of a and b and place the result in c.
var c mat.Dense
c.Mul(a, b)

// Print the result using the formatter.
fc := mat.Formatted(&c, mat.Prefix("    "), mat.Squeeze())
fmt.Printf("c = %v", fc)
}
``````
``````Last Run  :
c = ⎡16  0   0⎤
⎣ 0  0  16⎦``````
Go’s math/rand package provides pseudorandom number generation.
``````package main

import (
"fmt"
"math/rand"
"time"
)

func main() {

// rand.Intn returns a random int n, 0 <= n < 100.
fmt.Print(rand.Intn(100), ",")
fmt.Print(rand.Intn(100))
fmt.Println()

// rand.Float64 returns a float64 f, 0.0 <= f < 1.0.
fmt.Println(rand.Float64())

fmt.Print((rand.Float64()*5)+5, ",")
fmt.Print((rand.Float64() * 5) + 5)
fmt.Println()

// The default number generator is deterministic, so it'll produce the same sequence of numbers each time by default.
// To produce varying sequences, give it a seed that changes.
// Note that this is not safe to use for random numbers you intend to be secret, use crypto/rand for those.
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)

fmt.Print(r1.Intn(100), ",")
fmt.Print(r1.Intn(100))
fmt.Println()

// If you seed a source with the same number, it produces the same sequence of random numbers.
s2 := rand.NewSource(42)
r2 := rand.New(s2)
fmt.Print(r2.Intn(100), ",")
fmt.Print(r2.Intn(100))
fmt.Println()
s3 := rand.NewSource(42)
r3 := rand.New(s3)
fmt.Print(r3.Intn(100), ",")
fmt.Print(r3.Intn(100))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
x := math.Abs(-2)
fmt.Printf("%.1f\n", x)

y := math.Abs(2)
fmt.Printf("%.1f\n", y)
}``````
Computes the least integer value greater than or equal to x.
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Ceil(1.49)
fmt.Printf("%.1f", c)
}``````
Compute the greatest integer value less than or equal to x.
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Floor(1.51)
fmt.Printf("%.1f", c)
}``````
Round returns the nearest integer, rounding half away from zero.
``````package main

import (
"fmt"
"math"
)

func main() {
p := math.Round(10.5)
fmt.Printf("%.1f\n", p)

n := math.Round(-10.5)
fmt.Printf("%.1f\n", n)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(math.Max(1, 2))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(math.Min(1, 2))
}``````
Computes the remainder after division of one number by another.
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Mod(7, 4)
fmt.Printf("%.1f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
const (
a = 3
b = 4
)
c := math.Sqrt(a*a + b*b)
fmt.Printf("%.1f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(math.Cbrt(27))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%.2f", math.Exp(10))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%.2f", math.Exp2(10))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Pow(2, 3)
fmt.Printf("%.1f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
x := math.Log(1)
fmt.Printf("%.1f\n", x)

y := math.Log(2.7183)
fmt.Printf("%.1f\n", y)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%.1f", math.Log2(256))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%.1f", math.Log10(100))
}``````
``````package main

import (
"fmt"
"math"
)

func LogAny(x, b float64) float64 {
return math.Log(x) / math.Log(b)
}

func main() {
fmt.Printf("%.1f", LogAny(27, 3))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Erf(1.0)
fmt.Printf("%.6f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Erfc(1.0)
fmt.Printf("%.6f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Erfcinv(1.5)
fmt.Printf("%.6f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Erfinv(0.5)
fmt.Printf("%.6f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
c := math.Gamma(10.0)
fmt.Printf("%.1f", c)
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
not_a_number := math.NaN()
fmt.Println(not_a_number)
}``````
Returns whether float x is an IEEE 754 “not-a-number” value.
``````package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(math.IsNaN(math.Log(-1.0)))
}``````
``````package main

import (
"fmt"
"math"
)

func main() {
posInf := math.Inf(1)
fmt.Println(math.IsInf(posInf, 1))
}``````
``````Last Run  :
true
``````
``````package main

import (
"fmt"
"math"
)

func main() {
posInf := math.Inf(-1)
fmt.Println(math.IsInf(posInf, -1))
}``````
``````Last Run  :
true
``````