CErrorCalc
A quick programming tutorial

using namespace CErrorCalc;

typedef unsigned int UINT;

Table of Content

  1. Introduction
  2. Getting Started
    1. Discarding Values - The Max Error Delta
    2. The Average Deviation Delta
    3. The Standard Deviation Delta
    4. Other Values
  3. Feedback

Introduction

The CErrorCalc class is a very simple tool designed to simplify the evaluation of data. It can be used to easily calculate the average value, average deviation delta and standard deviation delta of any given data set. Such values are often used in physics when evaluating measurement data. This was the original reason why CErrorCalc was written, but it's applications don't end on scientific purposes. The standard deviation delta is often used by stock market analysts. There are many uses of this class I have never anticipated and probaly many that I even don't know of.

Getting Started

First untar the CErrorCalc classe's files into a directory and add them to your project. In order to use CErrorCalc you must include the "errorcalc.h" header. It will include the following:

#include <stdio.h>
#include <cmath>
#include <list>

There are three constructors available for the CErrorCalc class. They all use some sort of input to load the values the class will later evaluate. These inputs are: an open file handle, a const char* or an array of float values. All the constructors are described in detail in the documentation.

initialising from an open file handle:

#include "errorcalc.h"

void foo(const char *p)
{

[...]

FILE *hFile = fopen(p, "r");

CErrorCalc calc(hFile);

[...]

}

foo("test1.data");
initialising from a const char pointer:

#include "errorcalc.h"

void foo(const char *p)
{

[...]

CErrorCalc calc(p);

[...]

}

foo("213,444,233,215,128");
initialising from an array of floats:

#include "errorcalc.h"

#define array_size(a) (sizeof(a) / sizeof(a[0]))

void foo(float **data, UINT size)
{

[...]

CErrorCalc calc(data, size);

[...]

}

f[] = { 223, 233.6, 233.2, 333 };

foo(f, array_size(f));

Discarding Values - The Max Error Delta

When evaluating any sort of measurement data you must take into accounting any instrumental errors that might occour. This is done by calculating a value known as the max error delta (which we will call med from now on) and discarding all values out of the range (avg - med; avg + med) (I will refer to this a the max error delta range from now on). med is calculated as follows:

xMax - maximum value in the data set
xMin - minimum value in the data set

med = (xMax - xMin) / 2

By default CErrorCalc will discard all values out of the max error delta range. If you want these values to be taken into accounting you will have to instruct the CErrorCalc object to do so. This is done by calling the void SetOptions(ErrorCalcOptions options); function. Refer to the documentation for details. May this example clear things out:

CErrorCalc calc(hFile);

CErrorCalc::ErrorCalcOptions options;
options.discard = false;

calc.SetOptions(options);

The Average Deviation Delta

This value is computed based on the equation:

(|avg - x(1)| + |avg - x(2)| + ... + |avg - x(n -1)| + |avg - x(n)|) / n

Use the function float getErrorDelta(); to retrieve it.

The Standard Deviation Delta

This value is computed based on the equation:

sqrt(((avg - x(1))^2 + (avg - x(2))^2 + ... + (avg - x(n-1))^2 + (avg - x(n))^2) / n)

Use the function float getErrorStd(); to retrieve it.

Other Values

Besides the two most important values mentioned above there are also a couple of other values that can be retrieved from a CErrorCalc object. These include the average value of the measurement, the number of discarded values and the total number of values.

In order to find out how many values have been discarded, because they where out of the max error delta range use the following function:

UINT getDiscarded();

To get the total number of values that the CErrorCalc object holds use the following:

UINT getTotal();

If you want to know the average value of the data use:

float getAverage();

Feedback

If you have any questions or something in this tutorial is unclear please email me.


"We are the people our parents warned us about."

Last update: Wednesday, 11th October, 2023
Copyright © 2001-2024 by Lukasz Tomicki