duannan3959 2017-07-14 14:15
浏览 1600
已采纳

Go中float64和complex128类型的最大值

i need to know the maximum value of float64 and complex128 variable types in golang. go doesn't seem to have an equivalent of float.h and i don't know how to calculate it.

  • 写回答

1条回答 默认 最新

  • drebew5059 2017-07-14 14:20
    关注

    For example,

    package main
    
    import (
        "fmt"
        "math"
    )
    
    func main() {
        const f = math.MaxFloat64
        fmt.Printf("%[1]T %[1]v
    ", f)
        const c = complex(math.MaxFloat64, math.MaxFloat64)
        fmt.Printf("%[1]T %[1]v
    ", c)
    }
    

    Output:

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

    Package math

    import "math" 
    

    Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

    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

    A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

    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
    

    The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

    There is also a set of predeclared numeric types with implementation-specific sizes:

    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
    

    To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用