math Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/math/ Sat, 28 Mar 2020 13:46:21 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 math Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/math/ 32 32 159787465 Max of two numbers in Go (Golang) https://vikasboss.github.io/max-of-two-numbers-go/ https://vikasboss.github.io/max-of-two-numbers-go/#respond Sat, 28 Mar 2020 13:46:11 +0000 https://vikasboss.github.io/?p=1882 Overview math package of GO provides a Max method that can be used to get the maximum of two numbers. Below is the signature of the function. It takes input two float...

The post Max of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Max method that can be used to get the maximum of two numbers.

Below is the signature of the function. It takes input two float numbers and returns a float.

func Max(x, y float64) float64

Also some special cases of Max function 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

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    max := math.Max(2, 3)
    fmt.Println(max)

    max = math.Max(-2.1, -3.3)
    fmt.Println(max)
}

Output:

3
-2.1

The post Max of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/max-of-two-numbers-go/feed/ 0 1882
Min of two numbers in Go (Golang) https://vikasboss.github.io/min-of-two-numbers-golang/ https://vikasboss.github.io/min-of-two-numbers-golang/#respond Sat, 28 Mar 2020 13:41:22 +0000 https://vikasboss.github.io/?p=1877 Overview math package of GO provides a Min method that can be used to get the minimum of two numbers. The Below is the signature of the function. It takes input two...

The post Min of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Min method that can be used to get the minimum of two numbers. The


Below is the signature of the function. It takes input two float numbers and returns a float.

func Min(x, y float64) float64

Also some special cases of Min function are

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

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    min := math.Min(2, 3)
    fmt.Println(min)

    min = math.Min(-2.1, -3.3)
    fmt.Println(min)
}

Output:

2
-3.3

The post Min of two numbers in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/min-of-two-numbers-golang/feed/ 0 1877
Check if a number is negative or positive in Go (Golang) https://vikasboss.github.io/num-positive-negative-go/ https://vikasboss.github.io/num-positive-negative-go/#respond Sat, 28 Mar 2020 10:27:10 +0000 https://vikasboss.github.io/?p=1873 Overview math package of GO provides a Signbit method that can be used to check whether a given number is negative or positive. It returns true for a negative number It returns...

The post Check if a number is negative or positive in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Signbit method that can be used to check whether a given number is negative or positive.

  • It returns true for a negative number
  • It returns false for a positive number

Below is the signature of the function. It takes input a float and returns a bool

func Signbit(x float64) bool 

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    //Will return false for positive
    res := math.Signbit(4)
    fmt.Println(res)

    //Will return true for negative
    res = math.Signbit(-4)
    fmt.Println(res)

    //Will return false for zerp
    res = math.Signbit(0)
    fmt.Println(res)

    //Will return false for positive float
    res = math.Signbit(-0)
    fmt.Println(res)
}

Output:

false
true
false
false

The post Check if a number is negative or positive in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/num-positive-negative-go/feed/ 0 1873
Break number into integer and fraction parts in Go (Golang) https://vikasboss.github.io/break-integer-fraction-part-go/ https://vikasboss.github.io/break-integer-fraction-part-go/#respond Sat, 28 Mar 2020 10:17:04 +0000 https://vikasboss.github.io/?p=1864 Overview math package of GO provides a Modf method that can be used to break a floating-point number into integer and floating part. Please note that the integer part is also returned...

The post Break number into integer and fraction parts in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Modf method that can be used to break a floating-point number into integer and floating part. Please note that the integer part is also returned as a float by this function.

Below is the signature of the function. It takes input as a float number and returns two float64. The first one is the integer part and second is the fractional part.

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

Some points to note about above function

  • The return values int and frac add up to input f
  • int and frac has same sign a input f

Also some special cases of Modf function are

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

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    //Contain both integer and fraction
    integer, fraction := math.Modf(2.5)
    fmt.Printf("Integer: %f. Fraction: %f\n", integer, fraction)

    //Contain only integer part
    integer, fraction = math.Modf(2)
    fmt.Printf("Integer: %f. Fraction: %f\n", integer, fraction)

    //Negative floating point number
    integer, fraction = math.Modf(-2.5)
    fmt.Printf("Integer: %f. Fraction: %f\n", integer, fraction)

    //Contains only fraction part
    integer, fraction = math.Modf(0.5)
    fmt.Printf("Integer: %f. Fraction: %f\n", integer, fraction)
}

Output:

Integer: 2.000000. Fraction: 0.500000
Integer: 2.000000. Fraction: 0.000000
Integer: -2.000000. Fraction: -0.500000
Integer: 0.000000. Fraction: 0.500000

The post Break number into integer and fraction parts in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/break-integer-fraction-part-go/feed/ 0 1864
Remainder or Modulus in Go (Golang) https://vikasboss.github.io/remainder-modulus-go-golang/ https://vikasboss.github.io/remainder-modulus-go-golang/#respond Sat, 28 Mar 2020 10:12:07 +0000 https://vikasboss.github.io/?p=1859 Overview In this tutorial we will study about % Operator – applicable to get remainder for integers Mod function – can be used to get remainder in case of floats also Remainder...

The post Remainder or Modulus in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

In this tutorial we will study about

  • % Operator – applicable to get remainder for integers
  • Mod function – can be used to get remainder in case of floats also
  • Remainder function – can be used to get IEEE 754 remainder

% Operator

Golang has a modulus operator (‘ %’), that can be used to get the remainder on dividing two integer numbers. Let’s see a working program illustrating this

Code

package main

import (
    "fmt"
)

func main() {
    res := 4 % 2
    fmt.Println(res)

    res = 5 % 2
    fmt.Println(res)

    res = 8 % 3
    fmt.Println(res)
}

Output:

0
1
2

Mod function for floats

% function doesn’t work for floats. For getting remainder when dividing two floats we can use the Mod function provided by the math package itself. Below is the signature of the function. It takes in two floats and returns a float. It will return floating point remainder of x/y. The output will take the sign on x

func Mod(x, y float64) float64

Some special cases of Mod function are

  • Mod(±Inf, y) = NaN
  • Mod(NaN, y) = NaN
  • Mod(x, 0) = NaN
  • Mod(x, ±Inf) = x
  • Mod(x, NaN) = NaN

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Mod(4, 2)
    fmt.Println(res)

    res = math.Mod(4.2, 2)
    fmt.Println(res)

    res = math.Mod(5, 2)
    fmt.Println(res)

    res = math.Mod(-5, 2)
    fmt.Println(res)
}

Output

0
0.20000000000000018
1
-1

IEEE 754 Remainder

math package of GO provides a Remainder method that can be used to get IEEE 754 Remainder of two numbers, one acting as a Numerator and the acting as a denominator.

You can read more about why do we need IEEE 754 Remainder here – https://stackoverflow.com/questions/26671975/why-do-we-need-ieee-754-remainder

Below is the signature of the function. It takes input two numbers as float64 and returns a remainder which is also an IEEE 754 float64 Remainder.

func Remainder(x, y float64) float64

Some special cases of Remainder function are

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

Code

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Remainder(4, 2)
    fmt.Println(res)

    res = math.Remainder(5, 2)
    fmt.Println(res)

    res = math.Remainder(5.5, 2)
    fmt.Println(res)

    res = math.Remainder(5.5, 1.5)
    fmt.Println(res)
}

Output

0
1
-0.5
-0.5

The post Remainder or Modulus in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/remainder-modulus-go-golang/feed/ 0 1859
Square Root of a number in Go (Golang) https://vikasboss.github.io/square-root-number-golang/ https://vikasboss.github.io/square-root-number-golang/#respond Thu, 26 Mar 2020 05:40:18 +0000 https://vikasboss.github.io/?p=1846 Overview math package of GO provides a Sqrt method that can be used to get the cube root of that number. Below is the signature of the function. It takes input a...

The post Square Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Sqrt method that can be used to get the cube root of that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Sqrt(x float64) float64

Some special cases of Sqrt function are

  • Sqrt(±0) = ±0
  • Sqrt(±Inf) = ±Inf
  • Sqrt(x < 0) = NaN
  • Sqrt(NaN) = NaN

Code:

package main

import (
	"fmt"
	"math"
)

func main() {
	//Square root of a integer
	res := math.Sqrt(4)
	fmt.Println(res)

	//Square root of a integer
	res = math.Sqrt(9)
	fmt.Println(res)

	//Square Root of a float
	res = math.Sqrt(30.33)
	fmt.Println(res)

	//Square Root of a negative number
	res = math.Sqrt(-9)
	fmt.Println(res)
}

