Go中float64和complex128类型的最大值

moh*_*tux 6 variables floating-point go

我需要知道golang中float64和complex128变量类型的最大值.go似乎没有float.h的等价物,我不知道如何计算它.

pet*_*rSO 10

例如,

package main

import (
    "fmt"
    "math"
)

func main() {
    const f = math.MaxFloat64
    fmt.Printf("%[1]T %[1]v\n", f)
    const c = complex(math.MaxFloat64, math.MaxFloat64)
    fmt.Printf("%[1]T %[1]v\n", c)
}
Run Code Online (Sandbox Code Playgroud)

输出:

float64 1.7976931348623157e+308
complex128 (1.7976931348623157e+308+1.7976931348623157e+308i)
Run Code Online (Sandbox Code Playgroud)

包数学

import "math" 
Run Code Online (Sandbox Code Playgroud)

浮点限制值.Max是该类型可表示的最大有限值.SmallestNonzero是该类型可表示的最小正,非零值.

const (
        MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
        SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

        MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
        SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
Run Code Online (Sandbox Code Playgroud)

Go编程语言规范

数字类型

数字类型表示整数或浮点值的集合.预先声明的与体系结构无关的数字类型是:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32
Run Code Online (Sandbox Code Playgroud)

n位整数的值是n位宽,并使用二进制补码算法表示.

还有一组具有特定于实现的大小的预先声明的数字类型:

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value
Run Code Online (Sandbox Code Playgroud)

为避免可移植性问题,除了byte(uint8的别名)和rune(int32的别名)之外,所有数字类型都是不同的.在表达式或赋值中混合使用不同的数字类型时,需要进行转换.例如,int32和int的类型不同,即使它们在特定体系结构上可能具有相同的大小.