$whoami Ryoya Sekino / 関野 涼也 Software Engineer at UPSIDER, inc., leading Card Processing team Writing Go for 2.5 years as a main language Spoke at Go Conference 2021 Spring as well Loves music, DJ’ing and bathhouse/sauna 1. Introduction 5
( 厳密には、値に応じて、 default type が存在して、変 数への代入時等に使用される) 具体的な変数への代入等した際に、型を持った値に変 換される( そのため、代入時にoverflow したりする) func main() { const a = 111111111111111111111 var _ = a } $ go run main.go constant 111111111111111111111 overflows int 3. Go の組み込みの小数の挙動 27
型を持たない定数の数値は任意の精度で計 算してくれる ただのふつうの数として扱われる https://go.dev/blog/constants 仕様上は精度の限界がない( 処理系に依存する) 3. Go の組み込みの小数の挙動 Numeric constants live in an arbitrary-precision numeric space; they are just regular numbers. “ “ 28
型なし定数は大きい数の定義に使える 例えば、円周率を高めの精度で定義しておける Pi = 3.14159265358979323846264338327950288419716939937510582097494459 https://pkg.go.dev/math#pkg-constants 3. Go の組み込みの小数の挙動 29
計算誤差も出ない func main() { a, _ := decimal.NewFromString("0.1") b := decimal.NewFromInt(3) c, _ := decimal.NewFromString("0.3") fmt.Printf("a * b = %v\n", a.Mul(b)) fmt.Printf("c: %v\n", c) fmt.Printf("a * b == c: %t\n", a.Mul(b).Equal(c)) } $ go run main.go a * b = 0.3 c: 0.3 a * b == c: true 4. Go の小数計算のアプローチ - 3.shopspring/decimal 44