int/float/double overflow / underflow

int/float/double overflow / underflow

Post by NCHfrea » Thu, 24 Aug 2000 10:30:51



 Hello

        Does anyone know how to detect for int/float/double overflow and
underflow in c++ (without resort to using a larger representation)?

--
Best regards
FROM NCHFREAK

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

int/float/double overflow / underflow

Post by M DOUBE » Thu, 24 Aug 2000 04:00:00


In don't think it is standardized, but if you are under UNIX, you can
have look at ieeefp.h.

NAME
     fpgetround, fpsetround, fpgetmask,  fpsetmask,  fpgetsticky,
     fpsetsticky - IEEE floating-point environment control

DESCRIPTION
     There are five floating-point exceptions:

        o  divide-by-zero,

        o  overflow,

        o  underflow,

        o  imprecise (inexact) result, and
When a floating-point exception  occurs,  the  corresponding
     sticky  bit  is set (1), and if the mask bit is enabled (1),
     the trap takes place. These routines let the user change the
     behavior  on  occurrence of any of these exceptions, as well
     as change the rounding mode for floating-point operations.


>  Hello

>         Does anyone know how to detect for int/float/double overflow and
> underflow in c++ (without resort to using a larger representation)?

--
                                         Michael DOUBEZ

 
 
 

int/float/double overflow / underflow

Post by M DOUBE » Thu, 24 Aug 2000 04:00:00


Yes, ieeefp.h is under SunOS. Sorry.

Perhaps, the function  finite() of math.h would be more
appropriate for double.

For int, you could define:

pair<int,bool> mySum(int x,int y)
{
 int val=x+y;
 bool overunderflow=((x<=0)&&(y<=0)&&(x>=MIN_INT-y)) ||
((x>=0)&&(y>=0)&&(x<=MAX_INT-y));

 return pair<int,bool>(val,overunderflow);

Quote:}

or throw an exception if overunderflow is true.

However :
"The C++ programming language defines the action taken on underflow or
overflow to be machine-dependent, which means it is up to to compiler
and the target computer to determine
how to handle overflow and underflow. Most C++ compilers leave the
detection up to the programmer--that is, the compiler acts as though
underflow and overflow never occur.
This approach makes sense from a perspective of efficiency; if code has
to be generated to check for underflow or overflow whenever an
arithmetic operation is performed, then
programs would not only be larger, they would run more slowly, a very
undesirable situation given that arithmetic overflow seldom occurs in
programs that work with numbers
well within the defined range for integers."
--
                                         Michael DOUBEZ

 
 
 

int/float/double overflow / underflow

Post by James Mo » Thu, 24 Aug 2000 04:00:00



>  Hello

>         Does anyone know how to detect for int/float/double overflow and
> underflow in c++ (without resort to using a larger representation)?

    In general int's do not generate under-/overflow errors, although on
some CPUs it is possible to set options that will do so. If you need to
know, you have to check the int value yourself.
    Floating point is also platform dependent. If the platform confoorms
to IEEE floating point standards, you are supposed to get exceptions for
a variety of problems: divide by zero, invalid format, under-/overflow,
not-a-number (nan), etc. These are normally signals raised by the
system, not something that is C++.

--
sma at sohnen-moe dot com

 
 
 

int/float/double overflow / underflow

Post by Ron Natali » Thu, 24 Aug 2000 04:00:00



> In don't think it is standardized, but if you are under UNIX, you can
> have look at ieeefp.h.

If you are on a SUN running UNIX...
 
 
 

int/float/double overflow / underflow

Post by Edwar » Fri, 25 Aug 2000 02:08:26



Quote:> In don't think it is standardized, but if you are under UNIX, you can
> have look at ieeefp.h.

its defined by the standard in the exception class hierarchy.

class exception

class runtime_error : public exception

class overflow_error : public runtime_error

class undeflow_error : public runtime_error

you may throw the exception of that type and catch the exception.

I haven't really done any operations that involves overflow/undeflow but you
can make a research based on this info and hopefully you can solve your
problem.

Good luck,
Edward