×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math Package Constants and Functions

In Go language, the math package is used for various mathematical-related operations, math package has a variety of constants and mathematical functions to work with the mathematical operations. To use these math functions – we need to import the math package.

Note: The math package does not guarantee bit-identical results across architectures.

List of Golang math Package Constants & Functions

The math package has a set of constants and functions.

Constants

Constant Description
math.E It returns the value of mathematical constant e.
math.Pi It returns the value of the mathematical constant π.
math.Phi It returns the value of the mathematical constant φ (Golden Ratio).
math.Sqrt2 It returns the square root of 2, approximately equal to 1.414.
math.SqrtE It returns the square root of the mathematical constant e, approximately equal to 1.64872127.
math.SqrtPi It returns the square root of the mathematical constant π, approximately equal to 1.77245385.
math.SqrtPhi It returns the square root of the mathematical constant φ (Golden Ratio), approximately equal to 1.2720196495.
math.Ln2 It returns the natural logarithm of 2, approximately equal to 0.693.
math.Log2E It returns the base 2 logarithm of mathematical constant e.
math.Ln10 It returns the base-10 logarithm, approximately equal to 2.302585092994.
math.Log10E It returns the base-10 logarithm of mathematical constant e, approximately equal to 0.43429448.
math.MaxFloat32 It returns the largest finite value of float32 type.
math.MaxFloat64 It returns the largest finite value of float64 type.
math.SmallestNonzeroFloat32 It returns the smallest positive, non-zero value of float32 type.
math.SmallestNonzeroFloat64 It returns the smallest positive, non-zero value of float64 type.
math.MaxInt It returns the highest (maximum) value that can be represented by an int.
math.MinInt It returns the lowest (minimum) value that can be represented by an int.
math.MaxInt8 It returns the highest (maximum) value that can be represented by an int8.
math.MinInt8 It returns the lowest (minimum) value that can be represented by an int8.
math.MaxInt16 It returns the highest (maximum) value that can be represented by an int16.
math.MinInt16 It returns the lowest (minimum) value that can be represented by an int16.
math.MaxInt32 It returns the highest (maximum) value that can be represented by an int32.
math.MinInt32 It returns the lowest (minimum) value that can be represented by an int32.
math.MaxInt64 It returns the highest (maximum) value that can be represented by an int64.
math.MinInt64 It returns the lowest (minimum) value that can be represented by an int64.
math.MaxUint It returns the highest (maximum) value that can be represented by a uint (unsigned integer).
math.MaxUint8 It returns the highest (maximum) value that can be represented by a uint8 (unsigned int8).
math.MaxUint16 It returns the highest (maximum) value that can be represented by a uint16 (unsigned int16).
math.MaxUint32 It returns the highest (maximum) value that can be represented by a uint32 (unsigned int32).
math.MaxUint64 It returns the highest (maximum) value that can be represented by a uint64 (unsigned int64).

Functions

