OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of common; | 5 part of common; |
6 | 6 |
7 // Various math-related utility functions. | 7 // Various math-related utility functions. |
8 | 8 |
9 class Math2 { | 9 class Math2 { |
10 /// Computes the geometric mean of a set of numbers. | 10 /// Computes the geometric mean of a set of numbers. |
11 /// [minNumber] is optional (defaults to 0.001) anything smaller than this | 11 /// [minNumber] is optional (defaults to 0.001) anything smaller than this |
12 /// will be changed to this value, eliminating infinite results. | 12 /// will be changed to this value, eliminating infinite results. |
13 static double geometricMean(List<double> numbers, | 13 static double geometricMean(List<double> numbers, |
14 [double minNumber = 0.001]) { | 14 [double minNumber = 0.001]) { |
15 double log = 0.0; | 15 double log = 0.0; |
16 int nNumbers = 0; | 16 int nNumbers = 0; |
17 for (int i = 0, n = numbers.length; i < n; i++) { | 17 for (int i = 0, n = numbers.length; i < n; i++) { |
18 double number = numbers[i]; | 18 double number = numbers[i]; |
19 if (number < minNumber) { | 19 if (number < minNumber) { |
20 number = minNumber; | 20 number = minNumber; |
21 } | 21 } |
22 nNumbers++; | 22 nNumbers++; |
23 log += Math.log(number); | 23 log += Math.log(number); |
24 } | 24 } |
25 | 25 |
26 return nNumbers > 0 ? Math.pow(Math.E, log / nNumbers) : 0.0; | 26 return nNumbers > 0 ? Math.pow(Math.E, log / nNumbers) : 0.0; |
27 } | 27 } |
28 | 28 |
29 static int round(double d) { | 29 static int round(double d) { |
30 return d.round().toInt(); | 30 return d.round(); |
31 } | 31 } |
32 | 32 |
33 static int floor(double d) { | 33 static int floor(double d) { |
34 return d.floor().toInt(); | 34 return d.floor(); |
35 } | 35 } |
36 | 36 |
37 // TODO (olonho): use d.toStringAsFixed(precision) when implemented by DartVM | 37 // TODO (olonho): use d.toStringAsFixed(precision) when implemented by DartVM |
38 static String toStringAsFixed(num d, int precision) { | 38 static String toStringAsFixed(num d, int precision) { |
39 String dStr = d.toString(); | 39 String dStr = d.toString(); |
40 int pos = dStr.indexOf('.', 0); | 40 int pos = dStr.indexOf('.', 0); |
41 int end = pos < 0 ? dStr.length : pos + precision; | 41 int end = pos < 0 ? dStr.length : pos + precision; |
42 if (precision > 0) { | 42 if (precision > 0) { |
43 end++; | 43 end++; |
44 } | 44 } |
45 if (end > dStr.length) { | 45 if (end > dStr.length) { |
46 end = dStr.length; | 46 end = dStr.length; |
47 } | 47 } |
48 | 48 |
49 return dStr.substring(0, end); | 49 return dStr.substring(0, end); |
50 } | 50 } |
51 } | 51 } |
OLD | NEW |