Output:

2
3
5.5072679252057455

The post Square Root of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/square-root-number-golang/feed/ 0 1846
Pi value in Go (Golang) https://vikasboss.github.io/pi-value-golang/ https://vikasboss.github.io/pi-value-golang/#respond Thu, 26 Mar 2020 05:12:58 +0000 https://vikasboss.github.io/?p=1839 Overview math package of GO provides a Pi constant. This is how it is defined in the constant.go file of math package. It is a float64 value Code: Output:

The post Pi value in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Pi constant.


This is how it is defined in the constant.go file of math package. It is a float64 value

Pi  = 3.14159265358979323846264338327950288419716939937510582097494459

Code:

package main

import (
	"fmt"
	"math"
)

func main() {
	pi := math.Pi
	fmt.Println(pi)
}

Output:

3.141592653589793

The post Pi value in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/pi-value-golang/feed/ 0 1839
Get absolute value of a number in Go (Golang) https://vikasboss.github.io/absolute-value-number-golang/ https://vikasboss.github.io/absolute-value-number-golang/#respond Thu, 26 Mar 2020 05:02:27 +0000 https://vikasboss.github.io/?p=1836 Overview math package of GO provides an Abs method that can be used to get the absolute value of a number. Below is the signature of the function. It takes input a...

The post Get absolute value of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides an Abs method that can be used to get the absolute value of a number.

Below is the signature of the function. It takes input a float and also returns a float.

func Abs(x float64) float64

Some special cases of Abs function are

  • Abs(±0) = ±0
  • Abs(±Inf) = ±Inf
  • Abs(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Abs(-2)
    fmt.Println(res)

    res = math.Abs(2)
    fmt.Println(res)

    res = math.Abs(3.5)
    fmt.Println(res)

    res = math.Abs(-3.5)
    fmt.Println(res)
}

Output:

2
2
3.5
3.5

The post Get absolute value of a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/absolute-value-number-golang/feed/ 0 1836
Remove decimal points from a float in Go (Golang) https://vikasboss.github.io/remove-decimal-float-go/ https://vikasboss.github.io/remove-decimal-float-go/#respond Wed, 25 Mar 2020 18:14:11 +0000 https://vikasboss.github.io/?p=1828 Overview math package of GO provides a Trunc method that can be used to remove decimal points of a float and convert it to an integer Below is the signature of the...

The post Remove decimal points from a float in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a Trunc method that can be used to remove decimal points of a float and convert it to an integer

Below is the signature of the function. It takes input a float and also returns a float.

func Trunc(x float64) float64\

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Trunc(1.6)
    fmt.Println(res)

    res = math.Trunc(-1.6)
    fmt.Println(res)

    res = math.Trunc(1)
    fmt.Println(res)
}

Output:

1
-1
1

The post Remove decimal points from a float in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/remove-decimal-float-go/feed/ 0 1828
Round Even a number in Go (Golang) https://vikasboss.github.io/round-even-number-golang/ https://vikasboss.github.io/round-even-number-golang/#respond Wed, 25 Mar 2020 18:03:54 +0000 https://vikasboss.github.io/?p=1825 Overview math package of GO provides a RoundToEven method that can be used to round to even a number. It returns the nearest integer rounding ties to even. Here you can read...

The post Round Even a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a RoundToEven method that can be used to round to even a number. It returns the nearest integer rounding ties to even.

Here you can read about use case of RoundToEven – https://mathematica.stackexchange.com/questions/2116/why-round-to-even-integers/2120


Below is the signature of the function. It takes input a float and also returns a float.

func RoundToEven(x float64) float64


Some special cases of RoundToEven function are

  • RoundToEven(±0) = ±0
  • RoundToEven(±Inf) = ±Inf
  • RoundToEven(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.RoundToEven(0.5)
    fmt.Println(res)

    res = math.RoundToEven(1.5)
    fmt.Println(res)

    res = math.RoundToEven(2.5)
    fmt.Println(res)

    res = math.RoundToEven(3.5)
    fmt.Println(res)

    res = math.RoundToEven(4.5)
    fmt.Println(res)
}

Output:

0
2
2
4
4

The post Round Even a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/round-even-number-golang/feed/ 0 1825