scalar.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright ©2013 The Gonum Authors. All rights reserved.
  2. // Use of this code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package scalar
  5. import (
  6. "math"
  7. "strconv"
  8. )
  9. // EqualWithinAbs returns true when a and b have an absolute difference
  10. // not greater than tol.
  11. func EqualWithinAbs(a, b, tol float64) bool {
  12. return a == b || math.Abs(a-b) <= tol
  13. }
  14. // minNormalFloat64 is the smallest normal number. For 64 bit IEEE-754
  15. // floats this is 2^{-1022}.
  16. const minNormalFloat64 = 0x1p-1022
  17. // EqualWithinRel returns true when the difference between a and b
  18. // is not greater than tol times the greater absolute value of a and b,
  19. //
  20. // abs(a-b) <= tol * max(abs(a), abs(b)).
  21. func EqualWithinRel(a, b, tol float64) bool {
  22. if a == b {
  23. return true
  24. }
  25. delta := math.Abs(a - b)
  26. if delta <= minNormalFloat64 {
  27. return delta <= tol*minNormalFloat64
  28. }
  29. // We depend on the division in this relationship to identify
  30. // infinities (we rely on the NaN to fail the test) otherwise
  31. // we compare Infs of the same sign and evaluate Infs as equal
  32. // independent of sign.
  33. return delta/math.Max(math.Abs(a), math.Abs(b)) <= tol
  34. }
  35. // EqualWithinAbsOrRel returns true when a and b are equal to within
  36. // the absolute or relative tolerances. See EqualWithinAbs and
  37. // EqualWithinRel for details.
  38. func EqualWithinAbsOrRel(a, b, absTol, relTol float64) bool {
  39. return EqualWithinAbs(a, b, absTol) || EqualWithinRel(a, b, relTol)
  40. }
  41. // EqualWithinULP returns true when a and b are equal to within
  42. // the specified number of floating point units in the last place.
  43. func EqualWithinULP(a, b float64, ulp uint) bool {
  44. if a == b {
  45. return true
  46. }
  47. if math.IsNaN(a) || math.IsNaN(b) {
  48. return false
  49. }
  50. if math.Signbit(a) != math.Signbit(b) {
  51. return math.Float64bits(math.Abs(a))+math.Float64bits(math.Abs(b)) <= uint64(ulp)
  52. }
  53. return ulpDiff(math.Float64bits(a), math.Float64bits(b)) <= uint64(ulp)
  54. }
  55. func ulpDiff(a, b uint64) uint64 {
  56. if a > b {
  57. return a - b
  58. }
  59. return b - a
  60. }
  61. const (
  62. nanBits = 0x7ff8000000000000
  63. nanMask = 0xfff8000000000000
  64. )
  65. // NaNWith returns an IEEE 754 "quiet not-a-number" value with the
  66. // payload specified in the low 51 bits of payload.
  67. // The NaN returned by math.NaN has a bit pattern equal to NaNWith(1).
  68. func NaNWith(payload uint64) float64 {
  69. return math.Float64frombits(nanBits | (payload &^ nanMask))
  70. }
  71. // NaNPayload returns the lowest 51 bits payload of an IEEE 754 "quiet
  72. // not-a-number". For values of f other than quiet-NaN, NaNPayload
  73. // returns zero and false.
  74. func NaNPayload(f float64) (payload uint64, ok bool) {
  75. b := math.Float64bits(f)
  76. if b&nanBits != nanBits {
  77. return 0, false
  78. }
  79. return b &^ nanMask, true
  80. }
  81. // ParseWithNA converts the string s to a float64 in value.
  82. // If s equals missing, weight is returned as 0, otherwise 1.
  83. func ParseWithNA(s, missing string) (value, weight float64, err error) {
  84. if s == missing {
  85. return 0, 0, nil
  86. }
  87. value, err = strconv.ParseFloat(s, 64)
  88. if err == nil {
  89. weight = 1
  90. }
  91. return value, weight, err
  92. }
  93. // Round returns the half away from zero rounded value of x with prec precision.
  94. //
  95. // Special cases are:
  96. //
  97. // Round(±0) = +0
  98. // Round(±Inf) = ±Inf
  99. // Round(NaN) = NaN
  100. func Round(x float64, prec int) float64 {
  101. if x == 0 {
  102. // Make sure zero is returned
  103. // without the negative bit set.
  104. return 0
  105. }
  106. // Fast path for positive precision on integers.
  107. if prec >= 0 && x == math.Trunc(x) {
  108. return x
  109. }
  110. pow := math.Pow10(prec)
  111. intermed := x * pow
  112. if math.IsInf(intermed, 0) {
  113. return x
  114. }
  115. x = math.Round(intermed)
  116. if x == 0 {
  117. return 0
  118. }
  119. return x / pow
  120. }
  121. // RoundEven returns the half even rounded value of x with prec precision.
  122. //
  123. // Special cases are:
  124. //
  125. // RoundEven(±0) = +0
  126. // RoundEven(±Inf) = ±Inf
  127. // RoundEven(NaN) = NaN
  128. func RoundEven(x float64, prec int) float64 {
  129. if x == 0 {
  130. // Make sure zero is returned
  131. // without the negative bit set.
  132. return 0
  133. }
  134. // Fast path for positive precision on integers.
  135. if prec >= 0 && x == math.Trunc(x) {
  136. return x
  137. }
  138. pow := math.Pow10(prec)
  139. intermed := x * pow
  140. if math.IsInf(intermed, 0) {
  141. return x
  142. }
  143. x = math.RoundToEven(intermed)
  144. if x == 0 {
  145. return 0
  146. }
  147. return x / pow
  148. }
  149. // Same returns true when the inputs have the same value, allowing NaN equality.
  150. func Same(a, b float64) bool {
  151. return a == b || (math.IsNaN(a) && math.IsNaN(b))
  152. }