nextafter.go 700 B

12345678910111213141516171819202122232425262728
  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. package math32
  5. // Nextafter returns the next representable float32 value after x towards y.
  6. //
  7. // Special cases are:
  8. //
  9. // Nextafter32(x, x) = x
  10. // Nextafter32(NaN, y) = NaN
  11. // Nextafter32(x, NaN) = NaN
  12. func Nextafter(x, y float32) (r float32) {
  13. switch {
  14. case IsNaN(x) || IsNaN(y): // special case
  15. r = float32(NaN())
  16. case x == y:
  17. r = x
  18. case x == 0:
  19. r = float32(Copysign(Float32frombits(1), y))
  20. case (y > x) == (x > 0):
  21. r = Float32frombits(Float32bits(x) + 1)
  22. default:
  23. r = Float32frombits(Float32bits(x) - 1)
  24. }
  25. return
  26. }