math.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Copyright ©2015 The Gonum Authors. All rights reserved.
  5. // Use of this source code is governed by a BSD-style
  6. // license that can be found in the LICENSE file.
  7. package math32
  8. import (
  9. "math"
  10. )
  11. const (
  12. unan = 0x7fc00000
  13. uinf = 0x7f800000
  14. uneginf = 0xff800000
  15. mask = 0x7f8 >> 3
  16. shift = 32 - 8 - 1
  17. bias = 127
  18. )
  19. // Abs returns the absolute value of x.
  20. //
  21. // Special cases are:
  22. // Abs(±Inf) = +Inf
  23. // Abs(NaN) = NaN
  24. func Abs(x float32) float32 {
  25. switch {
  26. case x < 0:
  27. return -x
  28. case x == 0:
  29. return 0 // return correctly abs(-0)
  30. }
  31. return x
  32. }
  33. // Copysign returns a value with the magnitude
  34. // of x and the sign of y.
  35. func Copysign(x, y float32) float32 {
  36. const sign = 1 << 31
  37. return math.Float32frombits(math.Float32bits(x)&^sign | math.Float32bits(y)&sign)
  38. }
  39. // Hypot returns Sqrt(p*p + q*q), taking care to avoid
  40. // unnecessary overflow and underflow.
  41. //
  42. // Special cases are:
  43. // Hypot(±Inf, q) = +Inf
  44. // Hypot(p, ±Inf) = +Inf
  45. // Hypot(NaN, q) = NaN
  46. // Hypot(p, NaN) = NaN
  47. func Hypot(p, q float32) float32 {
  48. // special cases
  49. switch {
  50. case IsInf(p, 0) || IsInf(q, 0):
  51. return Inf(1)
  52. case IsNaN(p) || IsNaN(q):
  53. return NaN()
  54. }
  55. if p < 0 {
  56. p = -p
  57. }
  58. if q < 0 {
  59. q = -q
  60. }
  61. if p < q {
  62. p, q = q, p
  63. }
  64. if p == 0 {
  65. return 0
  66. }
  67. q = q / p
  68. return p * Sqrt(1+q*q)
  69. }
  70. // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
  71. func Inf(sign int) float32 {
  72. var v uint32
  73. if sign >= 0 {
  74. v = uinf
  75. } else {
  76. v = uneginf
  77. }
  78. return math.Float32frombits(v)
  79. }
  80. // IsInf reports whether f is an infinity, according to sign.
  81. // If sign > 0, IsInf reports whether f is positive infinity.
  82. // If sign < 0, IsInf reports whether f is negative infinity.
  83. // If sign == 0, IsInf reports whether f is either infinity.
  84. func IsInf(f float32, sign int) bool {
  85. // Test for infinity by comparing against maximum float.
  86. // To avoid the floating-point hardware, could use:
  87. // x := math.Float32bits(f);
  88. // return sign >= 0 && x == uinf || sign <= 0 && x == uneginf;
  89. return sign >= 0 && f > math.MaxFloat32 || sign <= 0 && f < -math.MaxFloat32
  90. }
  91. // IsNaN reports whether f is an IEEE 754 ``not-a-number'' value.
  92. func IsNaN(f float32) (is bool) {
  93. // IEEE 754 says that only NaNs satisfy f != f.
  94. // To avoid the floating-point hardware, could use:
  95. // x := math.Float32bits(f);
  96. // return uint32(x>>shift)&mask == mask && x != uinf && x != uneginf
  97. return f != f
  98. }
  99. // NaN returns an IEEE 754 ``not-a-number'' value.
  100. func NaN() float32 { return math.Float32frombits(unan) }