Open In App

math Package in Golang

Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. 

Function Description
Abs This function is used to return the absolute value of the specified number.
Acos This function returns the arccosine, in radians of the specified number.
Acosh This function returns the inverse hyperbolic cosine of the specified number.
Asin This function returns the arcsine, in radians of the specified number.
Asinh This function returns the inverse hyperbolic sine of the specified number.
Atan This function returns the arctangent, in radians of the specified number.
Atan2 This function returns the arc tangent of a/b, using the signs of the two to determine the quadrant of the return value.
Atanh This function returns the inverse hyperbolic tangent of the specified number.
Cbrt This function returns the cube root of the specified number.
Ceil This function returns the least integer value greater than or equal to the specified number.
Copysign This function returns a value with the magnitude of a and the sign of b.
Cos This function returns the cosine of the radian argument of the specified number.
Cosh This function returns the hyperbolic cosine of the specified number.
Dim This function returns the maximum of a – b or 0.
Erf This function returns the error function of the specified number.
Erfc This function returns the complementary error function of the specified number.
Erfcinv This function returns the inverse of Erfc(y).
Erfinv This function returns the inverse error function of the specified number.
Exp This function returns e**y, the base-e exponential of the specified number.
Exp2 This function returns 2**y, the base-2 exponential of the specified number.
Expm1 This function returns e**y – 1, the base-e exponential of y minus 1.
FMA This function returns a * b + c, computed with only one rounding.
Float32bits This function returns the IEEE 754 binary representation of x, with the sign bit of x and the result in the same bit position.
Float32frombits This function returns the floating-point number corresponding to the IEEE 754 binary representation x, with the sign bit of x and the result in the same bit position.
Float64bits This function returns the IEEE 754 binary representation of x, with the sign bit of x and the result in the same bit position, and Float64bits(Float64frombits(y)) == y.
Float64frombits This function returns the floating-point number corresponding to the IEEE 754 binary representation x, with the sign bit of x and the result in the same bit position.
Floor This function returns the greatest integer value less than or equal to the specified number.
Frexp This function is used to breaks t into a normalized fraction and an integral power of two and returns frac and exp satisfying t == frac × 2**exp, with the absolute value of frac in the interval [½, 1).
Gamma This function returns the Gamma function of the specified number.
Hypot This function returns Sqrt(a*a + b*b), taking care to avoid unnecessary overflow and underflow.
Ilogb This function returns the binary exponent of the specified number as an integer.
Inf This function returns positive infinity if sign >= 0, negative infinity if sign < 0.
IsInf This function reports whether t is an infinity, according to sign.
IsNaN This function reports whether t is an IEEE 754 “not-a-number” value.
J0 This function returns the order-zero Bessel function of the first kind.
J1 This function returns the order-one Bessel function of the first kind.
Jn This function returns the order-n Bessel function of the first kind.
Ldexp This function is the inverse of Frexp.
Lgamma This function returns the natural logarithm and sign (-1 or +1) of Gamma(y).
Log This function returns the natural logarithm of the specified number.
Log10 This function returns the decimal logarithm of the specified number.
Log1p This function returns the natural logarithm of 1 plus its argument of the specified number.
Log2 This function is used to return the binary logarithm of the specified number.
Logb This function returns the binary exponent of the specified number.
Max This function returns the larger of a or b.
Min This function returns the smaller of a or b.
Mod This function returns the floating-point remainder of a/b
Modf This function returns integer and fractional floating-point numbers that sum to f.
NaN This function returns an IEEE 754 “not-a-number” value.
Nextafter This function is used to return the next representable float64 value after a towards b.
Nextafter32 This function returns the next representable float32 value after a towards b.
Pow This function returns a**b, the base-a exponential of b.
Pow10 This function returns 10**m, the base-10 exponential of m.
Remainder This function returns the IEEE 754 floating-point remainder of a/b.
Round This function is used to return the nearest integer, rounding half away from zero.
RoundToEven This function returns the nearest integer, rounding ties to even.
Signbit This function reports whether x is negative or negative zero.
Sin This function returns the sine of the radian argument y.
Sincos This function returns Sin(x), Cos(x).
Sinh This function returns the hyperbolic sine of the specified number.
Sqrt This function returns the square root of the specified number.
Tan This function returns the tangent of the radian argument y.
Tanh This function returns the hyperbolic tangent of the specified number.
Trunc This function returns the integer value of the specified number.
Y0 This function returns the order-zero Bessel function of the second kind.
Y1 This function returns the order-one Bessel function of the second kind.
Yn This function returns the order-n Bessel function of the second kind.

