Thread: Function returning nan

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Function returning nan

    Hey guys. I was working on a simple program that computes an area beneath a graph, and this is what i have come up with until now:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    double area(double a, double b, double n);
    double areatriangle(double h, double w);
    double areasquare(double h, double w);
    
    double f(double x) { return (sqrt(4 - pow(x,2))); }
    
    int main(void) {
    	double a, b, n, grapharea;
    	printf("\nPlease enter a: "); scanf("%lf", &a); 
    	printf("\nPlease enter b: "); scanf("%lf", &b);
    	printf("\nPlease enter n: "); scanf("%lf", &n);
    	grapharea = area(a, b, n);
    	printf("\nArea beneath the graph is: %f", grapharea);
    }
    
    double area(double a, double b, double n) {
    	double x, sum = 0.0;
    	for (x = a; x < b; x += n) {
    		printf("\n%f", areatriangle(n, (f(x) - f(x-1))));
    		sum += areatriangle(n, (f(x) - f(x-1)));
    		sum += areasquare(n, f(x));
    		//printf("\nSum: %f X: %f",sum, x);
    	}	
    	return (sum);
    }
    
    double areatriangle(double h, double w) {
    	double area = (h * w / 2);
    	//printf("\nTriangle = W: %f, area: %f", w, area);
    	return (area);
    }
    double areasquare(double h, double w) {
    	double area = (h * w);
    	return (area);
    }
    The thing is that whenever we had to get the area from a "defined interval" (I'm not sure what its called in english), and i attempted to let you choose the interval - but we where supposed to get the answer for the bestemt interval of -2 to 2. The thing is, that as long as X is less then -1, the function areatriangle() will return nan. I know that nan means not a number, but i really can't see why it can compute as long as x > -1. Can anyone push me in the right direction ?

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    What does Math say about square roots of negative numbers, and what does documentation of srqt() say about negative arguments?
    Code:
    double f(double x) { return (sqrt(4 - pow(x,2))); }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    That I shouldn't have sleept through class Thanks alot, I can see the problem now and I'm pretty sure i can fix the problem... Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM