英文:
The maximum value for float64 and complex128 type in Go
问题
在Go语言中,float64和complex128变量类型的最大值如下:
- float64的最大值是math.MaxFloat64。
- complex128的最大值是math.MaxFloat64 + math.MaxFloat64i。
请注意,这些值是根据Go语言标准库中的math包提供的常量计算得出的。
英文:
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
得分: 33
例如,
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 是该类型可表示的最大有限值。SmallestNonzero 是该类型可表示的最小正非零值。
>
> const (
> MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2127 * (224 - 1) / 223
> SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2(127 - 1 + 23)
>
> MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 21023 * (253 - 1) / 252
> SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2(1023 - 1 + 52)
> )
> Go 编程语言规范
>
> 数值类型
>
> 数值类型表示整数或浮点数值的集合。
> 预声明的与体系结构无关的数值类型有:
>
> uint8 所有无符号 8 位整数的集合(0 到 255)
> uint16 所有无符号 16 位整数的集合(0 到 65535)
> uint32 所有无符号 32 位整数的集合(0 到 4294967295)
> uint64 所有无符号 64 位整数的集合(0 到 18446744073709551615)
>
> int8 所有有符号 8 位整数的集合(-128 到 127)
> int16 所有有符号 16 位整数的集合(-32768 到 32767)
> int32 所有有符号 32 位整数的集合(-2147483648 到 2147483647)
> int64 所有有符号 64 位整数的集合(-9223372036854775808 到 9223372036854775807)
>
> float32 所有 IEEE-754 32 位浮点数的集合
> float64 所有 IEEE-754 64 位浮点数的集合
>
> complex64 所有具有 float32 实部和虚部的复数的集合
> complex128 所有具有 float64 实部和虚部的复数的集合
>
> byte uint8 的别名
> rune int32 的别名
>
> n 位整数的值宽度为 n 位,并使用二进制补码算术表示。
>
> 还有一组具有实现特定大小的预声明数值类型:
>
> uint 32 位或 64 位
> int 与 uint 大小相同
> uintptr 一个足够大的无符号整数,用于存储指针值的未解释位
>
> 为了避免可移植性问题,所有数值类型都是不同的,除了 byte 是 uint8 的别名,rune 是 int32 的别名。当不同的数值类型在表达式或赋值中混合使用时,需要进行转换。例如,int32 和 int 虽然在特定体系结构上可能具有相同的大小,但它们不是相同的类型。
英文:
For example,
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)
}
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 // 2127 * (224 - 1) / 223
> SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2(127 - 1 + 23)
>
> MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 21023 * (253 - 1) / 252
> 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.
答案2
得分: 3
你还可以考虑使用math
包中的Inf方法,它返回一个表示无穷大的值(正无穷大或负无穷大),但被认为是float64
类型。
不太确定在math.MaxFloat64
和math.Inf()
之间是否有一个更好的选择。通过比较这两个值,我发现Go将无穷大的值视为大于最大浮点数。
package main
import (
"fmt"
"math"
)
func main() {
infPos := math.Inf(1) // 表示正无穷大
fmt.Printf("%[1]T %[1]v\n", infPos)
infNeg := math.Inf(-1) // 表示负无穷大
fmt.Printf("%[1]T %[1]v\n", infNeg)
}
英文:
You can also consider using the Inf method from the math
package which
returns a value for infinity (positive or negative if you want), but is considered to be float64
.
Not too sure if there is an argument for one or the other between math.MaxFloat64
and math.Inf()
. Comparing the two I've found that Go interprets the infinity values to be larger than the max float ones.
package main
import (
"fmt"
"math"
)
func main() {
infPos := math.Inf(1) // gives positive infinity
fmt.Printf("%[1]T %[1]v\n", infPos)
infNeg := math.Inf(-1) // gives negative infinity
fmt.Printf("%[1]T %[1]v\n", infNeg)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论