Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Beginner question about sqrt()

Reply
Thread Tools

Beginner question about sqrt()

 
 
Jeremy
Guest
Posts: n/a
 
      12-11-2005
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
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      12-11-2005
"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.


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-11-2005
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


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
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      12-11-2005
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

 
Reply With Quote
 
Guest
Posts: n/a
 
      12-11-2005
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

 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      12-11-2005
On Sun, 11 Dec 2005 20:33:37 +0100, Markus Moll
<> wrote:

> 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.
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      12-11-2005
>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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-11-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
beginner's question.. Scott Needham Wireless Networking 4 09-06-2005 12:52 AM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM
Beginner question: What trigs processes Jerker Hammarberg VHDL 16 07-22-2003 03:58 PM



Advertisments