EDOM: Domain Error

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
SpS SpS is offline Offline Dec 16th, 2006, 4:52 am |
0
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.
Quick reply to this message  
C Syntax
  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. ***/
0
Dave Sinkula Dave Sinkula is offline Offline | Jan 24th, 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
*/
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC