math

package
v0.0.0-...-7a62a49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2020 License: BSD-3-Clause, BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package math provides basic constants and mathematical functions.

This package does not guarantee bit-identical results across architectures.

Index

Examples

Constants

View Source
const (
	E   = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
	Pi  = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
	Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622

	Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
	SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
	SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
	SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339

	Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
	Log2E  = 1 / Ln2
	Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
	Log10E = 1 / Ln10
)

Mathematical constants.

View Source
const (
	MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
	SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

	MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
	SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

View Source
const (
	MaxInt8   = 1<<7 - 1
	MinInt8   = -1 << 7
	MaxInt16  = 1<<15 - 1
	MinInt16  = -1 << 15
	MaxInt32  = 1<<31 - 1
	MinInt32  = -1 << 31
	MaxInt64  = 1<<63 - 1
	MinInt64  = -1 << 63
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)

Integer limit values.

Variables

This section is empty.

Functions

func Abs

func Abs(x float64) float64
Example
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)
}
Output:

2.0
2.0

func Acos

func Acos(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Acos(1))
}
Output:

0.00

func Acosh

func Acosh(x float64) float64

Acosh returns the inverse hyperbolic cosine of x.

Special cases are:

Acosh(+Inf) = +Inf
Acosh(x) = NaN if x < 1
Acosh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Acosh(1))
}
Output:

0.00

func Asin

func Asin(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Asin(0))
}
Output:

0.00

func Asinh

func Asinh(x float64) float64

Asinh returns the inverse hyperbolic sine of x.

Special cases are:

Asinh(±0) = ±0
Asinh(±Inf) = ±Inf
Asinh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Asinh(0))
}
Output:

0.00

func Atan

func Atan(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atan(0))
}
Output:

0.00

func Atan2

func Atan2(y, x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atan2(0, 0))
}
Output:

0.00

func Atanh

func Atanh(x float64) float64

Atanh returns the inverse hyperbolic tangent of x.

Special cases are:

Atanh(1) = +Inf
Atanh(±0) = ±0
Atanh(-1) = -Inf
Atanh(x) = NaN if x < -1 or x > 1
Atanh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atanh(0))
}
Output:

0.00

func Cbrt

func Cbrt(x float64) float64

Cbrt returns the cube root of x.

Special cases are:

Cbrt(±0) = ±0
Cbrt(±Inf) = ±Inf
Cbrt(NaN) = NaN

func Ceil

func Ceil(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	c := math.Ceil(1.49)
	fmt.Printf("%.1f", c)
}
Output:

2.0

func Copysign

func Copysign(x, y float64) float64

Copysign returns a value with the magnitude of x and the sign of y.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Copysign(3.2, -1))
}
Output:

-3.20

func Cos

func Cos(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Cos(math.Pi/2))
}
Output:

0.00

func Cosh

func Cosh(x float64) float64

Cosh returns the hyperbolic cosine of x.

Special cases are:

Cosh(±0) = 1
Cosh(±Inf) = +Inf
Cosh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Cosh(0))
}
Output:

1.00

func Dim

func Dim(x, y float64) float64

Dim returns the maximum of x-y or 0.

Special cases are:

Dim(+Inf, +Inf) = NaN
Dim(-Inf, -Inf) = NaN
Dim(x, NaN) = Dim(NaN, x) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Dim(4, -2))
	fmt.Printf("%.2f\n", math.Dim(-4, 2))
}
Output:

6.00
0.00

func Erf

func Erf(x float64) float64

Erf returns the error function of x.

Special cases are:

Erf(+Inf) = 1
Erf(-Inf) = -1
Erf(NaN) = NaN

func Erfc

func Erfc(x float64) float64

Erfc returns the complementary error function of x.

Special cases are:

Erfc(+Inf) = 0
Erfc(-Inf) = 2
Erfc(NaN) = NaN

func Erfcinv

func Erfcinv(x float64) float64

