User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,759 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,461 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Dec 16th, 2006
Views: 2,330
SpS
For all functions, a domain error occurs if an input argument is outside the domain over which the mathematical function is defined. On a domain error, the function returns an implementation-defined value.
Last edited : Dec 16th, 2006.
c Syntax | 5 stars
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <math.h>
  4. #define POSITIVE 25
  5. #define NEGATIVE -25
  6.  
  7. int main()
  8. {
  9. double ret;
  10.  
  11. errno = 0;
  12. ret = sqrt(NEGATIVE);
  13. if (errno == EDOM) /*EDOM Signifies Domain Error*/
  14. printf("Domain Error : Invalid Input To Function\n");
  15. else
  16. printf("Valid Input To Function\n");
  17.  
  18. errno = 0;
  19. ret = sqrt(POSITIVE);
  20.  
  21. if (errno == EDOM)
  22. printf("Domain Error : Invalid Input To Function\n");
  23. else
  24. printf("Valid Input To Function\n");
  25.  
  26. return 0;
  27. }
  28.  
  29. /***
  30.   * Output
  31.   * Domain Error:Invalid Input To Function
  32.   * Valid Input To Function
  33. ***/
Comments (Newest First)
Dave Sinkula | long time no c | Jan 23rd, 2007
Consider also using perror to diagnose the problem.
#include <stdio.h>
#include <errno.h>
#include <math.h>

void test(double value)
{
   double ret;
   errno = 0;
   ret = sqrt(value);
   if ( errno )
   {
      perror("sqrt");
   }
   else
   {
      printf("ret = %g\n", ret);
   }
}

int main()
{
   test(-25);
   test(25);
}

/* my output
sqrt: Domain error
ret = 5
*/
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC