2017-07-14 21 views
1

です。golangのfloat64とcomplex128変数の最大値を知る必要があります。 goはfloat.hと等価ではないようで、計算方法はわかりません。例えばfloat64とcomplex128タイプの最大値はGo

+0

Cヘッダーファイルについては、Goで説明したように、この情報はパッケージドキュメントに記載されています。標準ライブラリパッケージのドキュメントは、次のURLから参照できます。https://golang.org/pkg/ 特定のケースでは、数学パッケージと組み込みパッケージを調べたいと考えています。 MaxFloat64の詳細はConstantsセクションにあります。 complex128の場合、complex128宣言の上のソースコメントを読んでcomplex128の可能な最大値を理解することができます。 パッケージのドキュメントには、パッケージのソースファイルへのリンク(太字の青色のハイパーリンク)も含まれています。 – sahaj

答えて

2

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" 

浮動小数点限界VAルーズ最大値は、タイプによって表現可能な最大有限値 です。 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) 
) 

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および 2の補数演算を用いて表されています。

実装固有のサイズで事前に宣言数値型のセットもあります:ポータビリティを避けるために

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 

は、すべての数値型は のuint8の別名であるバイトと、ルーンを除く異なっている問題はこれは int32の別名です。異なる数値型が式または代入で と混在する場合は、変換が必要です。たとえば、特定の アーキテクチャで同じサイズを持つ場合でも、int32とintは同じタイプの ではありません。