C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
util.hpp
00001 /*
00002 **  CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
00003 **
00004 **  Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
00005 **                          Universitaet Karlsruhe, Germany
00006 **            (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
00007 **                          Universitaet Wuppertal, Germany   
00008 **
00009 **  This library is free software; you can redistribute it and/or
00010 **  modify it under the terms of the GNU Library General Public
00011 **  License as published by the Free Software Foundation; either
00012 **  version 2 of the License, or (at your option) any later version.
00013 **
00014 **  This library is distributed in the hope that it will be useful,
00015 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 **  Library General Public License for more details.
00018 **
00019 **  You should have received a copy of the GNU Library General Public
00020 **  License along with this library; if not, write to the Free
00021 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 /* CVS $Id: util.hpp,v 1.15 2014/01/30 17:23:49 cxsc Exp $ */
00025 
00026 #ifndef _CXSC_UTIL_HPP_INCLUDED
00027 #define _CXSC_UTIL_HPP_INCLUDED
00028 
00029 #include <iostream>
00030 
00031 namespace cxsc {
00032 inline int add_int(int a,int b)
00033 // Calculating a+b; abortion by int overflow.
00034 {
00035    int res(a+b);
00036    bool bl1(a>0 && b>0 && res<a),
00037         bl2(a<0 && b<0 && res>a);
00038    if (bl1||bl2)
00039    {
00040        std::cerr << "ERROR in int add_int(int,int): Overflow!" << std::endl;
00041        exit(1);  
00042    }
00043    return res;
00044 }
00045 
00046 inline int sub_int(int a,int b)
00047 // Calculating a-b; abortion by int overflow.
00048 {
00049    if (b<0 && -b<0)
00050    { 
00051        std::cerr << "ERROR in int sub_int(int,int): Overflow!" << std::endl;
00052        exit(1);
00053    }
00054    return add_int(a,-b);
00055 }
00056 
00057 } // namespace cxsc 
00058 
00059 #endif // _CXSC_UTIL_HPP_INCLUDED