OLD | NEW |
(Empty) | |
| 1 dart_library.library('lib/math/pi_test', null, /* Imports */[ |
| 2 'dart_sdk', |
| 3 'expect' |
| 4 ], function load__pi_test(exports, dart_sdk, expect) { |
| 5 'use strict'; |
| 6 const core = dart_sdk.core; |
| 7 const math = dart_sdk.math; |
| 8 const dart = dart_sdk.dart; |
| 9 const dartx = dart_sdk.dartx; |
| 10 const expect$ = expect.expect; |
| 11 const pi_test = Object.create(null); |
| 12 let __Tovoid = () => (__Tovoid = dart.constFn(dart.definiteFunctionType(dart.v
oid, [], [dart.dynamic])))(); |
| 13 pi_test.known_bad_seeds = dart.constList([50051, 55597, 59208], core.int); |
| 14 pi_test.main = function(args) { |
| 15 if (args === void 0) args = null; |
| 16 let seed = -1; |
| 17 if (args != null && dart.test(dart.dsend(dart.dload(args, 'length'), '>', 0)
)) { |
| 18 seed = core.int.parse(core.String._check(dart.dindex(args, 0))); |
| 19 } else { |
| 20 let seed_prng = math.Random.new(); |
| 21 while (seed == -1) { |
| 22 seed = seed_prng.nextInt(1 << 16); |
| 23 if (dart.test(pi_test.known_bad_seeds[dartx.contains](seed))) { |
| 24 seed = -1; |
| 25 } |
| 26 } |
| 27 } |
| 28 core.print(dart.str`pi_test seed: ${seed}`); |
| 29 let prng = math.Random.new(seed); |
| 30 let outside = 0; |
| 31 let inside = 0; |
| 32 for (let i = 0; i < 600000; i++) { |
| 33 let x = prng.nextDouble(); |
| 34 let y = prng.nextDouble(); |
| 35 if (dart.notNull(x) * dart.notNull(x) + dart.notNull(y) * dart.notNull(y)
< 1.0) { |
| 36 inside++; |
| 37 } else { |
| 38 outside++; |
| 39 } |
| 40 } |
| 41 let pie = 4.0 * (inside / (inside + outside)); |
| 42 core.print(dart.str`${pie}`); |
| 43 expect$.Expect.isTrue(math.PI - 0.009 < pie && pie < math.PI + 0.009); |
| 44 }; |
| 45 dart.fn(pi_test.main, __Tovoid()); |
| 46 // Exports: |
| 47 exports.pi_test = pi_test; |
| 48 }); |
OLD | NEW |