Function Description
math.Abs() It returns the absolute value of the given value.
math.Acos() It returns the arccosine (in radians) of the given value.
math.Acosh() It returns the inverse hyperbolic cosine of the given value.
math.Asin() It returns the arcsine value (in radians) of the given value.
math.Asinh() It returns the inverse hyperbolic sine of the given value.
math.Atan() It returns the arctangent value (in radians) of the given value.
math.Atan2() It returns the arctangent value (in radians) of y/x (where y is the first parameter and x is the second parameter) using the signs of the two to determine the quadrant of the return value.
math.Atanh() It returns the inverse hyperbolic tangent value of the given value.
math.Cbrt() It returns the cube root of the given value.
math.Ceil() It returns the least integer value greater than or equal to the given value.
math.Copysign() It returns a value with the magnitude of x and the sign of y. Where, x is the first parameter, and y is the second parameter.
math.Cos() It returns the cosine value of the given radian value.
math.Cosh() It returns the hyperbolic cosine of the given value.
math.Dim() It returns the maximum of x-y or 0. Where x is the first parameter and y is the second parameter.
math.Erf() It returns the error function of the given parameter.
math.Erfc() It returns the complementary error function of the given parameter.
math.Erfcinv() It returns the inverse of Erfc(x), where x is the given parameter.
math.Erfinv() It returns the inverse error function of x, where x is the given parameter.
math.Exp() It returns the e**x, the base-e exponential of x, where x is the given parameter.
math.Exp2() It returns the 2**x, the base-2 exponential of x, where x is the given parameter.
math.Expm1() It returns the e**x - 1, the base-e exponential of x minus 1. Where x is the given parameter. It is more accurate than Exp(x) - 1 when x is near zero.
math.FMA() It returns the fused multiply-add of x, y, and z (i.e., x * y + z) computed with only one rounding. Where, x, y, and x are the parameters.
math.Float32bits() It returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position. Where f is the given parameter.
math.Float32frombits() It 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. Where b is the given parameter.
math.Float64bits() It returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position. Where f is the given parameter.
math.Float64frombits() It 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. Where b is the given parameter.
math.Floor() It returns the greatest integer value less than or equal to the given number.
math.Frexp() It returns the frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).
math.Gamma() It returns the Gamma function of the given number.
math.Hypot() It returns the square root of the sum of squares of the given parameters (Sqrt(p*p + q*q)), taking care to avoid unnecessary overflow and underflow.
math.Ilogb() It returns the binary exponent of the given number as an integer.
math.Inf() It returns the positive infinity or negative infinity. It returns the positive infinity (+Inf) if sign >= 0, the negative infinity (-Inf) if sign < 0. Where the sign is the given parameter.
math.IsInf() It checks whether the given parameter (f) is an infinity, according to another given parameter (sign). If sign > 0, IsInf() checks whether f is positive infinity. If sign < 0, IsInf() checks whether f is negative infinity. If sign == 0, IsInf() checks whether f is either infinity.
math.IsNaN() It checks whether the given parameter is an IEEE 754 "not-a-number" value.
math.J0() It returns the order-zero Bessel function of the first kind.
math.J1() It returns the order-one Bessel function of the first kind.
math.Jn() It returns the order-n Bessel function of the first kind.
math.Ldexp() It returns the inverse of Frexp. It returns frac × 2**exp.
math.Lgamma() It returns the natural logarithm and sign (-1 or +1) of Gamma(x). Where x is the given parameter.
math.Log() It returns the natural logarithm of the given number.
math.Log10() It returns the decimal logarithm (base-10 logarithm) of the given number.
math.Log1p() It returns the natural logarithm of 1 plus the given value. It is more accurate than Log(1 + x) when x is near zero.
math.Log2() It returns the binary logarithm (base-2 logarithm) of the given number.
math.Logb() It returns the binary exponent of the given number.
math.Max() It returns the larger value of the given parameters.
math.Min() It returns the smaller value of the given parameters.
math.Mod() It returns the floating-point remainder of the given parameters (x/y). The magnitude of the result is less than the second parameter (y) and its sign agrees with that of the first parameter (x).
math.Modf() It returns the integer and fractional floating-point numbers that sum to the given value (f). Both values have the same sign as f.
math.NaN() It returns an IEEE 754 "not-a-number" value.
math.Nextafter() It returns the next representable float64 (floating-point) value after the first parameter (x) towards the second parameter (y).
math.Nextafter32() It returns the next representable float32 (floating-point) value after the first parameter (x) towards the second parameter (y).
math.Pow() It returns the x**y, the base-x exponential of y (we can say x to the power y).
math.Pow10() It returns the 10**n, the base-10 exponential of n (we can say, 10 to the power n).
math.Remainder() It returns the IEEE 754 floating-point remainder of x/y. Where x is the dividend and y is the divisor.
math.Round() It returns the nearest integer, rounding half away from zero.
math.RoundToEven() It returns the nearest integer, rounding ties to even.
math.Signbit() It checks whether the given value is negative or negative zero.
math.Sin() It returns the sine of the given radian value.
math.Sincos() It returns the Sin(x), Cos(x). Where x is the given value.
math.Sinh() It returns the hyperbolic sine of the given parameter.
math.Sqrt() It returns the square root of the given value.
math.Tan() It returns the tangent of the given radian value.
math.Tanh() It returns the hyperbolic tangent of the given value.
math.Trunc() It returns the integer value of the given value.
math.Y0() It returns the order-zero Bessel function of the second kind.
math.Y1() It returns the order-one Bessel function of the second kind.
math.Yn() It returns the order-n Bessel function of the second kind.

Example of Golang math package constants

// Golang program to demonstrate the
// example of math constants

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println("Value of math.E:", math.E)
	fmt.Println("Value of math.Pi:", math.Pi)
	fmt.Println("Value of math.Phi:", math.Phi)
	fmt.Println("Value of math.Sqrt2:", math.Sqrt2)
	fmt.Println("Value of math.SqrtE:", math.SqrtE)
	fmt.Println("Value of math.SqrtPi:", math.SqrtPi)
	fmt.Println("Value of math.SqrtPhi:", math.SqrtPhi)
	fmt.Println("Value of math.Ln2:", math.Ln2)
	fmt.Println("Value of math.Log2E:", math.Log2E)
	fmt.Println("Value of math.Ln10:", math.Ln10)
	fmt.Println("Value of math.Log10E:", math.Log10E)
}

Output:

Value of math.E: 2.718281828459045
Value of math.Pi: 3.141592653589793
Value of math.Phi: 1.618033988749895
Value of math.Sqrt2: 1.4142135623730951
Value of math.SqrtE: 1.6487212707001282
Value of math.SqrtPi: 1.772453850905516
Value of math.SqrtPhi: 1.272019649514069
Value of math.Ln2: 0.6931471805599453
Value of math.Log2E: 1.4426950408889634
Value of math.Ln10: 2.302585092994046
Value of math.Log10E: 0.4342944819032518

Example of math.Sqrt() function

// Golang program to demonstrate the
// example of math.Sqrt() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Sqrt(25))
	fmt.Println(math.Sqrt(225))
	fmt.Println(math.Sqrt(64.25))
	fmt.Println(math.Sqrt(123.45))

	fmt.Println(math.Sqrt(1))
	fmt.Println(math.Sqrt(0))

	fmt.Println(math.Sqrt(-1))
	fmt.Println(math.Sqrt(math.Inf(-1)))
	fmt.Println(math.Sqrt(math.Inf(1)))
	fmt.Println(math.Sqrt(math.NaN()))
}

Output:

5
15
8.0156097709407
11.110805551354051
1
0
NaN
NaN
+Inf
NaN

Example of math.Pow() function

// Golang program to demonstrate the
// example of math.Pow() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Pow(2, 6))
	fmt.Println(math.Pow(2.5, 1.2))
	fmt.Println(math.Pow(-10.23, -20))
	fmt.Println(math.Pow(10, 5))
	fmt.Println(math.Pow(1.49, -2))

	fmt.Println(math.Pow(5.0, 0))
	fmt.Println(math.Pow(1, 10))
	fmt.Println(math.Pow(math.NaN(), 10))
	fmt.Println(math.Pow(math.Inf(1), 0))
}

Output:

64
3.002811084953578
6.3458138139042046e-21
100000
0.4504301608035674
1
1
NaN
1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.