github.com/primecitizens/pcz/std@v0.2.1/builtin/float/float.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2017 The Go 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  
     8  package stdfloat
     9  
    10  import "unsafe"
    11  
    12  var inf = Float64FromBits(0x7FF0000000000000)
    13  
    14  // IsNaN reports whether f is an IEEE 754 “not-a-number” value.
    15  func IsNaN(f float64) (is bool) {
    16  	// IEEE 754 says that only NaNs satisfy f != f.
    17  	return f != f
    18  }
    19  
    20  // IsFinite reports whether f is neither NaN nor an infinity.
    21  func IsFinite(f float64) bool {
    22  	return !IsNaN(f - f)
    23  }
    24  
    25  // IsInf reports whether f is an infinity.
    26  func IsInf(f float64) bool {
    27  	return !IsNaN(f) && !IsFinite(f)
    28  }
    29  
    30  // Abs returns the absolute value of x.
    31  //
    32  // Special cases are:
    33  //
    34  //	Abs(±Inf) = +Inf
    35  //	Abs(NaN) = NaN
    36  func Abs(x float64) float64 {
    37  	const sign = 1 << 63
    38  	return Float64FromBits(Float64Bits(x) &^ sign)
    39  }
    40  
    41  // CopySign returns a value with the magnitude
    42  // of x and the sign of y.
    43  func CopySign(x, y float64) float64 {
    44  	const sign = 1 << 63
    45  	return Float64FromBits(Float64Bits(x)&^sign | Float64Bits(y)&sign)
    46  }
    47  
    48  // Float64Bits returns the IEEE 754 binary representation of f.
    49  func Float64Bits(f float64) uint64 {
    50  	return *(*uint64)(unsafe.Pointer(&f))
    51  }
    52  
    53  // Float64FromBits returns the floating point number corresponding
    54  // the IEEE 754 binary representation b.
    55  func Float64FromBits(b uint64) float64 {
    56  	return *(*float64)(unsafe.Pointer(&b))
    57  }