From: richardgraham Date: Wed, 12 Sep 2012 07:28:57 +0000 (+0000) Subject: Fixed problem when integrating & averaging arrays containing NaN's X-Git-Tag: upstream/12.09^2~7 X-Git-Url: https://git.gag.com/?p=debian%2Fopenrocket;a=commitdiff_plain;h=7f3ff15f6bbeeb8eae236cc3a111bcc6c60fd441 Fixed problem when integrating & averaging arrays containing NaN's git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@1019 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/core/src/net/sf/openrocket/util/ArrayUtils.java b/core/src/net/sf/openrocket/util/ArrayUtils.java index 7d373096..d0293940 100644 --- a/core/src/net/sf/openrocket/util/ArrayUtils.java +++ b/core/src/net/sf/openrocket/util/ArrayUtils.java @@ -32,7 +32,9 @@ public class ArrayUtils { public static double mean(double[] vals){ double subtotal = 0; for (int i = 0; i < vals.length; i++ ){ - subtotal += vals[i]; + if (!Double.isNaN(vals[i])){ + subtotal += vals[i]; + } } subtotal = subtotal / vals.length; return subtotal; @@ -68,8 +70,10 @@ public class ArrayUtils { double sumsq = 0.0; double temp = 0; for (int i = 0; i < vals.length; i++){ - temp = (mu - vals[i]); - sumsq += temp*temp; + if (!Double.isNaN(vals[i])){ + temp = (mu - vals[i]); + sumsq += temp*temp; + } } return sumsq / (vals.length); } @@ -92,7 +96,7 @@ public class ArrayUtils { /** * Returns the integral of a given array calculated by the trapezoidal rule - * dt is the time step between each array value + * dt is the time step between each array value. Any NaN values are treated as zero */ public static double trapz(double[] y, double dt){ double stop = (y.length -1) * dt; @@ -103,7 +107,10 @@ public class ArrayUtils { double sum = 0.0; for (int i = 1; i < x.length; i++) { - sum += (x[i] - x[i-1]) * (y[i] + y[i-1]); + double temp = (x[i] - x[i-1]) * (y[i] + y[i-1]); + if (!Double.isNaN(temp)){ + sum += temp; + } } return sum * 0.5; }