1
0
mirror of https://github.com/golang/go synced 2024-10-05 09:11:21 -06:00
go/src/math/abs.go
Brad Fitzpatrick a59a27564b math: fix typo and braino in my earlier commit
The bug number was a typo, and I forgot to switch the implementation
back to if statements after the change from Float64bits in the first
patchset back to branching.

if statements can currently be inlined, but switch cannot (#13071)

Change-Id: I81d0cf64bda69186c3d747a07047f6a694f8fa70
Reviewed-on: https://go-review.googlesource.com/16446
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-29 21:12:08 +00:00

24 lines
591 B
Go

// 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.
package math
// Abs returns the absolute value of x.
//
// Special cases are:
// Abs(±Inf) = +Inf
// Abs(NaN) = NaN
func Abs(x float64) float64 {
// TODO: once golang.org/issue/13095 is fixed, change this to:
// return Float64frombits(Float64bits(x) &^ (1 << 63))
// But for now, this generates better code and can also be inlined:
if x < 0 {
return -x
}
if x == 0 {
return 0 // return correctly abs(-0)
}
return x
}