Beginner question about sqrt()

J

Jeremy

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
 
O

osmium

Jeremy said:
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.
 
A

A. Sinan Unur

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

Please do.
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
 
M

Markus Moll

Hi
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
 
G

Guest

Jeremy ([email protected]) 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
 
J

Jeremy

Hi


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

Gordon Burditt

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
 
M

Martin Ambuhl

Jeremy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top