Jeremy wrote:

hello,

I am trying to calculate the standard deviation with my program, but I

ran into a small problem with squaring negative value's. Which cannot

be done..

Of course one can square negative values; it's taking their square roots

in that's a problem in the domain of real numbers.

Ehm let me explain let's say;

score=8 mean=9.67 and deviation=-1.67

This never happens. A sample variance is

V = sum(x*x)/n - (sum(x)/n)*(sum(x)/n)

and the estimate of the population variance

is V' = V * n /(n-1)

Those numbers for n > 1 and each x real are both always positive, and

the corresponding standard deviations are the positive square roots of

those variances. Neither the variance nor the standard deviations are

ever negative.

now I want to square the deviation with sqrt

sqrt(x) yields the square root of x, not the square of x.

To square x, just multiply it by itself. If you tire of writing x*x

(where x is long or complicated) you can use a function or macro:

inline double sqr(double x) { return x*x; }

- or -

#define SQR(x) ((x)*(x))

but that doesn't really

work anymore since the value is negative,

You have made an error: the standard deviation is never negative for

measurements with real values.

what function could I use

from the math.h to obtain the correct squared deviation value of 2.79?

See above.

#include <stdio.h>

#include <float.h>

int main(void)

{

double deviation = 1.67, variance;

variance = deviation * deviation;

printf("[Output:]\n"

"If the standard deviation is %.*g,\n"

"then the variance is %.*g.\n",

DBL_DIG, deviation, DBL_DIG, variance);

return 0;

}

[Output:]

If the standard deviation is 1.67,

then the variance is 2.7889.