github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/compile/internal/gc/testdata/sqrt_const.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "math" 10 ) 11 12 var tests = [...]struct { 13 name string 14 in float64 // used for error messages, not an input 15 got float64 16 want float64 17 }{ 18 {"sqrt0", 0, math.Sqrt(0), 0}, 19 {"sqrt1", 1, math.Sqrt(1), 1}, 20 {"sqrt2", 2, math.Sqrt(2), math.Sqrt2}, 21 {"sqrt4", 4, math.Sqrt(4), 2}, 22 {"sqrt100", 100, math.Sqrt(100), 10}, 23 {"sqrt101", 101, math.Sqrt(101), 10.04987562112089}, 24 } 25 26 var nanTests = [...]struct { 27 name string 28 in float64 // used for error messages, not an input 29 got float64 30 }{ 31 {"sqrtNaN", math.NaN(), math.Sqrt(math.NaN())}, 32 {"sqrtNegative", -1, math.Sqrt(-1)}, 33 {"sqrtNegInf", math.Inf(-1), math.Sqrt(math.Inf(-1))}, 34 } 35 36 var failed = false 37 38 func main() { 39 for _, test := range tests { 40 if test.got != test.want { 41 fmt.Printf("%s: math.Sqrt(%f): got %f, want %f\n", test.name, test.in, test.got, test.want) 42 failed = true 43 } 44 } 45 for _, test := range nanTests { 46 if math.IsNaN(test.got) != true { 47 fmt.Printf("%s: math.Sqrt(%f): got %f, want NaN\n", test.name, test.in, test.got) 48 failed = true 49 } 50 } 51 if got := math.Sqrt(math.Inf(1)); !math.IsInf(got, 1) { 52 fmt.Printf("math.Sqrt(+Inf), got %f, want +Inf\n", got) 53 failed = true 54 } 55 56 if failed { 57 panic("failed") 58 } 59 }