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