Canola  0.8.D001
lib/number/positive_minus_positive.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, version 3, as
00007 // published by the Free Software Foundation.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program. If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include <lib/ac/assert.h>
00019 
00020 #include <lib/number.h>
00021 
00022 
00023 number
00024 number::positive_minus_positive(const number &lhs, const number &rhs)
00025 {
00026     assert(!lhs.negative);
00027     assert(!rhs.negative);
00028 
00029     // short circuit the easy case
00030     if (rhs.value.is_zero())
00031         return lhs;
00032 
00033     // Note: overflow cannot occur, because the abolute value of the result
00034     // will always be less than max(t1, t2), and since neither t1 nor t2 are
00035     // overflow, and both are positive, no overflow can result.
00036 
00037     unsigned ndec = max(lhs.decimal, rhs.decimal);
00038     number_z t1(lhs.value);
00039     if (ndec > lhs.decimal)
00040         t1 = t1.shift_left(ndec - lhs.decimal);
00041     number_z t2(rhs.value);
00042     if (ndec > rhs.decimal)
00043         t2 = t2.shift_left(ndec - rhs.decimal);
00044     if (t1 >= t2)
00045     {
00046         number_z t3 = t1 - t2;
00047         truncate_floating_operation(t3, ndec);
00048         return number(t3, ndec, false);
00049     }
00050     else
00051     {
00052         number_z t3 = t2 - t1;
00053         truncate_floating_operation(t3, ndec);
00054         return number(t3, ndec, true);
00055     }
00056 }
00057 
00058 
00059 // vim: set ts=8 sw=4 et :