Canola
0.8.D001
|
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 bool 00024 number_z::div_mod(const number_z &numerator, const number_z &denominator, 00025 number_z "ient, number_z &remainder) 00026 { 00027 assert(numerator.is_valid()); 00028 assert(denominator.is_valid()); 00029 if (denominator.is_zero()) 00030 { 00031 quotient.clear(); 00032 remainder = numerator; 00033 return false; 00034 } 00035 if (denominator.is_one()) 00036 { 00037 quotient = numerator; 00038 remainder.clear(); 00039 return true; 00040 } 00041 if (numerator.is_zero()) 00042 { 00043 quotient.clear(); 00044 remainder.clear(); 00045 return true; 00046 } 00047 00048 quotient.clear(); 00049 remainder = numerator; 00050 number_z over(1u); 00051 number_z under(denominator); 00052 while (under < remainder) 00053 { 00054 under = under.shift_left(1); 00055 over = over.shift_left(1); 00056 } 00057 for (;;) 00058 { 00059 over = over.shift_right(1); 00060 if (over.is_zero()) 00061 break; 00062 under = under.shift_right(1); 00063 while (under <= remainder) 00064 { 00065 remainder -= under; 00066 quotient += over; 00067 } 00068 } 00069 return true; 00070 } 00071 00072 00073 // vim: set ts=8 sw=4 et :