summaryrefslogtreecommitdiff
path: root/src/pkg/math/nextafter.go
diff options
context:
space:
mode:
authorCharles L. Dorian <cldorian@gmail.com>2010-02-09 13:33:12 -0800
committerCharles L. Dorian <cldorian@gmail.com>2010-02-09 13:33:12 -0800
commitdd8b786ff9134bf0aff276bbb8fe0c119e019865 (patch)
treeae49e9428b80e79134ebaec27e793e6a9325e79c /src/pkg/math/nextafter.go
parent6e9262316e7841cab01fbfe73b24a1dddd4d5995 (diff)
downloadgolang-dd8b786ff9134bf0aff276bbb8fe0c119e019865.tar.gz
math: add functions Log2, Nextafter, Fdim, Fmax, Fmin
Add functions, tests and benchmarks. Fix typos in comments in expm1 and hypot_386. Fix Acosh domain error in benchmark test. R=rsc CC=golang-dev http://codereview.appspot.com/204069 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/math/nextafter.go')
-rw-r--r--src/pkg/math/nextafter.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/pkg/math/nextafter.go b/src/pkg/math/nextafter.go
new file mode 100644
index 000000000..b57d3e715
--- /dev/null
+++ b/src/pkg/math/nextafter.go
@@ -0,0 +1,27 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package math
+
+// Nextafter returns the next representable value after x towards y.
+// If x == y, then x is returned.
+//
+// Special cases are:
+// Nextafter(NaN, y) = NaN
+// Nextafter(x, NaN) = NaN
+func Nextafter(x, y float64) (r float64) {
+ switch {
+ case IsNaN(x) || IsNaN(y): // special case
+ r = NaN()
+ case x == y:
+ r = x
+ case x == 0:
+ r = Copysign(Float64frombits(1), y)
+ case (y > x) == (x > 0):
+ r = Float64frombits(Float64bits(x) + 1)
+ default:
+ r = Float64frombits(Float64bits(x) - 1)
+ }
+ return r
+}