PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / libgo / go / math / floor.go
blob9115968c74a12ccda29b7ba7eda5098fdec1bd80
1 // Copyright 2009 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.
5 package math
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
14 //extern floor
15 func libc_floor(float64) float64
17 func Floor(x float64) float64 {
18 return libc_floor(x)
21 func floor(x float64) float64 {
22 if x == 0 || IsNaN(x) || IsInf(x, 0) {
23 return x
25 if x < 0 {
26 d, fract := Modf(-x)
27 if fract != 0.0 {
28 d = d + 1
30 return -d
32 d, _ := Modf(x)
33 return d
36 // Ceil returns the least integer value greater than or equal to x.
38 // Special cases are:
39 // Ceil(±0) = ±0
40 // Ceil(±Inf) = ±Inf
41 // Ceil(NaN) = NaN
43 //extern ceil
44 func libc_ceil(float64) float64
46 func Ceil(x float64) float64 {
47 return libc_ceil(x)
50 func ceil(x float64) float64 {
51 return -Floor(-x)
54 // Trunc returns the integer value of x.
56 // Special cases are:
57 // Trunc(±0) = ±0
58 // Trunc(±Inf) = ±Inf
59 // Trunc(NaN) = NaN
61 func Trunc(x float64) float64 {
62 return trunc(x)
65 func trunc(x float64) float64 {
66 if x == 0 || IsNaN(x) || IsInf(x, 0) {
67 return x
69 d, _ := Modf(x)
70 return d
73 // Round returns the nearest integer, rounding half away from zero.
75 // Special cases are:
76 // Round(±0) = ±0
77 // Round(±Inf) = ±Inf
78 // Round(NaN) = NaN
79 func Round(x float64) float64 {
80 // Round is a faster implementation of:
82 // func Round(x float64) float64 {
83 // t := Trunc(x)
84 // if Abs(x-t) >= 0.5 {
85 // return t + Copysign(1, x)
86 // }
87 // return t
88 // }
89 bits := Float64bits(x)
90 e := uint(bits>>shift) & mask
91 if e < bias {
92 // Round abs(x) < 1 including denormals.
93 bits &= signMask // +-0
94 if e == bias-1 {
95 bits |= uvone // +-1
97 } else if e < bias+shift {
98 // Round any abs(x) >= 1 containing a fractional component [0,1).
100 // Numbers with larger exponents are returned unchanged since they
101 // must be either an integer, infinity, or NaN.
102 const half = 1 << (shift - 1)
103 e -= bias
104 bits += half >> e
105 bits &^= fracMask >> e
107 return Float64frombits(bits)
110 // RoundToEven returns the nearest integer, rounding ties to even.
112 // Special cases are:
113 // RoundToEven(±0) = ±0
114 // RoundToEven(±Inf) = ±Inf
115 // RoundToEven(NaN) = NaN
116 func RoundToEven(x float64) float64 {
117 // RoundToEven is a faster implementation of:
119 // func RoundToEven(x float64) float64 {
120 // t := math.Trunc(x)
121 // odd := math.Remainder(t, 2) != 0
122 // if d := math.Abs(x - t); d > 0.5 || (d == 0.5 && odd) {
123 // return t + math.Copysign(1, x)
124 // }
125 // return t
126 // }
127 bits := Float64bits(x)
128 e := uint(bits>>shift) & mask
129 if e >= bias {
130 // Round abs(x) >= 1.
131 // - Large numbers without fractional components, infinity, and NaN are unchanged.
132 // - Add 0.499.. or 0.5 before truncating depending on whether the truncated
133 // number is even or odd (respectively).
134 const halfMinusULP = (1 << (shift - 1)) - 1
135 e -= bias
136 bits += (halfMinusULP + (bits>>(shift-e))&1) >> e
137 bits &^= fracMask >> e
138 } else if e == bias-1 && bits&fracMask != 0 {
139 // Round 0.5 < abs(x) < 1.
140 bits = bits&signMask | uvone // +-1
141 } else {
142 // Round abs(x) <= 0.5 including denormals.
143 bits &= signMask // +-0
145 return Float64frombits(bits)