Source file src/math/nextafter.go

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

View as plain text