C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
util.hpp
1 /*
2 ** CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
3 **
4 ** Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
5 ** Universitaet Karlsruhe, Germany
6 ** (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
7 ** Universitaet Wuppertal, Germany
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /* CVS $Id: util.hpp,v 1.15 2014/01/30 17:23:49 cxsc Exp $ */
25 
26 #ifndef _CXSC_UTIL_HPP_INCLUDED
27 #define _CXSC_UTIL_HPP_INCLUDED
28 
29 #include <iostream>
30 
31 namespace cxsc {
32 inline int add_int(int a,int b)
33 // Calculating a+b; abortion by int overflow.
34 {
35  int res(a+b);
36  bool bl1(a>0 && b>0 && res<a),
37  bl2(a<0 && b<0 && res>a);
38  if (bl1||bl2)
39  {
40  std::cerr << "ERROR in int add_int(int,int): Overflow!" << std::endl;
41  exit(1);
42  }
43  return res;
44 }
45 
46 inline int sub_int(int a,int b)
47 // Calculating a-b; abortion by int overflow.
48 {
49  if (b<0 && -b<0)
50  {
51  std::cerr << "ERROR in int sub_int(int,int): Overflow!" << std::endl;
52  exit(1);
53  }
54  return add_int(a,-b);
55 }
56 
57 } // namespace cxsc
58 
59 #endif // _CXSC_UTIL_HPP_INCLUDED
cxsc
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29