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(+Inf) = +Inf 11 // Floor(-Inf) = -Inf 12 // Floor(NaN) = NaN 13 func Floor(x float64) float64 { 14 // TODO(rsc): Remove manual inlining of IsNaN, IsInf 15 // when compiler does it for us 16 if x == 0 || x != x || x > MaxFloat64 || x < -MaxFloat64 { // 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(+Inf) = +Inf 34 // Ceil(-Inf) = -Inf 35 // Ceil(NaN) = NaN 36 func Ceil(x float64) float64 { return -Floor(-x) } 37 38 // Trunc returns the integer value of x. 39 // 40 // Special cases are: 41 // Trunc(+Inf) = +Inf 42 // Trunc(-Inf) = -Inf 43 // Trunc(NaN) = NaN 44 func Trunc(x float64) float64 { 45 // TODO(rsc): Remove manual inlining of IsNaN, IsInf 46 // when compiler does it for us 47 if x == 0 || x != x || x > MaxFloat64 || x < -MaxFloat64 { // x == 0 || IsNaN(x) || IsInf(x, 0) 48 return x 49 } 50 d, _ := Modf(x) 51 return d 52 }