summaryrefslogtreecommitdiff
path: root/src/tool/moneycount/main.cpp
diff options
context:
space:
mode:
authorChuck Miller <shadowmil@gmail.com>2010-06-21 13:12:27 -0400
committerChuck Miller <shadowmil@gmail.com>2010-06-21 13:16:56 -0400
commit3697be69b0dffc185c9b4cf8221c782f48546771 (patch)
treed7446a76f046de6d1e1d0239ebccf9d4ba8f5b39 /src/tool/moneycount/main.cpp
parent14655f336082a85eaafaae8f54f9a5d9a2c6f061 (diff)
downloadtmwa-3697be69b0dffc185c9b4cf8221c782f48546771.tar.gz
tmwa-3697be69b0dffc185c9b4cf8221c782f48546771.tar.bz2
tmwa-3697be69b0dffc185c9b4cf8221c782f48546771.tar.xz
tmwa-3697be69b0dffc185c9b4cf8221c782f48546771.zip
Use the stlplus::inf class in the money count to avoid overflow
Also changed the degree of freedom to 0
Diffstat (limited to 'src/tool/moneycount/main.cpp')
-rw-r--r--src/tool/moneycount/main.cpp42
1 files changed, 29 insertions, 13 deletions
diff --git a/src/tool/moneycount/main.cpp b/src/tool/moneycount/main.cpp
index 0e090bd..d15f223 100644
--- a/src/tool/moneycount/main.cpp
+++ b/src/tool/moneycount/main.cpp
@@ -9,6 +9,7 @@
#include "mmo.h"
#include "athena_text.h"
+#include "inf.hpp"
#define ATHENA_FILE "save/athena.txt"
#define ACCREG_FILE "save/accreg.txt"
@@ -79,13 +80,26 @@ void countAccReg()
fp.close();
}
-long long stdDevTotal = 0;
-long long sum = 0;
-int mean = 0;
+stlplus::inf stdDevTotal(0);
+stlplus::inf sum(0);
+stlplus::inf mean(0);
bool lessthan (int i,int j) { return (i<j); }
-void findstddev(int i) { stdDevTotal += (i - mean) * (i - mean); }
-void findSum(int i) { sum += i; }
+void findstddev(int i) { stdDevTotal += stlplus::inf((stlplus::inf(i) - mean) * (stlplus::inf(i) - mean)); }
+void findSum(int i) { sum += stlplus::inf(i); }
+
+stlplus::inf infsqrt(stlplus::inf &x)
+{
+ stlplus::inf old(x);
+ stlplus::inf newv(x / stlplus::inf(2));
+ while (old - newv > stlplus::inf(1))
+ {
+ old = newv;
+ newv = old - (old * old - x) / (stlplus::inf(2) * old);
+ }
+ return newv;
+}
+
void showStats()
{
@@ -97,14 +111,14 @@ void showStats()
std::sort(values.begin(), values.end(), lessthan);
std::for_each(values.begin(), values.end(), findSum);
- long long total = sum;
- int count = values.size();
- mean = total / count;
+ stlplus::inf total(sum);
+ stlplus::inf count(values.size());
+ stlplus::inf mean(total / count);
std::for_each(values.begin(), values.end(), findstddev);
- int a4th = count / 4;
- int a10th = count / 10;
+ int a4th = stlplus::inf(count / stlplus::inf(4)).to_int();
+ int a10th = stlplus::inf(count / stlplus::inf(10)).to_int();
int lower = values[0],
@@ -126,13 +140,15 @@ void showStats()
t8 = values[a10th * 8],
t9 = values[a10th * 9],
- upper = values[count - 1];
+ upper = values[count.to_int() - 1];
+
+ stlplus::inf variance(stdDevTotal / count);
std::cout << "Sum = " << total
<< "\nCount = " << count
<< "\nMean = " << mean
- << "\nSimple Variance = " << (stdDevTotal / (count - 1))
- << "\nStandard Deviation = " << std::sqrt(stdDevTotal / (count - 1))
+ << "\nSimple Variance = " << variance
+ << "\nStandard Deviation = " << infsqrt(variance)
<< "\nLower bound = " << lower
<< "\n10th Percentile = " << t1
<< "\n20th Percentile = " << t2