Debian Codegraph @

libgo/go/math/floor.go

raw
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.
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 func Trunc(x float64) float64 {
61 if haveArchTrunc {
62 return archTrunc(x)
63 }
64 return trunc(x)
65 }
66
67 func trunc(x float64) float64 {
68 if x == 0 || IsNaN(x) || IsInf(x, 0) {
69 return x
70 }
71 d, _ := Modf(x)
72 return d
73 }
74
75 // Round returns the nearest integer, rounding half away from zero.
76 //
77 // Special cases are:
78 // Round(±0) = ±0
79 // Round(±Inf) = ±Inf
80 // Round(NaN) = NaN
81 func Round(x float64) float64 {
82 // Round is a faster implementation of:
83 //
84 // func Round(x float64) float64 {
85 // t := Trunc(x)
86 // if Abs(x-t) >= 0.5 {
87 // return t + Copysign(1, x)
88 // }
89 // return t
90 // }
91 bits := Float64bits(x)
92 e := uint(bits>>shift) & mask
93 if e < bias {
94 // Round abs(x) < 1 including denormals.
95 bits &= signMask // +-0
96 if e == bias-1 {
97 bits |= uvone // +-1
98 }
99 } else if e < bias+shift {
100 // Round any abs(x) >= 1 containing a fractional component [0,1).
101 //
102 // Numbers with larger exponents are returned unchanged since they
103 // must be either an integer, infinity, or NaN.
104 const half = 1 << (shift - 1)
105 e -= bias
106 bits += half >> e
107 bits &^= fracMask >> e
108 }
109 return Float64frombits(bits)
110 }
111
112 // RoundToEven returns the nearest integer, rounding ties to even.
113 //
114 // Special cases are:
115 // RoundToEven(±0) = ±0
116 // RoundToEven(±Inf) = ±Inf
117 // RoundToEven(NaN) = NaN
118 func RoundToEven(x float64) float64 {
119 // RoundToEven is a faster implementation of:
120 //
121 // func RoundToEven(x float64) float64 {
122 // t := math.Trunc(x)
123 // odd := math.Remainder(t, 2) != 0
124 // if d := math.Abs(x - t); d > 0.5 || (d == 0.5 && odd) {
125 // return t + math.Copysign(1, x)
126 // }
127 // return t
128 // }
129 bits := Float64bits(x)
130 e := uint(bits>>shift) & mask
131 if e >= bias {
132 // Round abs(x) >= 1.
133 // - Large numbers without fractional components, infinity, and NaN are unchanged.
134 // - Add 0.499.. or 0.5 before truncating depending on whether the truncated
135 // number is even or odd (respectively).
136 const halfMinusULP = (1 << (shift - 1)) - 1
137 e -= bias
138 bits += (halfMinusULP + (bits>>(shift-e))&1) >> e
139 bits &^= fracMask >> e
140 } else if e == bias-1 && bits&fracMask != 0 {
141 // Round 0.5 < abs(x) < 1.
142 bits = bits&signMask | uvone // +-1
143 } else {
144 // Round abs(x) <= 0.5 including denormals.
145 bits &= signMask // +-0
146 }
147 return Float64frombits(bits)
148 }