Erfcinv returns the inverse of Erfc(x).

Special cases are:

Erfcinv(0) = +Inf
Erfcinv(2) = -Inf
Erfcinv(x) = NaN if x < 0 or x > 2
Erfcinv(NaN) = NaN

func Erfinv

func Erfinv(x float64) float64

Erfinv returns the inverse error function of x.

Special cases are:

Erfinv(1) = +Inf
Erfinv(-1) = -Inf
Erfinv(x) = NaN if x < -1 or x > 1
Erfinv(NaN) = NaN

func Exp

func Exp(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp(1))
	fmt.Printf("%.2f\n", math.Exp(2))
	fmt.Printf("%.2f\n", math.Exp(-1))
}
Output:

2.72
7.39
0.37

func Exp2

func Exp2(x float64) float64

Exp2 returns 2**x, the base-2 exponential of x.

Special cases are the same as Exp.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp2(1))
	fmt.Printf("%.2f\n", math.Exp2(-3))
}
Output:

2.00
0.12

func Expm1

func Expm1(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.6f\n", math.Expm1(0.01))
	fmt.Printf("%.6f\n", math.Expm1(-1))
}
Output:

0.010050
-0.632121

func FMA

func FMA(x, y, z float64) float64

FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)

func Float32bits

func Float32bits(f float32) uint32

Float32bits returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position. Float32bits(Float32frombits(x)) == x.

func Float32frombits

func Float32frombits(b uint32) float32

Float32frombits returns the floating-point number corresponding to the IEEE 754 binary representation b, with the sign bit of b and the result in the same bit position. Float32frombits(Float32bits(x)) == x.

func Float64bits

func Float64bits(f float64) uint64

Float64bits returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position, and Float64bits(Float64frombits(x)) == x.

func Float64frombits

func Float64frombits(b uint64) float64

Float64frombits returns the floating-point number corresponding to the IEEE 754 binary representation b, with the sign bit of b and the result in the same bit position. Float64frombits(Float64bits(x)) == x.

func Floor

func Floor(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	c := math.Floor(1.51)
	fmt.Printf("%.1f", c)
}
Output:

1.0

func Frexp

func Frexp(f float64) (frac float64, exp int)

Frexp breaks f into a normalized fraction and an integral power of two. It returns frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).

Special cases are:

Frexp(±0) = ±0, 0
Frexp(±Inf) = ±Inf, 0
Frexp(NaN) = NaN, 0

func Gamma

func Gamma(x float64) float64

Gamma returns the Gamma function of x.

Special cases are:

Gamma(+Inf) = +Inf
Gamma(+0) = +Inf
Gamma(-0) = -Inf
Gamma(x) = NaN for integer x < 0
Gamma(-Inf) = NaN
Gamma(NaN) = NaN

func Hypot

func Hypot(p, q float64) float64

Hypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.

Special cases are:

Hypot(±Inf, q) = +Inf
Hypot(p, ±Inf) = +Inf
Hypot(NaN, q) = NaN
Hypot(p, NaN) = NaN

func Ilogb

func Ilogb(x float64) int

Ilogb returns the binary exponent of x as an integer.

Special cases are:

Ilogb(±Inf) = MaxInt32
Ilogb(0) = MinInt32
Ilogb(NaN) = MaxInt32

func Inf

func Inf(sign int) float64

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func IsInf

func IsInf(f float64, sign int) bool

IsInf reports whether f is an infinity, according to sign. If sign > 0, IsInf reports whether f is positive infinity. If sign < 0, IsInf reports whether f is negative infinity. If sign == 0, IsInf reports whether f is either infinity.

func IsNaN

func IsNaN(f float64) (is bool)

IsNaN reports whether f is an IEEE 754 “not-a-number” value.

func J0

func J0(x float64) float64

J0 returns the order-zero Bessel function of the first kind.

Special cases are:

