Russ Cox | 488ca3c | 2009-10-15 23:09:22 -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 | |
| 5 | package math |
| 6 | |
| 7 | const ( |
Shenghou Ma | 6e9506a | 2012-08-07 09:57:14 +0800 | [diff] [blame] | 8 | uvnan = 0x7FF8000000000001 |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 9 | uvinf = 0x7FF0000000000000 |
| 10 | uvneginf = 0xFFF0000000000000 |
| 11 | mask = 0x7FF |
| 12 | shift = 64 - 11 - 1 |
Eoghan Sherry | 976e457 | 2010-12-15 13:20:52 -0500 | [diff] [blame] | 13 | bias = 1023 |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0. |
| 17 | func Inf(sign int) float64 { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 18 | var v uint64 |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 19 | if sign >= 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 20 | v = uvinf |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 21 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 22 | v = uvneginf |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 23 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 24 | return Float64frombits(v) |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // NaN returns an IEEE 754 ``not-a-number'' value. |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 28 | func NaN() float64 { return Float64frombits(uvnan) } |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 29 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 30 | // IsNaN reports whether f is an IEEE 754 ``not-a-number'' value. |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 31 | func IsNaN(f float64) (is bool) { |
Russ Cox | 1e9e7ec | 2009-12-15 17:21:01 -0800 | [diff] [blame] | 32 | // IEEE 754 says that only NaNs satisfy f != f. |
| 33 | // To avoid the floating-point hardware, could use: |
| 34 | // x := Float64bits(f); |
| 35 | // return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf |
| 36 | return f != f |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Rob Pike | abe384f | 2013-07-23 11:59:49 +1000 | [diff] [blame] | 39 | // IsInf reports whether f is an infinity, according to sign. |
| 40 | // If sign > 0, IsInf reports whether f is positive infinity. |
| 41 | // If sign < 0, IsInf reports whether f is negative infinity. |
| 42 | // If sign == 0, IsInf reports whether f is either infinity. |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 43 | func IsInf(f float64, sign int) bool { |
Russ Cox | 1e9e7ec | 2009-12-15 17:21:01 -0800 | [diff] [blame] | 44 | // Test for infinity by comparing against maximum float. |
| 45 | // To avoid the floating-point hardware, could use: |
| 46 | // x := Float64bits(f); |
| 47 | // return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf; |
| 48 | return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64 |
Russ Cox | 488ca3c | 2009-10-15 23:09:22 -0700 | [diff] [blame] | 49 | } |
Eoghan Sherry | 13c2e62 | 2011-01-19 14:23:59 -0500 | [diff] [blame] | 50 | |
| 51 | // normalize returns a normal number y and exponent exp |
| 52 | // satisfying x == y × 2**exp. It assumes x is finite and non-zero. |
| 53 | func normalize(x float64) (y float64, exp int) { |
| 54 | const SmallestNormal = 2.2250738585072014e-308 // 2**-1022 |
Rob Pike | 1a13f9b | 2011-09-29 09:54:20 -0700 | [diff] [blame] | 55 | if Abs(x) < SmallestNormal { |
Eoghan Sherry | 13c2e62 | 2011-01-19 14:23:59 -0500 | [diff] [blame] | 56 | return x * (1 << 52), -52 |
| 57 | } |
| 58 | return x, 0 |
| 59 | } |