8000 round: add Round and RoundToEven pure Go implementations by soypat · Pull Request #31 · chewxy/math32 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

round: add Round and RoundToEven pure Go implementations #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package math32
const (
uvnan = 0x7FE00000
uvinf = 0x7F800000
uvone = 0x3f800000
uvneginf = 0xFF800000
mask = 0xFF
shift = 32 - 8 - 1
Expand Down
75 changes: 72 additions & 3 deletions round.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
package math32

import "math"
// Round returns the nearest integer, rounding half away from zero.
//
// Special cases are:
// Round(±0) = ±0
// Round(±Inf) = ±Inf
// Round(NaN) = NaN
func Round(x float32) float32 {
// Round is a faster implementation of:
//
// func Round(x float64) float64 {
// t := Trunc(x)
// if Abs(x-t) >= 0.5 {
// return t + Copysign(1, x)
// }
// return t
// }
bits := Float32bits(x)
e := uint(bits>>shift) & mask
if e < bias {
// Round abs(x) < 1 including denormals.
bits &= signMask // +-0
if e == bias-1 {
bits |= uvone // +-1
}
} else if e < bias+shift {
// Round any abs(x) >= 1 containing a fractional component [0,1).
//
// Numbers with larger exponents are returned unchanged since they
// must be either an integer, infinity, or NaN.
const half = 1 << (shift - 1)
e -= bias
bits += half >> e
bits &^= fracMask >> e
}
return Float32frombits(bits)
}

func Round(value float32) float32 {
return float32(math.Round(float64(value)))
// RoundToEven returns the nearest integer, rounding ties to even.
//
// Special cases are:
// RoundToEven(±0) = ±0
// RoundToEven(±Inf) = ±Inf
// RoundToEven(NaN) = NaN
func RoundToEven(x float32) float32 {
// RoundToEven is a faster implementation of:
//
// func RoundToEven(x float64) float64 {
// t := math.Trunc(x)
// odd := math.Remainder(t, 2) != 0
// if d := math.Abs(x - t); d > 0.5 || (d == 0.5 && odd) {
// return t + math.Copysign(1, x)
// }
// return t
// }
bits := Float32bits(x)
e := uint(bits>>shift) & mask
if e >= bias {
// Round abs(x) >= 1.
// - Large numbers without fractional components, infinity, and NaN are unchanged.
// - Add 0.499.. or 0.5 before truncating depending on whether the truncated
// number is even or odd (respectively).
const halfMinusULP = (1 << (shift - 1)) - 1
e -= bias
bits += (halfMinusULP + (bits>>(shift-e))&1) >> e
bits &^= fracMask >> e
} else if e == bias-1 && bits&fracMask != 0 {
// Round 0.5 < abs(x) < 1.
bits = bits&signMask | uvone // +-1
} else {
// Round abs(x) <= 0.5 including denormals.
bits &= signMask // +-0
}
return Float32frombits(bits)
}
0