J0(±Inf) = 0
J0(0) = 1
J0(NaN) = NaN

func J1

func J1(x float64) float64

J1 returns the order-one Bessel function of the first kind.

Special cases are:

J1(±Inf) = 0
J1(NaN) = NaN

func Jn

func Jn(n int, x float64) float64

Jn returns the order-n Bessel function of the first kind.

Special cases are:

Jn(n, ±Inf) = 0
Jn(n, NaN) = NaN

func Ldexp

func Ldexp(frac float64, exp int) float64

func Lgamma

func Lgamma(x float64) (lgamma float64, sign int)

Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).

Special cases are:

Lgamma(+Inf) = +Inf
Lgamma(0) = +Inf
Lgamma(-integer) = +Inf
Lgamma(-Inf) = -Inf
Lgamma(NaN) = NaN

func Log

func Log(x float64) float64
Example
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)
}
Output:

0.0
1.0

func Log10

func Log10(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

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

2.0

func Log1p

func Log1p(x float64) float64

func Log2

func Log2(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

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

8.0

func Logb

func Logb(x float64) float64

Logb returns the binary exponent of x.

Special cases are:

Logb(±Inf) = +Inf
Logb(0) = -Inf
Logb(NaN) = NaN

func Max

func Max(x, y float64) float64

Max returns the larger of x or y.

Special cases are:

Max(x, +Inf) = Max(+Inf, x) = +Inf
Max(x, NaN) = Max(NaN, x) = NaN
Max(+0, ±0) = Max(±0, +0) = +0
Max(-0, -0) = -0

func Min

func Min(x, y float64) float64

Min returns the smaller of x or y.

Special cases are:

Min(x, -Inf) = Min(-Inf, x) = -Inf
Min(x, NaN) = Min(NaN, x) = NaN
Min(-0, ±0) = Min(±0, -0) = -0

func Mod

func Mod(x, y float64) float64
Example
package main

import (
	"fmt"
	"math"
)

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

3.0

func Modf

func Modf(f float64) (int float64, frac float64)

Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.

Special cases are:

Modf(±Inf) = ±Inf, NaN
Modf(NaN) = NaN, NaN

func NaN

func NaN() float64

NaN returns an IEEE 754 “not-a-number” value.

func Nextafter

func Nextafter(x, y float64) (r float64)

Nextafter returns the next representable float64 value after x towards y.

Special cases are:

Nextafter(x, x)   = x
Nextafter(NaN, y) = NaN
Nextafter(x, NaN) = NaN

func Nextafter32

func Nextafter32(x, y float32) (r float32)

Nextafter32 returns the next representable float32 value after x towards y.

Special cases are:

Nextafter32(x, x)   = x
Nextafter32(NaN, y) = NaN
Nextafter32(x, NaN) = NaN

func Pow

func Pow(x, y float64) float64

Pow returns x**y, the base-x exponential of y.

Special cases are (in order):

Pow(x, ±0) = 1 for any x
Pow(1, y) = 1 for any y
Pow(x, 1) = x for any x
Pow(NaN, y) = NaN
Pow(x, NaN) = NaN
Pow(±0, y) = ±Inf for y an odd integer < 0
Pow(±0, -Inf) = +Inf
Pow(±0, +Inf) = +0
Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer
Pow(-1, ±Inf) = 1
Pow(x, +Inf) = +Inf for |x| > 1
Pow(x, -Inf) = +0 for |x| > 1
Pow(x, +Inf) = +0 for |x| < 1
Pow(x, -Inf) = +Inf for |x| < 1
Pow(+Inf, y) = +Inf for y > 0
Pow(+Inf, y) = +0 for y < 0
Pow(-Inf, y) = Pow(-0, -y)
Pow(x, y) = NaN for finite x < 0 and finite non-integer y
Example
package main

import (
	"fmt"
	"math"
)

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

8.0

func Pow10

func Pow10(n int) float64

Pow10 returns 10**n, the base-10 exponential of n.

Special cases are:

Pow10(n) =    0 for n < -323
Pow10(n) = +Inf for n > 308
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	c := math.Pow10(2)
	fmt.Printf("%.1f", c)
}
Output:

100.0

func Remainder

func Remainder(x, y float64) float64

Remainder returns the IEEE 754 floating-point remainder of x/y.

Special cases are:

Remainder(±Inf, y) = NaN
Remainder(NaN, y) = NaN
Remainder(x, 0) = NaN
Remainder(x, ±Inf) = x
Remainder(x, NaN) = NaN

func Round

func Round(x float64) float64

Round returns the nearest integer, rounding half away from zero.

Special cases are:

Round(±0) = ±0
Round(±Inf) = ±Inf
Round(NaN) = NaN
Example
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)
}
Output:

