abs.go 513 B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2009 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 math
  5. // Abs returns the absolute value of x.
  6. //
  7. // Special cases are:
  8. // Abs(±Inf) = +Inf
  9. // Abs(NaN) = NaN
  10. //extern fabs
  11. func libc_fabs(float64) float64
  12. func Abs(x float64) float64 {
  13. return libc_fabs(x)
  14. }
  15. func abs(x float64) float64 {
  16. switch {
  17. case x < 0:
  18. return -x
  19. case x == 0:
  20. return 0 // return correctly abs(-0)
  21. }
  22. return x
  23. }