Overflow, underflow, and exceptions

Overflow, underflow, and exceptions

Post by Scott Meyer » Fri, 15 Jan 1999 04:00:00



Is an implementation allowed to throw an exception in the case of underflow
or overflow of built-in types?  Chapter 5 paragraph 5 suggests yes:

  If during the evaluation of an expression, the result is not mathemat-
  ically  defined  or  not  in the range of representable values for its
  type, the behavior is undefined, unless such an expression is  a  con-
  stant  expression  (_expr.const_),  in  which case the program is ill-
  formed.  [Note: most existing implementations of  C++  ignore  integer
  overflows.  Treatment of division by zero, forming a remainder using a
  zero divisor, and all floating point exceptions vary  among  machines,
  and is usually adjustable by a library function.  ]

Hence,

  int i = std::numeric_limits<int>::max();  // set i to the maximum int value

  i++;      // results are undefined, hence an implementation could choose
            // to throw an exception

If my understanding is correct, what is the situation in C?  Is the
behavior of the following also undefined in C?

  int i = INT_MAX;
  i++;

Thanks,

Scott

Scott Meyers, Ph.D.                  Voice: 503/638-6028
Author:  Effective C++ CD            Fax:   503/638-6614

         More Effective C++          WWW:   http://www.aristeia.com/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

Overflow, underflow, and exceptions

Post by Bill Seymou » Fri, 15 Jan 1999 04:00:00



> Is the behavior of the following also undefined in C?

>   int i = INT_MAX;
>   i++;

Yes, for signed arithmetic, overflow is undefined and so
implementations are permitted to trap.  This is not the
case with unsigned arithmetic which never overflows,
but rather just wraps around modulo Umumble_MAX + 1.
I assume that this behavior was inherited by C++.

--Bill Seymour

  Programmer/Analyst, U.S. Postal Service

  Vocabulary Representative, J11 - Editor, C9X Rationale -
  Member, U.S. Delegation to SC22 WG14

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

1. Q: how to detect a overflow (or underflow) floating point calculation?

Hi,

I want to detect an overflow (or underfolow) floating point
calculation.  Do you know how?

I figure out a way below but it just partly works (only for
SW_ZERODIVIDE events). In the code below, d2 is apparently overflow.
But ui gets a value of zero instead of _SW_OVERFLOW (0x00000004) when
I debug the code.  If I change d1 to 0., then ui is _SW_ZERODIVIDE
(0x00000008) as desired.  Do you know why? Thanks for any advise.
Tony

_clearfp();
double d1 = 100.;
double d2 = DBL_MAX * d1;
unsigned int ui = _statusfp();
if (ui > 1)
{
  // handle the error
  .....

2. Sun450 vs. Ultra 2 for numerical uses

3. int/float/double overflow / underflow

4. CERCI MicroStation DWG/DXF translators per Win98

5. Requestors/Map Drive

6. Overloading Overflow and Underflow in StreamBuff

7. Getting New DSL Service

8. overflow/underflow

9. Throwing exception from derived streambuf::underflow ...

10. streambuf::underflow dislikes exceptions

11. Unhandled exception filter not called for stack overflow