Topic: std::domain_error


Author: restor <akrzemi1@interia.pl>
Date: Fri, 20 Jul 2007 14:24:54 CST
Raw View
I believe that std::domain_error is underspecified. The standard only
says it is for domain errors, but it is unclear (at least for me) what
the difference between a domain error and an invalid argument is.
Should passing 0 to function Divide trigger a domain or invalid
argument error?

&rzej

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Lance Diduck <lancediduck@nyc.rr.com>
Date: Sun, 22 Jul 2007 13:17:21 CST
Raw View
On Jul 20, 4:24 pm, restor <akrze...@interia.pl> wrote:
> I believe that std::domain_error is underspecified. The standard only
> says it is for domain errors, but it is unclear (at least for me) what
> the difference between a domain error and an invalid argument is.
> Should passing 0 to function Divide trigger a domain or invalid
> argument error?
>
> &rzej
In this case it would be a domain error.
domain_error and invalid_argument take their semantics from the
standard C libraries EDOM and EINVAL. So for example I have a C
function that calculates the reciprocal, and places it in an out
argument, and returns the error code
int recip(double val, double* ret){

    int fptype=fpclass(val);
    if(fptype==FP_SNAN || fptype == FP_QNAN || ret==0)return EINVAL;
                 //preconditions of function implementation not met
    if(fptype==FP_NZERO || fptype == FP_PZERO) //val==0.0
            return EDOM; //preconditions of math function not met
    *ret=1/val;
    swtch(fpclass(*ret)){
        case FP_SINF:
        case FP_PINF:
             return ERANGE; //range limit of math function implemented
        case FP_SNAN:
        case FP_QNAN:
            return EDOM; //invalid operator
             //alternative error detection to the argument is 0 case
--
             // fpu will use a Nan for divide by 0
    }
    return 0;//success
};


Now let's change this to a format that cannot use return codes:
struct Recip{

 operator double()const{
          return m_ret;
 }
  Recip(double val){
    switch(fpclass(val)){
      case FP_SNAN: case FP_QNAN:
      throw std::invalid_argument("bad arg for Recip");
      case FP_NZERO: case FP_PZERO:
      throw std::domain_error("Recip arg cannot be 0");

    };
    m_ret=1/val;
    switch(fpclass(val)){
      case FP_SINF: case FP_PINF:
      throw std::range_error("Recip result is out of range");
    };

  }
 private:
 double m_ret;
};
I will agree that there is no clear distinction between domain and
invalid argument errors. But in general, domain errors only occur when
the arguments are not defined for the math function involved (cannot
divide by 0, cannot take square root of a negative number, probability
not between 0 and 1)  and invalid argument for al other things
(pointer is null, signal number does not exist, file handle not valid,
etc)

Hope that helps
Lance

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]