Example 1: 

Go




// Golang program to illustrate how to
// find the IEEE 754 binary representation
package main
 
import (
    "fmt"
    "math"
)
 
// Main function
func main() {
 
    // Finding IEEE 754 binary
    // representation of the
    // given numbers
    // Using Float64bits() function
    res_1 := math.Float64bits(2)
    res_2 := math.Float64bits(1)
    res_3 := math.Float64bits(0)
    res_4 := math.Float64bits(2.3)
 
    // Displaying the result
    fmt.Println("Result 1: ", res_1)
    fmt.Println("Result 2: ", res_2)
    fmt.Println("Result 3: ", res_3)
    fmt.Println("Result 4: ", res_4)
 
}


Output:

Result 1: 4611686018427387904
Result 2: 4607182418800017408
Result 3: 0
Result 4: 4612361558371493478

Example 2: 

Go




// Golang program to illustrate
// the use of math.Yn() function
 
package main
 
import (
    "fmt"
    "math"
)
 
// Main function
func main() {
 
    // Finding the order-n Bessel
    // function of the second kind
    // Using Yn() function
    res_1 := math.Yn(-3, -2)
    res_2 := math.Yn(6, 3)
    res_3 := math.Yn(1, 1.1)
    res_4 := math.Yn(1, math.NaN())
    res_5 := math.Yn(-1, 0)
 
    // Displaying the result
    fmt.Println("Result 1: ", res_1)
    fmt.Println("Result 2: ", res_2)
    fmt.Println("Result 3: ", res_3)
    fmt.Println("Result 4: ", res_4)
    fmt.Println("Result 5: ", res_5)
 
}


Output:

Result 1:  NaN
Result 2:  -5.436470340703773
Result 3:  -0.698119560067667
Result 4:  NaN
Result 5:  +Inf

Example 3 : 

Go




package main
 
import (
    "fmt"
    "math"
)
 
func main() {
    //Abs returns the absolute value
    a := math.Abs(-5)
    //Round returns the nearest integer, rounding half away from zero.
    b := math.Round(4.544)
    //RoundToEven returns the nearest integer, rounding ties to even.
    d := math.RoundToEven(4.6)
    //Cbrt returns the cube root
    c := math.Cbrt(64)
    //Ceil returns the least integer value greater than or equal to given argument
    e := math.Ceil(4.5)
    //Floor returns the greatest integer value less than or equal to given argument
    f := math.Floor(5.7)
    //Copysign returns a value with the magnitude of first argument and the sign of second argument
    g := math.Copysign(5.6, -5)
    //Max returns the larger of two arguments passed
    h := math.Max(5, 7)
    //Min returns the smaller of two arguments passed
    i := math.Min(5, 7)
    //Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x.(x,y are arguments)
    j := math.Mod(10, 3)
    //Nextafter returns the next representable float64 value after x towards y.(x,y are arguments)
    k := math.Nextafter(5.5, 5.8)
    //Trunc returns the integer value of x(x - argument)
    l := math.Trunc(10.998)
    //Remainder returns the IEEE 754 floating-point remainder of x/y.(x,y-arguments)
    m := math.Remainder(11, 9)
    // Pow10 returns 10**n, the base-10 exponential of n.(n-argument)
    n := math.Pow10(4)
    //Pow returns x**y, the base-x exponential of y.(x,y-arguments)
    o := math.Pow(7, 3)
    fmt.Println(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
}


Output

5 5 4 5 5 5 -5.6 7 5 1 5.500000000000001 10 2 10000 343


Last Updated : 15 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads