Canola  0.8.D001
lib/number/z/subtraction.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/z.h>
00021 
00022 
00023 number_z &
00024 number_z::operator-=(const number_z &rhs)
00025 {
00026     assert(is_valid());
00027     assert(rhs.is_valid());
00028     // Note: if (*this < rhs) the result is undefined.
00029     int carry = 0;
00030     for (size_t n = 0; n < digits_used; ++n)
00031     {
00032         int dig = get(n) - rhs.get(n) - carry;
00033         carry = 0;
00034         while (dig < 0)
00035         {
00036             dig += 10;
00037             ++carry;
00038         }
00039         set(n, dig);
00040     }
00041     // Put into normal form, no leading zeros.
00042     while (digits_used > 0 && digits[digits_used - 1] == 0)
00043         --digits_used;
00044     assert(is_valid());
00045     return *this;
00046 }
00047 
00048 
00049 number_z
00050 number_z::operator-(const number_z &rhs)
00051     const
00052 {
00053     number_z result(*this);
00054     result -= rhs;
00055     return result;
00056 }
00057 
00058 
00059 // vim: set ts=8 sw=4 et :