Go中float64和complex128类型的最大值

时间:2017-07-14 14:15:30

标签: variables go floating-point

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

2 个答案:

答案 0 :(得分:8)

例如,

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)
}

输出:

float64 1.7976931348623157e+308
complex128 (1.7976931348623157e+308+1.7976931348623157e+308i)
  

Package math

import "math" 
     

浮点限制值。 Max是最大的有限值   按类型表示。最小的诺龙是最小的积极的,   可以通过类型表示的非零值。

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)
)
  

The Go Programming Language Specification

     

Numeric types

     

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

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
     

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
     

为避免可移植性问题,除了以外,所有数字类型都是不同的   byte,uint8的别名,以及rune,它是别名   INT32。混合使用不同的数字类型时,需要进行转换   在表达或赋值中。例如,int32和int不是   即使它们在特定的尺寸上可能具有相同的尺寸,也是相同的类型   架构。

答案 1 :(得分:0)

您还可以考虑使用class软件包中的Inf方法 返回无穷大的值(如果需要,可以返回正数或负数),但被视为math

不太确定float64math.MaxFloat64之间是否存在一个自变量。比较这两者,我发现Go会将无穷大值解释为大于最大浮点数。

math.Inf()