src/pkg/math/floor.go - The Go Programming Language

Golang

Source file src/pkg/math/floor.go

     1	// Copyright 2009-2010 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package math
     6	
     7	// Floor returns the greatest integer value less than or equal to x.
     8	//
     9	// Special cases are:
    10	//	Floor(±0) = ±0
    11	//	Floor(±Inf) = ±Inf
    12	//	Floor(NaN) = NaN
    13	func Floor(x float64) float64
    14	
    15	func floor(x float64) float64 {
    16		if x == 0 || IsNaN(x) || IsInf(x, 0) {
    17			return x
    18		}
    19		if x < 0 {
    20			d, fract := Modf(-x)
    21			if fract != 0.0 {
    22				d = d + 1
    23			}
    24			return -d
    25		}
    26		d, _ := Modf(x)
    27		return d
    28	}
    29	
    30	// Ceil returns the least integer value greater than or equal to x.
    31	//
    32	// Special cases are:
    33	//	Ceil(±0) = ±0
    34	//	Ceil(±Inf) = ±Inf
    35	//	Ceil(NaN) = NaN
    36	func Ceil(x float64) float64
    37	
    38	func ceil(x float64) float64 {
    39		return -Floor(-x)
    40	}
    41	
    42	// Trunc returns the integer value of x.
    43	//
    44	// Special cases are:
    45	//	Trunc(±0) = ±0
    46	//	Trunc(±Inf) = ±Inf
    47	//	Trunc(NaN) = NaN
    48	func Trunc(x float64) float64
    49	
    50	func trunc(x float64) float64 {
    51		if x == 0 || IsNaN(x) || IsInf(x, 0) {
    52			return x
    53		}
    54		d, _ := Modf(x)
    55		return d
    56	}