Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 1 | // $G $F.go && $L $F.$A && ./$A.out |
| 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 | package main |
| 8 | |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 9 | func |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 10 | pow10(pow int) float64 { |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 11 | if pow < 0 { return 1/pow10(-pow); } |
| 12 | if pow > 0 { return pow10(pow-1)*10; } |
| 13 | return 1; |
| 14 | } |
| 15 | |
| 16 | func |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 17 | close(da float64, ia, ib int64, pow int) bool { |
Russ Cox | 9cdb8bd | 2008-10-29 13:58:12 -0700 | [diff] [blame] | 18 | db := float64(ia) / float64(ib); |
Ken Thompson | 343f5aa | 2008-06-12 21:48:56 -0700 | [diff] [blame] | 19 | db *= pow10(pow); |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 20 | |
Russ Cox | ef46a9d | 2009-11-15 17:24:14 -0800 | [diff] [blame] | 21 | if da == 0 || db == 0 { |
| 22 | if da == 0 && db == 0 { |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 23 | return true; |
| 24 | } |
| 25 | return false; |
| 26 | } |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 27 | |
Ken Thompson | 343f5aa | 2008-06-12 21:48:56 -0700 | [diff] [blame] | 28 | de := (da-db) /da; |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 29 | if de < 0 { |
| 30 | de = -de; |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 31 | } |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 32 | |
Ken Thompson | 343f5aa | 2008-06-12 21:48:56 -0700 | [diff] [blame] | 33 | if de < 1.0e-14 { |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 34 | return true; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
Ken Thompson | b1a3463 | 2008-06-10 13:23:19 -0700 | [diff] [blame] | 39 | func |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 40 | main() { |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 41 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 42 | if !close(0., 0, 1, 0) { print("0. is ", 0., "\n"); } |
| 43 | if !close(+10., 10, 1, 0) { print("+10. is ", +10., "\n"); } |
| 44 | if !close(-210., -210, 1, 0) { print("-210. is ", -210., "\n"); } |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 45 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 46 | if !close(.0, 0, 1, 0) { print(".0 is ", .0, "\n"); } |
| 47 | if !close(+.01, 1, 100, 0) { print("+.01 is ", +.01, "\n"); } |
| 48 | if !close(-.012, -12, 1000, 0) { print("-.012 is ", -.012, "\n"); } |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 49 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 50 | if !close(0.0, 0, 1, 0) { print("0.0 is ", 0.0, "\n"); } |
| 51 | if !close(+10.01, 1001, 100, 0) { print("+10.01 is ", +10.01, "\n"); } |
| 52 | if !close(-210.012, -210012, 1000, 0) { print("-210.012 is ", -210.012, "\n"); } |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 53 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 54 | if !close(0E+1, 0, 1, 0) { print("0E+1 is ", 0E+1, "\n"); } |
| 55 | if !close(+10e2, 10, 1, 2) { print("+10e2 is ", +10e2, "\n"); } |
| 56 | if !close(-210e3, -210, 1, 3) { print("-210e3 is ", -210e3, "\n"); } |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 57 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 58 | if !close(0E-1, 0, 1, 0) { print("0E-1 is ", 0E-1, "\n"); } |
Russ Cox | ef46a9d | 2009-11-15 17:24:14 -0800 | [diff] [blame] | 59 | if !close(+0e23, 0, 1, 1) { print("+0e23 is ", +0e23, "\n"); } |
| 60 | if !close(-0e345, 0, 1, 1) { print("-0e345 is ", -0e345, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 61 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 62 | if !close(0E1, 0, 1, 1) { print("0E1 is ", 0E1, "\n"); } |
| 63 | if !close(+10e23, 10, 1, 23) { print("+10e23 is ", +10e23, "\n"); } |
| 64 | if !close(-210e34, -210, 1, 34) { print("-210e34 is ", -210e34, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 65 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 66 | if !close(0.E1, 0, 1, 1) { print("0.E1 is ", 0.E1, "\n"); } |
| 67 | if !close(+10.e+2, 10, 1, 2) { print("+10.e+2 is ", +10.e+2, "\n"); } |
| 68 | if !close(-210.e-3, -210, 1, -3) { print("-210.e-3 is ", -210.e-3, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 69 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 70 | if !close(.0E1, 0, 1, 1) { print(".0E1 is ", .0E1, "\n"); } |
| 71 | if !close(+.01e2, 1, 100, 2) { print("+.01e2 is ", +.01e2, "\n"); } |
| 72 | if !close(-.012e3, -12, 1000, 3) { print("-.012e3 is ", -.012e3, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 73 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 74 | if !close(0.0E1, 0, 1, 0) { print("0.0E1 is ", 0.0E1, "\n"); } |
| 75 | if !close(+10.01e2, 1001, 100, 2) { print("+10.01e2 is ", +10.01e2, "\n"); } |
| 76 | if !close(-210.012e3, -210012, 1000, 3) { print("-210.012e3 is ", -210.012e3, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 77 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 78 | if !close(0.E+12, 0, 1, 0) { print("0.E+12 is ", 0.E+12, "\n"); } |
| 79 | if !close(+10.e23, 10, 1, 23) { print("+10.e23 is ", +10.e23, "\n"); } |
| 80 | if !close(-210.e33, -210, 1, 33) { print("-210.e33 is ", -210.e33, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 81 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 82 | if !close(.0E-12, 0, 1, 0) { print(".0E-12 is ", .0E-12, "\n"); } |
| 83 | if !close(+.01e23, 1, 100, 23) { print("+.01e23 is ", +.01e23, "\n"); } |
| 84 | if !close(-.012e34, -12, 1000, 34) { print("-.012e34 is ", -.012e34, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 85 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 86 | if !close(0.0E12, 0, 1, 12) { print("0.0E12 is ", 0.0E12, "\n"); } |
| 87 | if !close(+10.01e23, 1001, 100, 23) { print("+10.01e23 is ", +10.01e23, "\n"); } |
| 88 | if !close(-210.012e33, -210012, 1000, 33) { print("-210.012e33 is ", -210.012e33, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 89 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 90 | if !close(0.E123, 0, 1, 123) { print("0.E123 is ", 0.E123, "\n"); } |
| 91 | if !close(+10.e+23, 10, 1, 23) { print("+10.e+234 is ", +10.e+234, "\n"); } |
| 92 | if !close(-210.e-35, -210, 1, -35) { print("-210.e-35 is ", -210.e-35, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 93 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 94 | if !close(.0E123, 0, 1, 123) { print(".0E123 is ", .0E123, "\n"); } |
| 95 | if !close(+.01e29, 1, 100, 29) { print("+.01e29 is ", +.01e29, "\n"); } |
| 96 | if !close(-.012e29, -12, 1000, 29) { print("-.012e29 is ", -.012e29, "\n"); } |
Ken Thompson | ad073b1 | 2008-06-08 16:16:17 -0700 | [diff] [blame] | 97 | |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 98 | if !close(0.0E123, 0, 1, 123) { print("0.0E123 is ", 0.0E123, "\n"); } |
| 99 | if !close(+10.01e31, 1001, 100, 31) { print("+10.01e31 is ", +10.01e31, "\n"); } |
| 100 | if !close(-210.012e19, -210012, 1000, 19) { print("-210.012e19 is ", -210.012e19, "\n"); } |
Robert Griesemer | d2490e7 | 2008-03-19 15:45:07 -0700 | [diff] [blame] | 101 | } |