11.0
-11.0

func RoundToEven

func RoundToEven(x float64) float64

RoundToEven returns the nearest integer, rounding ties to even.

Special cases are:

RoundToEven(±0) = ±0
RoundToEven(±Inf) = ±Inf
RoundToEven(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	u := math.RoundToEven(11.5)
	fmt.Printf("%.1f\n", u)

	d := math.RoundToEven(12.5)
	fmt.Printf("%.1f\n", d)
}
Output:

12.0
12.0

func Signbit

func Signbit(x float64) bool

Signbit reports whether x is negative or negative zero.

func Sin

func Sin(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Sin(math.Pi))
}
Output:

0.00

func Sincos

func Sincos(x float64) (sin, cos float64)

Sincos returns Sin(x), Cos(x).

Special cases are:

Sincos(±0) = ±0, 1
Sincos(±Inf) = NaN, NaN
Sincos(NaN) = NaN, NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	sin, cos := math.Sincos(0)
	fmt.Printf("%.2f, %.2f", sin, cos)
}
Output:

0.00, 1.00

func Sinh

func Sinh(x float64) float64

Sinh returns the hyperbolic sine of x.

Special cases are:

Sinh(±0) = ±0
Sinh(±Inf) = ±Inf
Sinh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Sinh(0))
}
Output:

0.00

func Sqrt

func Sqrt(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

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

5.0

func Tan

func Tan(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Tan(0))
}
Output:

0.00

func Tanh

func Tanh(x float64) float64

Tanh returns the hyperbolic tangent of x.

Special cases are:

Tanh(±0) = ±0
Tanh(±Inf) = ±1
Tanh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Tanh(0))
}
Output:

0.00

func Trunc

func Trunc(x float64) float64
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Trunc(math.Pi))
	fmt.Printf("%.2f\n", math.Trunc(-1.2345))
}
Output:

3.00
-1.00

func Y0

func Y0(x float64) float64

Y0 returns the order-zero Bessel function of the second kind.

Special cases are:

Y0(+Inf) = 0
Y0(0) = -Inf
Y0(x < 0) = NaN
Y0(NaN) = NaN

func Y1

func Y1(x float64) float64

Y1 returns the order-one Bessel function of the second kind.

Special cases are:

Y1(+Inf) = 0
Y1(0) = -Inf
Y1(x < 0) = NaN
Y1(NaN) = NaN

func Yn

func Yn(n int, x float64) float64

Yn returns the order-n Bessel function of the second kind.

Special cases are:

Yn(n, +Inf) = 0
Yn(n ≥ 0, 0) = -Inf
Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
Yn(n, x < 0) = NaN
Yn(n, NaN) = NaN

Types

This section is empty.

Directories

Path Synopsis
Package big implements arbitrary-precision arithmetic (big numbers).
Package big implements arbitrary-precision arithmetic (big numbers).
Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types.
Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types.
Package cmplx provides basic constants and mathematical functions for complex numbers.
Package cmplx provides basic constants and mathematical functions for complex numbers.
Package rand implements pseudo-random number generators.
Package rand implements pseudo-random number generators.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL