×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Floor() Function with Examples

Golang | math.Floor() Function: Here, we are going to learn about the Floor() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 31, 2021

math.Floor()

The Floor() function is an inbuilt function of the math package which is used to get the greatest integer value less than or equal to the given number.

It accepts a parameter (x) and returns the greatest integer value less than or equal to x.

Syntax

func Floor(x float64) float64

Parameters

  • x : The value whose floor value is to be found.

Return Value

The return type of Floor() function is a float64, it returns the greatest integer value less than or equal to the given number.

Special Cases

  • Floor(±0) = ±0
    If the parameter is ±0, the function returns the same (±0).
  • Floor(±Inf) = ±Inf
    If the parameter is ±Inf, the function returns the same (±Inf).
  • Floor(NaN) = NaN
    If the parameter is NaN, the function returns the same (NaN).

Example 1

// Golang program to demonstrate the
// example of math.Floor() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Floor(1))
	fmt.Println(math.Floor(1.49))
	fmt.Println(math.Floor(1.50))
	fmt.Println(math.Floor(1.75))

	fmt.Println(math.Floor(-1))
	fmt.Println(math.Floor(-1.49))
	fmt.Println(math.Floor(-1.50))
	fmt.Println(math.Floor(-1.75))

	fmt.Println(math.Floor(0))
	fmt.Println(math.Floor(math.Inf(-1)))
	fmt.Println(math.Floor(math.Inf(+1)))
	fmt.Println(math.Floor(math.NaN()))
}

Output:

1
1
1
1
-1
-2
-2
-2
0
-Inf
+Inf
NaN

Example 2

// Golang program to demonstrate the
// example of math.Floor() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var FloorX float64

	x = 1
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = 1.49
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = 1.75
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = -1.5
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = 0
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = math.Inf(1)
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)

	x = math.NaN()
	FloorX = math.Floor(x)
	fmt.Println("Floor(", x, ") = ", FloorX)
}

Output:

Floor( 1 ) =  1
Floor( 1.49 ) =  1
Floor( 1.75 ) =  1
Floor( -1.5 ) =  -2
Floor( 0 ) =  0
Floor( +Inf ) =  +Inf
Floor( NaN ) =  NaN

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.