github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/math/floor.go (about) 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 14 //extern floor 15 func libc_floor(float64) float64 16 17 func Floor(x float64) float64 { 18 return libc_floor(x) 19 } 20 21 func floor(x float64) float64 { 22 if x == 0 || IsNaN(x) || IsInf(x, 0) { 23 return x 24 } 25 if x < 0 { 26 d, fract := Modf(-x) 27 if fract != 0.0 { 28 d = d + 1 29 } 30 return -d 31 } 32 d, _ := Modf(x) 33 return d 34 } 35 36 // Ceil returns the least integer value greater than or equal to x. 37 // 38 // Special cases are: 39 // Ceil(±0) = ±0 40 // Ceil(±Inf) = ±Inf 41 // Ceil(NaN) = NaN 42 43 //extern ceil 44 func libc_ceil(float64) float64 45 46 func Ceil(x float64) float64 { 47 return libc_ceil(x) 48 } 49 50 func ceil(x float64) float64 { 51 return -Floor(-x) 52 } 53 54 // Trunc returns the integer value of x. 55 // 56 // Special cases are: 57 // Trunc(±0) = ±0 58 // Trunc(±Inf) = ±Inf 59 // Trunc(NaN) = NaN 60 61 func Trunc(x float64) float64 { 62 return trunc(x) 63 } 64 65 func trunc(x float64) float64 { 66 if x == 0 || IsNaN(x) || IsInf(x, 0) { 67 return x 68 } 69 d, _ := Modf(x) 70 return d 71 }