Open In App

math Package in Golang

Last Updated : 15 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
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. 

FunctionDescription
AbsThis function is used to return the absolute value of the specified number.
AcosThis function returns the arccosine, in radians of the specified number.
AcoshThis function returns the inverse hyperbolic cosine of the specified number.
AsinThis function returns the arcsine, in radians of the specified number.
AsinhThis function returns the inverse hyperbolic sine of the specified number.
AtanThis function returns the arctangent, in radians of the specified number.
Atan2This function returns the arc tangent of a/b, using the signs of the two to determine the quadrant of the return value.
AtanhThis function returns the inverse hyperbolic tangent of the specified number.
CbrtThis function returns the cube root of the specified number.
CeilThis function returns the least integer value greater than or equal to the specified number.
CopysignThis function returns a value with the magnitude of a and the sign of b.
CosThis function returns the cosine of the radian argument of the specified number.
CoshThis function returns the hyperbolic cosine of the specified number.
DimThis function returns the maximum of a - b or 0.
ErfThis function returns the error function of the specified number.
ErfcThis function returns the complementary error function of the specified number.
ErfcinvThis function returns the inverse of Erfc(y).
ErfinvThis function returns the inverse error function of the specified number.
ExpThis function returns e**y, the base-e exponential of the specified number.
Exp2This function returns 2**y, the base-2 exponential of the specified number.
Expm1This function returns e**y - 1, the base-e exponential of y minus 1.
FMAThis function returns a * b + c, computed with only one rounding.
Float32bitsThis function returns the IEEE 754 binary representation of x, with the sign bit of x and the result in the same bit position.
Float32frombitsThis 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.
Float64bitsThis 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.
Float64frombitsThis 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.
FloorThis function returns the greatest integer value less than or equal to the specified number.
FrexpThis 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).
GammaThis function returns the Gamma function of the specified number.
HypotThis function returns Sqrt(a*a + b*b), taking care to avoid unnecessary overflow and underflow.
IlogbThis function returns the binary exponent of the specified number as an integer.
InfThis function returns positive infinity if sign >= 0, negative infinity if sign < 0.
IsInfThis function reports whether t is an infinity, according to sign.
IsNaNThis function reports whether t is an IEEE 754 “not-a-number” value.
J0This function returns the order-zero Bessel function of the first kind.
J1This function returns the order-one Bessel function of the first kind.
JnThis function returns the order-n Bessel function of the first kind.
LdexpThis function is the inverse of Frexp.
LgammaThis function returns the natural logarithm and sign (-1 or +1) of Gamma(y).
LogThis function returns the natural logarithm of the specified number.
Log10This function returns the decimal logarithm of the specified number.
Log1pThis function returns the natural logarithm of 1 plus its argument of the specified number.
Log2This function is used to return the binary logarithm of the specified number.
LogbThis function returns the binary exponent of the specified number.
MaxThis function returns the larger of a or b.
MinThis function returns the smaller of a or b.
ModThis function returns the floating-point remainder of a/b
ModfThis function returns integer and fractional floating-point numbers that sum to f.
NaNThis function returns an IEEE 754 “not-a-number” value.
NextafterThis function is used to return the next representable float64 value after a towards b.
Nextafter32This function returns the next representable float32 value after a towards b.
PowThis function returns a**b, the base-a exponential of b.
Pow10This function returns 10**m, the base-10 exponential of m.
RemainderThis function returns the IEEE 754 floating-point remainder of a/b.
RoundThis function is used to return the nearest integer, rounding half away from zero.
RoundToEvenThis function returns the nearest integer, rounding ties to even.
SignbitThis function reports whether x is negative or negative zero.
SinThis function returns the sine of the radian argument y.
SincosThis function returns Sin(x), Cos(x).
SinhThis function returns the hyperbolic sine of the specified number.
SqrtThis function returns the square root of the specified number.
TanThis function returns the tangent of the radian argument y.
TanhThis function returns the hyperbolic tangent of the specified number.
TruncThis function returns the integer value of the specified number.
Y0This function returns the order-zero Bessel function of the second kind.
Y1This function returns the order-one Bessel function of the second kind.
YnThis 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

Next Article
Article Tags :

Similar Reads