Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Finding Mod of Given Number in Golang

  • Last Updated : 13 Apr, 2020

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find a mod or floating-point remainder of specified a/b with the help of Mod() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Mod() function.

Syntax:

func Min(a, b float64) float64
  • In this function, the magnitude of the result is less than b and its sign agrees with that of a.
  • If you pass -Inf or +Inf in this function like Mod(-Inf, b) or Mod(+Inf, b), then this function will return NaN.
  • If you pass NaN in this function like Mod(NaN, b), then this function will return NaN.
  • If you pass b=0 in this function like Mod(a, 0), then this function will return NaN.
  • If you pass -Inf or +Inf in this function like Mod(a, -Inf) or Mod(b, +Inf), then this function will return a.
  • If you pass NaN in this function like Mod(a, NaN), then this function will return NaN.

Example 1:




// Golang program to illustrate how to
// find mod of the specified numbers
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding mod of the given numbers
    // Using Mod() function
    res_1 := math.Mod(60, 5)
    res_2 := math.Mod(-100, 100)
    res_3 := math.Mod(45.6, 8.9)
    res_4 := math.Mod(math.NaN(), 67)
    res_5 := math.Mod(math.Inf(1), 67)
  
    // Displaying the result
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2: %.1f", res_2)
    fmt.Printf("\nResult 3: %.1f", res_3)
    fmt.Printf("\nResult 4: %.1f", res_4)
    fmt.Printf("\nResult 5: %.1f", res_5)
  
}

Output:

Result 1: 0.0
Result 2: -0.0
Result 3: 1.1
Result 4: NaN
Result 5: NaN

Example 2:




// Golang program to illustrate how to
// find mod of the specified numbers
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding mod of 
    // the given numbers
    // Using Mod() function
    nvalue_1 := math.Mod(34, 6)
    nvalue_2 := math.Mod(56.7, 3.4)
  
    // Finding sum of the given mod
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.2f + %.2f = %.2f"
            nvalue_1, nvalue_2, res)
  
}

Output:

4.00 + 2.30 = 6.30



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!