gonum.org/v1/gonum@v0.14.0/internal/math32/math.go (about)

     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  // Copyright ©2015 The Gonum Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package math32
    10  
    11  import (
    12  	"math"
    13  )
    14  
    15  const (
    16  	unan    = 0x7fc00000
    17  	uinf    = 0x7f800000
    18  	uneginf = 0xff800000
    19  	mask    = 0x7f8 >> 3
    20  	shift   = 32 - 8 - 1
    21  	bias    = 127
    22  )
    23  
    24  // Abs returns the absolute value of x.
    25  //
    26  // Special cases are:
    27  //
    28  //	Abs(±Inf) = +Inf
    29  //	Abs(NaN) = NaN
    30  func Abs(x float32) float32 {
    31  	switch {
    32  	case x < 0:
    33  		return -x
    34  	case x == 0:
    35  		return 0 // return correctly abs(-0)
    36  	}
    37  	return x
    38  }
    39  
    40  // Copysign returns a value with the magnitude
    41  // of x and the sign of y.
    42  func Copysign(x, y float32) float32 {
    43  	const sign = 1 << 31
    44  	return math.Float32frombits(math.Float32bits(x)&^sign | math.Float32bits(y)&sign)
    45  }
    46  
    47  // Hypot returns Sqrt(p*p + q*q), taking care to avoid
    48  // unnecessary overflow and underflow.
    49  //
    50  // Special cases are:
    51  //
    52  //	Hypot(±Inf, q) = +Inf
    53  //	Hypot(p, ±Inf) = +Inf
    54  //	Hypot(NaN, q) = NaN
    55  //	Hypot(p, NaN) = NaN
    56  func Hypot(p, q float32) float32 {
    57  	// special cases
    58  	switch {
    59  	case IsInf(p, 0) || IsInf(q, 0):
    60  		return Inf(1)
    61  	case IsNaN(p) || IsNaN(q):
    62  		return NaN()
    63  	}
    64  	if p < 0 {
    65  		p = -p
    66  	}
    67  	if q < 0 {
    68  		q = -q
    69  	}
    70  	if p < q {
    71  		p, q = q, p
    72  	}
    73  	if p == 0 {
    74  		return 0
    75  	}
    76  	q = q / p
    77  	return p * Sqrt(1+q*q)
    78  }
    79  
    80  // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
    81  func Inf(sign int) float32 {
    82  	var v uint32
    83  	if sign >= 0 {
    84  		v = uinf
    85  	} else {
    86  		v = uneginf
    87  	}
    88  	return math.Float32frombits(v)
    89  }
    90  
    91  // IsInf reports whether f is an infinity, according to sign.
    92  // If sign > 0, IsInf reports whether f is positive infinity.
    93  // If sign < 0, IsInf reports whether f is negative infinity.
    94  // If sign == 0, IsInf reports whether f is either infinity.
    95  func IsInf(f float32, sign int) bool {
    96  	// Test for infinity by comparing against maximum float.
    97  	// To avoid the floating-point hardware, could use:
    98  	//	x := math.Float32bits(f);
    99  	//	return sign >= 0 && x == uinf || sign <= 0 && x == uneginf;
   100  	return sign >= 0 && f > math.MaxFloat32 || sign <= 0 && f < -math.MaxFloat32
   101  }
   102  
   103  // IsNaN reports whether f is an IEEE 754 “not-a-number” value.
   104  func IsNaN(f float32) (is bool) {
   105  	// IEEE 754 says that only NaNs satisfy f != f.
   106  	// To avoid the floating-point hardware, could use:
   107  	//	x := math.Float32bits(f);
   108  	//	return uint32(x>>shift)&mask == mask && x != uinf && x != uneginf
   109  	return f != f
   110  }
   111  
   112  // Max returns the larger of x or y.
   113  //
   114  // Special cases are:
   115  //
   116  //	Max(x, +Inf) = Max(+Inf, x) = +Inf
   117  //	Max(x, NaN) = Max(NaN, x) = NaN
   118  //	Max(+0, ±0) = Max(±0, +0) = +0
   119  //	Max(-0, -0) = -0
   120  func Max(x, y float32) float32 {
   121  	// special cases
   122  	switch {
   123  	case IsInf(x, 1) || IsInf(y, 1):
   124  		return Inf(1)
   125  	case IsNaN(x) || IsNaN(y):
   126  		return NaN()
   127  	case x == 0 && x == y:
   128  		if Signbit(x) {
   129  			return y
   130  		}
   131  		return x
   132  	}
   133  	if x > y {
   134  		return x
   135  	}
   136  	return y
   137  }
   138  
   139  // Min returns the smaller of x or y.
   140  //
   141  // Special cases are:
   142  //
   143  //	Min(x, -Inf) = Min(-Inf, x) = -Inf
   144  //	Min(x, NaN) = Min(NaN, x) = NaN
   145  //	Min(-0, ±0) = Min(±0, -0) = -0
   146  func Min(x, y float32) float32 {
   147  	// special cases
   148  	switch {
   149  	case IsInf(x, -1) || IsInf(y, -1):
   150  		return Inf(-1)
   151  	case IsNaN(x) || IsNaN(y):
   152  		return NaN()
   153  	case x == 0 && x == y:
   154  		if Signbit(x) {
   155  			return x
   156  		}
   157  		return y
   158  	}
   159  	if x < y {
   160  		return x
   161  	}
   162  	return y
   163  }
   164  
   165  // NaN returns an IEEE 754 “not-a-number” value.
   166  func NaN() float32 { return math.Float32frombits(unan) }