mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 22:02:55 +01:00
Format code
This commit is contained in:
parent
a5298e13bb
commit
0ccda7ac4d
1 changed files with 18 additions and 10 deletions
|
|
@ -1,21 +1,24 @@
|
||||||
import 'dart:async';
|
|
||||||
import 'dart:math' show Random;
|
import 'dart:math' show Random;
|
||||||
main() async {
|
|
||||||
|
void main() async {
|
||||||
print('Compute π using the Monte Carlo method.');
|
print('Compute π using the Monte Carlo method.');
|
||||||
await for (var estimate in computePi().take(100)) {
|
await for (final estimate in computePi().take(100)) {
|
||||||
print('π ≅ $estimate');
|
print('π ≅ $estimate');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a stream of increasingly accurate estimates of π.
|
/// Generates a stream of increasingly accurate estimates of π.
|
||||||
Stream<double> computePi({int batch: 100000}) async* {
|
Stream<double> computePi({int batch = 100000}) async* {
|
||||||
var total = 0;
|
var total = 0; // Inferred to be of type int
|
||||||
var count = 0;
|
var count = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
var points = generateRandom().take(batch);
|
final points = generateRandom().take(batch);
|
||||||
var inside = points.where((p) => p.isInsideUnitCircle);
|
final inside = points.where((p) => p.isInsideUnitCircle);
|
||||||
|
|
||||||
total += batch;
|
total += batch;
|
||||||
count += inside.length;
|
count += inside.length;
|
||||||
var ratio = count / total;
|
final ratio = count / total;
|
||||||
|
|
||||||
// Area of a circle is A = π⋅r², therefore π = A/r².
|
// Area of a circle is A = π⋅r², therefore π = A/r².
|
||||||
// So, when given random points with x ∈ <0,1>,
|
// So, when given random points with x ∈ <0,1>,
|
||||||
// y ∈ <0,1>, the ratio of those inside a unit circle
|
// y ∈ <0,1>, the ratio of those inside a unit circle
|
||||||
|
|
@ -24,14 +27,19 @@ Stream<double> computePi({int batch: 100000}) async* {
|
||||||
yield ratio * 4;
|
yield ratio * 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Iterable<Point> generateRandom([int seed]) sync* {
|
|
||||||
|
Iterable<Point> generateRandom([int? seed]) sync* {
|
||||||
final random = Random(seed);
|
final random = Random(seed);
|
||||||
while (true) {
|
while (true) {
|
||||||
yield Point(random.nextDouble(), random.nextDouble());
|
yield Point(random.nextDouble(), random.nextDouble());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Point {
|
class Point {
|
||||||
final double x, y;
|
final double x;
|
||||||
|
final double y;
|
||||||
|
|
||||||
const Point(this.x, this.y);
|
const Point(this.x, this.y);
|
||||||
|
|
||||||
bool get isInsideUnitCircle => x * x + y * y <= 1;
|
bool get isInsideUnitCircle => x * x + y * y <= 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue