GeeksforGeeks App
Open App
Browser
Continue

# math Package in Golang

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

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`

My Personal Notes arrow_drop_up