github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/float_lit.go (about) 1 // run 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test floating-point literal syntax. 8 9 package main 10 11 var bad bool 12 13 func pow10(pow int) float64 { 14 if pow < 0 { 15 return 1 / pow10(-pow) 16 } 17 if pow > 0 { 18 return pow10(pow-1) * 10 19 } 20 return 1 21 } 22 23 func close(da float64, ia, ib int64, pow int) bool { 24 db := float64(ia) / float64(ib) 25 db *= pow10(pow) 26 27 if da == 0 || db == 0 { 28 if da == 0 && db == 0 { 29 return true 30 } 31 return false 32 } 33 34 de := (da - db) / da 35 if de < 0 { 36 de = -de 37 } 38 39 if de < 1e-14 { 40 return true 41 } 42 if !bad { 43 println("BUG") 44 bad = true 45 } 46 return false 47 } 48 49 func main() { 50 if !close(0., 0, 1, 0) { 51 print("0. is ", 0., "\n") 52 } 53 if !close(+10., 10, 1, 0) { 54 print("+10. is ", +10., "\n") 55 } 56 if !close(-210., -210, 1, 0) { 57 print("-210. is ", -210., "\n") 58 } 59 60 if !close(.0, 0, 1, 0) { 61 print(".0 is ", .0, "\n") 62 } 63 if !close(+.01, 1, 100, 0) { 64 print("+.01 is ", +.01, "\n") 65 } 66 if !close(-.012, -12, 1000, 0) { 67 print("-.012 is ", -.012, "\n") 68 } 69 70 if !close(0.0, 0, 1, 0) { 71 print("0.0 is ", 0.0, "\n") 72 } 73 if !close(+10.01, 1001, 100, 0) { 74 print("+10.01 is ", +10.01, "\n") 75 } 76 if !close(-210.012, -210012, 1000, 0) { 77 print("-210.012 is ", -210.012, "\n") 78 } 79 80 if !close(0E+1, 0, 1, 0) { 81 print("0E+1 is ", 0E+1, "\n") 82 } 83 if !close(+10e2, 10, 1, 2) { 84 print("+10e2 is ", +10e2, "\n") 85 } 86 if !close(-210e3, -210, 1, 3) { 87 print("-210e3 is ", -210e3, "\n") 88 } 89 90 if !close(0E-1, 0, 1, 0) { 91 print("0E-1 is ", 0E-1, "\n") 92 } 93 if !close(+0e23, 0, 1, 1) { 94 print("+0e23 is ", +0e23, "\n") 95 } 96 if !close(-0e345, 0, 1, 1) { 97 print("-0e345 is ", -0e345, "\n") 98 } 99 100 if !close(0E1, 0, 1, 1) { 101 print("0E1 is ", 0E1, "\n") 102 } 103 if !close(+10e23, 10, 1, 23) { 104 print("+10e23 is ", +10e23, "\n") 105 } 106 if !close(-210e34, -210, 1, 34) { 107 print("-210e34 is ", -210e34, "\n") 108 } 109 110 if !close(0.E1, 0, 1, 1) { 111 print("0.E1 is ", 0.E1, "\n") 112 } 113 if !close(+10.e+2, 10, 1, 2) { 114 print("+10.e+2 is ", +10.e+2, "\n") 115 } 116 if !close(-210.e-3, -210, 1, -3) { 117 print("-210.e-3 is ", -210.e-3, "\n") 118 } 119 120 if !close(.0E1, 0, 1, 1) { 121 print(".0E1 is ", .0E1, "\n") 122 } 123 if !close(+.01e2, 1, 100, 2) { 124 print("+.01e2 is ", +.01e2, "\n") 125 } 126 if !close(-.012e3, -12, 1000, 3) { 127 print("-.012e3 is ", -.012e3, "\n") 128 } 129 130 if !close(0.0E1, 0, 1, 0) { 131 print("0.0E1 is ", 0.0E1, "\n") 132 } 133 if !close(+10.01e2, 1001, 100, 2) { 134 print("+10.01e2 is ", +10.01e2, "\n") 135 } 136 if !close(-210.012e3, -210012, 1000, 3) { 137 print("-210.012e3 is ", -210.012e3, "\n") 138 } 139 140 if !close(0.E+12, 0, 1, 0) { 141 print("0.E+12 is ", 0.E+12, "\n") 142 } 143 if !close(+10.e23, 10, 1, 23) { 144 print("+10.e23 is ", +10.e23, "\n") 145 } 146 if !close(-210.e33, -210, 1, 33) { 147 print("-210.e33 is ", -210.e33, "\n") 148 } 149 150 if !close(.0E-12, 0, 1, 0) { 151 print(".0E-12 is ", .0E-12, "\n") 152 } 153 if !close(+.01e23, 1, 100, 23) { 154 print("+.01e23 is ", +.01e23, "\n") 155 } 156 if !close(-.012e34, -12, 1000, 34) { 157 print("-.012e34 is ", -.012e34, "\n") 158 } 159 160 if !close(0.0E12, 0, 1, 12) { 161 print("0.0E12 is ", 0.0E12, "\n") 162 } 163 if !close(+10.01e23, 1001, 100, 23) { 164 print("+10.01e23 is ", +10.01e23, "\n") 165 } 166 if !close(-210.012e33, -210012, 1000, 33) { 167 print("-210.012e33 is ", -210.012e33, "\n") 168 } 169 170 if !close(0.E123, 0, 1, 123) { 171 print("0.E123 is ", 0.E123, "\n") 172 } 173 if !close(+10.e+23, 10, 1, 23) { 174 print("+10.e+234 is ", +10.e+234, "\n") 175 } 176 if !close(-210.e-35, -210, 1, -35) { 177 print("-210.e-35 is ", -210.e-35, "\n") 178 } 179 180 if !close(.0E123, 0, 1, 123) { 181 print(".0E123 is ", .0E123, "\n") 182 } 183 if !close(+.01e29, 1, 100, 29) { 184 print("+.01e29 is ", +.01e29, "\n") 185 } 186 if !close(-.012e29, -12, 1000, 29) { 187 print("-.012e29 is ", -.012e29, "\n") 188 } 189 190 if !close(0.0E123, 0, 1, 123) { 191 print("0.0E123 is ", 0.0E123, "\n") 192 } 193 if !close(+10.01e31, 1001, 100, 31) { 194 print("+10.01e31 is ", +10.01e31, "\n") 195 } 196 if !close(-210.012e19, -210012, 1000, 19) { 197 print("-210.012e19 is ", -210.012e19, "\n") 198 } 199 200 if bad { 201 panic("float_lit") 202 } 203 }