2008-03-28 14:56:47 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
package math
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2009-03-05 14:31:01 -07:00
|
|
|
// Fabs returns the absolute value of x.
|
2010-02-01 23:21:40 -07:00
|
|
|
//
|
|
|
|
// Special cases are:
|
|
|
|
// Fabs(+Inf) = +Inf
|
|
|
|
// Fabs(-Inf) = +Inf
|
|
|
|
// Fabs(NaN) = NaN
|
2009-03-05 14:31:01 -07:00
|
|
|
func Fabs(x float64) float64 {
|
2010-04-26 23:44:39 -06:00
|
|
|
switch {
|
|
|
|
case x < 0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return -x
|
2010-04-26 23:44:39 -06:00
|
|
|
case x == 0:
|
|
|
|
return 0 // return correctly fabs(-0)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return x
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|