Ian Lance Taylor | dbc0c7e | 2018-01-05 13:26:30 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 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 | |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 7 | // Floor returns the greatest integer value less than or equal to x. |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 8 | // |
| 9 | // Special cases are: |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 10 | // Floor(±0) = ±0 |
Ian Lance Taylor | ce5916e | 2011-12-13 11:14:08 -0800 | [diff] [blame] | 11 | // Floor(±Inf) = ±Inf |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 12 | // Floor(NaN) = NaN |
Ian Lance Taylor | adccafd | 2012-02-07 11:19:23 -0800 | [diff] [blame] | 13 | |
| 14 | //extern floor |
| 15 | func libc_floor(float64) float64 |
| 16 | |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 17 | func Floor(x float64) float64 { |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 18 | return libc_floor(x) |
| 19 | } |
| 20 | |
| 21 | func floor(x float64) float64 { |
Ian Lance Taylor | 8cf10fb | 2012-02-09 00:06:37 -0800 | [diff] [blame] | 22 | if x == 0 || IsNaN(x) || IsInf(x, 0) { |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 23 | return x |
| 24 | } |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 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. |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 37 | // |
| 38 | // Special cases are: |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 39 | // Ceil(±0) = ±0 |
Ian Lance Taylor | ce5916e | 2011-12-13 11:14:08 -0800 | [diff] [blame] | 40 | // Ceil(±Inf) = ±Inf |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 41 | // Ceil(NaN) = NaN |
Ian Lance Taylor | adccafd | 2012-02-07 11:19:23 -0800 | [diff] [blame] | 42 | |
| 43 | //extern ceil |
| 44 | func libc_ceil(float64) float64 |
| 45 | |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 46 | func Ceil(x float64) float64 { |
| 47 | return libc_ceil(x) |
| 48 | } |
| 49 | |
| 50 | func ceil(x float64) float64 { |
| 51 | return -Floor(-x) |
| 52 | } |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 53 | |
| 54 | // Trunc returns the integer value of x. |
| 55 | // |
| 56 | // Special cases are: |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 57 | // Trunc(±0) = ±0 |
Ian Lance Taylor | ce5916e | 2011-12-13 11:14:08 -0800 | [diff] [blame] | 58 | // Trunc(±Inf) = ±Inf |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 59 | // Trunc(NaN) = NaN |
Ian Lance Taylor | adccafd | 2012-02-07 11:19:23 -0800 | [diff] [blame] | 60 | |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 61 | func Trunc(x float64) float64 { |
Ian Lance Taylor | e76b03d | 2012-02-15 23:15:01 -0800 | [diff] [blame] | 62 | return trunc(x) |
Ian Lance Taylor | 0f0f9c1 | 2012-01-11 17:09:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func trunc(x float64) float64 { |
Ian Lance Taylor | 8cf10fb | 2012-02-09 00:06:37 -0800 | [diff] [blame] | 66 | if x == 0 || IsNaN(x) || IsInf(x, 0) { |
Ian Lance Taylor | fb6b68d | 2010-02-19 21:28:16 -0800 | [diff] [blame] | 67 | return x |
| 68 | } |
| 69 | d, _ := Modf(x) |
| 70 | return d |
| 71 | } |
Ian Lance Taylor | dbc0c7e | 2018-01-05 13:26:30 -0800 | [diff] [blame] | 72 | |
| 73 | // Round returns the nearest integer, rounding half away from zero. |
| 74 | // |
| 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: |
| 81 | // |
| 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 |
| 96 | } |
| 97 | } else if e < bias+shift { |
| 98 | // Round any abs(x) >= 1 containing a fractional component [0,1). |
| 99 | // |
| 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 |
| 106 | } |
| 107 | return Float64frombits(bits) |
| 108 | } |
| 109 | |
| 110 | // RoundToEven returns the nearest integer, rounding ties to even. |
| 111 | // |
| 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: |
| 118 | // |
| 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 |
| 144 | } |
| 145 | return Float64frombits(bits) |
| 146 | } |