Source File
float.go
Belonging Package
math/big
package big
import (
)
const debugFloat = false // enable for debugging
type RoundingMode byte
const (
ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven
ToNearestAway // == IEEE 754-2008 roundTiesToAway
ToZero // == IEEE 754-2008 roundTowardZero
AwayFromZero // no IEEE 754-2008 equivalent
ToNegativeInf // == IEEE 754-2008 roundTowardNegative
ToPositiveInf // == IEEE 754-2008 roundTowardPositive
)
if == 0 {
.prec = 0
func ( *Float) () RoundingMode {
return .mode
}
.setExpAndRound(int64(.exp)+int64(), 0)
}
return
}
func ( *Float) () bool {
if debugFloat {
.validate()
func ( *Float) () {
panic("validate called but debugFloat is not set")
}
if .form != finite {
return
}
:= len(.mant)
if == 0 {
panic("nonzero finite number with empty mantissa")
}
const = 1 << (_W - 1)
if .mant[-1]& == 0 {
panic(fmt.Sprintf("msb not set in last word %#x of %s", .mant[-1], .Text('p', 0)))
}
if .prec == 0 {
panic("zero precision finite number")
}
}
return
return
if == 0 && ( == 0 || .mode == ToNearestEven) {
= .mant.sticky()
}
&= 1 // be safe and ensure it's a single bit
:= false
switch .mode {
case ToNegativeInf:
= .neg
case ToNearestEven:
= != 0 && ( != 0 || .mant[0]& != 0)
case ToNearestAway:
= != 0
case AwayFromZero:
= true
case ToPositiveInf:
= !.neg
default:
panic("unreachable")
}
return 0, Below
return 0,
const (
= 32 // float size
= 23 // mantissa size (excluding implicit msb)
= - - 1 // 8 exponent size
= 1<<(-1) - 1 // 127 exponent bias
= 1 - - // -149 smallest unbiased exponent (denormal)
= 1 - // -126 smallest unbiased exponent (normal)
= // 127 largest unbiased exponent (normal)
)
:= .exp - 1 // exponent for normal mantissa m with 1.0 <= m < 2.0
:= + 1 // precision of normal float
if == 0 {
if .neg {
return -math.SmallestNonzeroFloat32, Below
}
return math.SmallestNonzeroFloat32, Above
}
= uint32(+) <<
= msb32(.mant) >> & (1<< - 1) // cut off msb (implicit 1 bit)
}
return math.Float32frombits( | | ), .acc
case zero:
if .neg {
var float32
return -, Exact
}
return 0.0, Exact
case inf:
if .neg {
return float32(math.Inf(-1)), Exact
}
return float32(math.Inf(+1)), Exact
}
panic("unreachable")
}
const (
= 64 // float size
= 52 // mantissa size (excluding implicit msb)
= - - 1 // 11 exponent size
= 1<<(-1) - 1 // 1023 exponent bias
= 1 - - // -1074 smallest unbiased exponent (denormal)
= 1 - // -1022 smallest unbiased exponent (normal)
= // 1023 largest unbiased exponent (normal)
)
:= .exp - 1 // exponent for normal mantissa m with 1.0 <= m < 2.0
:= + 1 // precision of normal float
if == 0 {
if .neg {
return -math.SmallestNonzeroFloat64, Below
}
return math.SmallestNonzeroFloat64, Above
}
return .SetInt64(0),
if debugFloat {
validateBinaryOperands(, )
}
if debugFloat {
validateBinaryOperands(, )
}
:= int64(.exp) - int64(len(.mant))*_W
:= int64(.exp) - int64(len(.mant))*_W
:= alias(.mant, .mant) || alias(.mant, .mant)
switch {
case < :
if {
:= nat(nil).shl(.mant, uint(-))
.mant = .sub(.mant, )
} else {
.mant = .mant.shl(.mant, uint(-))
.mant = .mant.sub(.mant, .mant)
}
func ( *Float) (, *Float) {
if debugFloat {
validateBinaryOperands(, )
}
func ( *Float) (, *Float) {
if debugFloat {
validateBinaryOperands(, )
}
:= .mant
var uint
if len() > 0 {
= 1
}
.setExpAndRound(-fnorm(.mant), )
}
func ( *Float) ( *Float) int {
if debugFloat {
validateBinaryOperands(, )
}
switch {
case .exp < .exp:
return -1
case .exp > .exp:
return +1
.uadd(, )
return .Set()
}
return .Set()
}
.uadd(, )
return .Set()
}
return .Neg()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |