# Beginner question about sqrt()

Discussion in 'C Programming' started by Jeremy, Dec 11, 2005.

1. ### JeremyGuest

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.. Ehm let me explain let's say;

score=8 mean=9.67 and deviation=-1.67

now I want to square the deviation with sqrt but that doesn't really
work anymore since the value is negative, what function could I use
from the math.h to obtain the correct squared deviation value of 2.79?

Thanks you.

Jeremy

Jeremy, Dec 11, 2005

2. ### osmiumGuest

"Jeremy" writes:

> 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.. Ehm let me explain let's say;
>
> score=8 mean=9.67 and deviation=-1.67

A score is a point sample. It makes no sense to try to compute the std
deviation of a single sample.

> now I want to square the deviation with sqrt but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

It is perfectly permissible to square a negative number, square roots are a
different matter. Note that the square of a negative number is always
positive. The equation for standard deviation will always result in taking
the square root of a *positive* number. Look more closely at the equation
you are using.

osmium, Dec 11, 2005

3. ### A. Sinan UnurGuest

Jeremy <> wrote in
news::

> 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..

Huh?

> Ehm let me explain

> score=8 mean=9.67 and deviation=-1.67
>
> now I want to square the deviation with sqrt

sqrt is for taking the square root of a number.

> but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

You seem to be confused between "standard deviation" and z-score.

Sinan
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

A. Sinan Unur, Dec 11, 2005
4. ### Markus MollGuest

Hi

Jeremy wrote:

> score=8 mean=9.67 and deviation=-1.67
>
> now I want to square the deviation with sqrt but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

Ever thought about why there is a 't' at the end of 'sqrt'? ;-)

Firstly, x squared simply is x * x.
Secondly, if that is statistics, then you're probably doing something wrong.
The standard deviation (if that's what you mean by deviation) is defined to
be the square root of the variance. An unbiased estimator for the variance
is given by
\frac{1}{n-1} \sum_{i=1}^N E((x_i - Âµ)^2)

Markus

Markus Moll, Dec 11, 2005
5. ### GuestGuest

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.. Ehm let me explain let's say;

You can compute the square of a negativ value, and you can compute
the square root of a negative value, but the square root of a
negative value can not be represented in a double. Since C
does not provide a mechanism for a functoin to return either
a "double" or a "double _Complex" depending on the sign of the argument,
an implementation of "double sqrt(double);" is limited to
arguments that are not negative.

If you really need a function that computes square roots of
negative numbers, have a look at "double complex csqrt(double complex)"
from <complex.h>. If your implementation has signed zero representations,
you might want to think about the sign of the imarinary 0 you
use to expand your negative double to an equivaent double complex.

:
: score=8 mean=9.67 and deviation=-1.67
:
: now I want to square the deviation with sqrt but that doesn't really
: work anymore since the value is negative, what function could I use
: from the math.h to obtain the correct squared deviation value of 2.79?

"deviaton * deviation" might be the best solution for you, even if
"pow(deviation, 2.0)" should work as well. The function sqrt()
does not do what you seem to think it should do.

Kurt Watzka

Guest, Dec 11, 2005
6. ### JeremyGuest

On Sun, 11 Dec 2005 20:33:37 +0100, Markus Moll

> Hi
>
> Jeremy wrote:
>
> > score=8 mean=9.67 and deviation=-1.67
> >
> > now I want to square the deviation with sqrt but that doesn't really
> > work anymore since the value is negative, what function could I use
> > from the math.h to obtain the correct squared deviation value of 2.79?

>
> Ever thought about why there is a 't' at the end of 'sqrt'? ;-)
>
> Firstly, x squared simply is x * x.
> Secondly, if that is statistics, then you're probably doing something wrong.
> The standard deviation (if that's what you mean by deviation) is defined to
> be the square root of the variance. An unbiased estimator for the variance
> is given by
> \frac{1}{n-1} \sum_{i=1}^N E((x_i - µ)^2)
>
> Markus

I think this is the language barriere kicking in with a lack of
education and a beginner hobby programmer....

I fixed the differences or mixup inbetween square and sqrt and it's
working ! Halleluja..

Thanks you.

Jeremy, Dec 11, 2005
7. ### Gordon BurdittGuest

>I am trying to calculate the standard deviation with my program, but I
>ran into a small problem with squaring negative value's.

Make up your mind what you want to do: SQUARE a value or TAKE THE
SQUARE ROOT of a value. They are different, in both mathematics
and C.

> Which cannot
>be done.. Ehm let me explain let's say;
>
>score=8 mean=9.67 and deviation=-1.67
>
>now I want to square the deviation with sqrt but that doesn't really

Do not attempt to square anything with sqrt(). It's like
constructing a skyscraper with explosives.

>work anymore since the value is negative, what function could I use
>from the math.h to obtain the correct squared deviation value of 2.79?

If the correct answer is 2.79, apparently you are trying to SQUARE
a value, and sqrt() is not the way to do it.

Gordon L. Burditt

Gordon Burditt, Dec 11, 2005
8. ### Martin AmbuhlGuest

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.

Martin Ambuhl, Dec 11, 2005