github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/src/math/nextafter.go (about)

     1  // Copyright 2010 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  // Nextafter32 returns the next representable float32 value after x towards y.
     8  //
     9  // Special cases are:
    10  //	Nextafter32(x, x)   = x
    11  //	Nextafter32(NaN, y) = NaN
    12  //	Nextafter32(x, NaN) = NaN
    13  func Nextafter32(x, y float32) (r float32) {
    14  	switch {
    15  	case IsNaN(float64(x)) || IsNaN(float64(y)): // special case
    16  		r = float32(NaN())
    17  	case x == y:
    18  		r = x
    19  	case x == 0:
    20  		r = float32(Copysign(float64(Float32frombits(1)), float64(y)))
    21  	case (y > x) == (x > 0):
    22  		r = Float32frombits(Float32bits(x) + 1)
    23  	default:
    24  		r = Float32frombits(Float32bits(x) - 1)
    25  	}
    26  	return
    27  }
    28  
    29  // Nextafter returns the next representable float64 value after x towards y.
    30  //
    31  // Special cases are:
    32  //	Nextafter(x, x)   = x
    33  //	Nextafter(NaN, y) = NaN
    34  //	Nextafter(x, NaN) = NaN
    35  func Nextafter(x, y float64) (r float64) {
    36  	switch {
    37  	case IsNaN(x) || IsNaN(y): // special case
    38  		r = NaN()
    39  	case x == y:
    40  		r = x
    41  	case x == 0:
    42  		r = Copysign(Float64frombits(1), y)
    43  	case (y > x) == (x > 0):
    44  		r = Float64frombits(Float64bits(x) + 1)
    45  	default:
    46  		r = Float64frombits(Float64bits(x) - 1)
    47  	}
    48  	return
    49  }