Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Remainder or Modulus in Go (Golang)

Posted on March 28, 2023March 28, 2023 by admin

Table of Contents

  • Overview
  • % Operator
    • Code
  • Mod function for floats
    • Code
  • IEEE 754 Remainder
    • Code

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
  • go
  • math
  • mod
  • modulus
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2024 Welcome To Golang By Example | Design: Web XP