Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 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 | |
Rob Pike | 4331293 | 2008-06-27 17:06:23 -0700 | [diff] [blame] | 5 | package math |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 6 | |
Rob Pike | 1a13f9b | 2011-09-29 09:54:20 -0700 | [diff] [blame] | 7 | // Abs returns the absolute value of x. |
Charles L. Dorian | a0690b6 | 2010-02-01 22:21:40 -0800 | [diff] [blame] | 8 | // |
| 9 | // Special cases are: |
Charles L. Dorian | c8d2544 | 2011-11-28 13:04:52 -0800 | [diff] [blame] | 10 | // Abs(±Inf) = +Inf |
Rob Pike | 1a13f9b | 2011-09-29 09:54:20 -0700 | [diff] [blame] | 11 | // Abs(NaN) = NaN |
Brad Fitzpatrick | 6f8a665 | 2015-10-29 15:13:58 +0000 | [diff] [blame] | 12 | func Abs(x float64) float64 { |
Keith Randall | fb05948 | 2017-08-24 13:19:40 -0700 | [diff] [blame] | 13 | return Float64frombits(Float64bits(x) &^ (1 << 63)) |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 14 | } |