Thread: Error Using Math Functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Error Using Math Functions

    I am trying to get the standard deviations in my array and the funtions pow() and sqrt() won't work because "error more than one instance of overloaded function pow". Same error with the sqrt.

    I've researched it and it has something to do with using integers in the statement. If I caste them as doubles then they work but the results aren't accurate.

    If anybody knows how to work around this problems, it would be extremely helpful.

    Here is the code I have written so far:

    Code:
    void standard (int array[], int numItems, int average)
    {
        int i = 0, j, n;
        int sum = 0, variance; 
        int hold [1500];
    
        for (i = 0; i < numItems; i++)
        {
            hold[i] = array[i];
        }
    
        i = 0;
        n = numItems;
    
        for (j = 0; j < numItems; j++)
        {
            hold[i] = pow((hold[i] - average),2);
            sum += hold[i];
            i++;
        }
        
        variance = sum / (n - 1);
    
        printf ("\nThe standard deviation is %i\n", sqrt(variance));
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should include the declarations for both lib-functions in your code, put
    Code:
    #include <math.h>
    at the begin of your sourcefile.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    I do have that declaration I just didn't include it in the code I pasted

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Post the full example code.
    Make sure, that you use a C compiler not a C++ compiler.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    I actually was able to figure it out

    If I caste the pow as an int and then the hold[i] as a double it works perfect.

    Thanks anyways

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by skmightymouse View Post
    I actually was able to figure it out

    If I caste the pow as an int and then the hold[i] as a double it works perfect.

    Thanks anyways
    I can't believe that.
    Code:
    printf ("\nThe standard deviation is %i\n", sqrt(variance));
    will print bullsh... and I would listen to BillyTKid and use a c-compiler to compile c-code.
    Kurt

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    typecasting is one way to do it, albeit it is not necessary to use floating point (or functions from <math.h>) in your code at all (except when computing the standard deviation).

    Code:
       int temp = hold[i] - average;
         hold[i] = temp * temp;
    will achieve the same effect.

    For that matter, your code may be made much simpler by not introducing hold as an array.

    I'm surprised your code doesn't have problems when printing out sqrt(variance).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Sorry, I forgot to mention that I did the same thing with the sqrt() function. Yes this works and I calculated on my calculator to make sure:

    Code:
    hold[i] = (int) pow(((double)hold[i] - average),2);
    Same with the sqrt

    Code:
        printf ("\nThe standard deviation is %i\n", (int)sqrt((double)variance));
    Last edited by skmightymouse; 05-05-2012 at 10:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trig Functions without math.h
    By daltore in forum C Programming
    Replies: 13
    Last Post: 12-29-2008, 04:47 PM
  2. how to use math functions in g++
    By mukeshshah in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2008, 12:12 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Functions
    By glider_pilot123 in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:22 PM
  5. math functions
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2002, 06:14 PM