1

Somethings I should say: this is the first time I make a question in here. Usually, when I'm with a doubt about something, I found it answered somewhere (including I found a lot of answers in this website). But this time, I'm not finding an answer, so if there was, I didn't find and I'm sorry for making a question that has already been made (I know you guys don't like it, but I promise that I've searched).

I came out with this doubt by helping to sove another person's doubt. Well, I'm not sure how to say this in English, but I believe it is: "standard deviation" (Standard Deviation on Wikipedia). That's what the program is about.

A person came with a question how to do this, it wasn't working... I didn't know the formula to calculate the standard deviation, but he gave to me. But the one he gave was wrong. I'll show the code of how the program is: PasteBin of My Standard Deviation Code

It seems to be working now with this way I did, but I'm not sure. I gave this solution to the person who asked my help. Is the program right?

But my real question is not if the program is right. There is this part on the code:

sum += pow(v[i] - m,2);

When he gave me the wrong formula it was:

sum += v[i] - m;

Can you guys compile that wrong code? Depending on the numbers that you put, the output is -1.#J. Why's that? What does this mean?

#include <stdio.h>
#include <math.h>

int main (void)
{
    int n = 10, i;
    double d, m = 0.0f, sum = 0.0f, v[10];

    for (i = 0; i < n; i++)
    {
        printf ("Inform a real number: ");
        scanf ("%lf",&v[i]);
        m += v[i];
    }

    m /= n;

    for (i = 0; i < n; i++)
        sum += pow(v[i] - m,2);

    d = sqrt (sum/(n-1));

    printf ("The standard deviation of vector v is = %.2lf\n\n",d);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I removed the [tag:c++] tag from this [tag:c] question. – johnsyweb Dec 01 '13 at 06:27
  • 4
    What inputs cause the weird -1#J output? – John Saxton Dec 01 '13 at 06:27
  • 1
    What is your input? Put it here: http://ideone.com/YCkLcU – Preet Sangha Dec 01 '13 at 06:29
  • Please remove (or heavily edit) the struck-out parts of the question; they are noise that do not help us or you. The sum of the deviations from the mean should be zero. Mathematically, they are zero; computationally, the difference will be small, and might be positive or negative. At a guess, the wrong code ended up with a small negative number, and `sqrt()` objected and gave a peculiar result. You should validate your inputs (echo the input data), and the intermediate results. The chances are if you print out the sum, it will be negative. – Jonathan Leffler Dec 01 '13 at 07:30
  • Which IDE are you using? Visual Studio or something else? Also could you give us that numbers which gives you this output? On my compiler it is running fine. – haccks Dec 01 '13 at 07:41
  • Possible duplicate of [What does floating point error -1.#J mean?](http://stackoverflow.com/questions/840081/what-does-floating-point-error-1-j-mean) – phuclv Apr 11 '16 at 16:12

3 Answers3

1

1.#J is Microsoft's strange way of displaying infinity. This is a special floating-point value that results from dividing by zero or from numeric overflow.

I can't figure out why you're getting it, though. What inputs are you entering that give you the strange output?

dan04
  • 87,747
  • 23
  • 163
  • 198
  • That's helpful to know. Who on earth knows why they do such confusing things? A mere `inf` would do as well... – glglgl Dec 01 '13 at 08:59
  • Hi there! Thanks for the answers! There are multiple values I can enter on the input and that happens. I put random numbers like: 1233, 412412, 412, 12312, 123, 123123, 12, 1231, 13213, 12331 and -1.#J appears! But it seems to be the Microsoft's strang way of displaying infinity, right dan04? Thank you guys! – BlueSky Light Programmer Dec 01 '13 at 15:00
0

m /= n;this is the average

d = sqrt (sum/(n-1));but why this place use n -1,I think it should be n

now why the output is -1.#j: This is because the float you input is not right(I mean your input is not a float),example you input ad,I think the output is random if the input is wrong.

sundq
  • 735
  • 2
  • 9
  • 28
  • 1
    The difference between dividing by `n` and `n-1` is the difference between the population and sample standard deviation. The sample deviation is probably the better choice here. – Jonathan Leffler Dec 01 '13 at 07:19
0

If you are not compiling your program in C99/11 mode then the program's behavior is undefined, you can get anything. A double type data is printed by using %f format specifier.

printf ("The standard deviation of vector v is = %.2lf\n\n",d);  
                                                     ^
                                                     | Wrong specifier  

7.21.6 Formatted input/output functions:

If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
Also change

double d, m = 0.0f, sum = 0.0f, v[10];  

line to

double d, m = 0.0, sum = 0.0, v[10];   

m and sum are of double type, no need to put f for forcing that it is a float type.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    C11 and C99 allow `%lf`; if the user is on Windows with MSVC, they don't have C99 and they're stuck with C89, and this could be a problem. (ISO/IEC 9899:1999 §7.19.6.1 The `fprintf` function: _`l` (ell) Specifies that a following [...]; or has no effect on a following `a`, `A`, `e`, `E`, `f`, `F`, `g`, or `G` conversion specifier._) – Jonathan Leffler Dec 01 '13 at 07:33
  • @JonathanLeffler; I forgot that C99 allow `%lf` in `printf` for `double`. – haccks Dec 01 '13 at 07:59