Canola  0.8.D001
lib/number/truncate_floating_operation.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 //
00024 // Instruction Manual: p. 21
00025 //
00026 // "If the number of digits in the result exceeds 16, the leftmost
00027 // digit priority system functions and excess digits at the right are
00028 // dropped."
00029 //
00030 // I.e. intermediate results round down.
00031 //
00032 #undef ROUND_OFF
00033 
00034 
00035 void
00036 number::truncate_floating_operation(number_z &t3, unsigned &t3ndec)
00037 {
00038     size_t t3dc = t3.get_digit_count();
00039     if (t3dc > 16 || t3ndec > 15)
00040     {
00041         //
00042         // Truncate decimal places to the right of the decimal point.
00043         //
00044         // Instruction Manual, p. 21:
00045         //
00046         // "If the number of digits in the result exceeds 16, the
00047         // leftmost digit priority system functions and excess digits at
00048         // the right are dropped."
00049         //
00050         size_t dump_this_many = 0;
00051         if (t3dc > 16)
00052         {
00053             dump_this_many = t3dc - 16;
00054             if (dump_this_many > t3ndec)
00055                 dump_this_many = t3ndec;
00056         }
00057         if (t3ndec - dump_this_many > 15)
00058             dump_this_many = t3ndec - 15;
00059 #ifdef ROUND_OFF
00060         bool nudge = (dump_this_many > 0) && (t3.get(dump_this_many - 1) >= 5);
00061 #endif
00062         t3ndec -= dump_this_many;
00063         t3 = t3.shift_right(dump_this_many);
00064 #ifdef ROUND_OFF
00065         if (nudge)
00066             t3 += number_z::one();
00067 #endif
00068     }
00069 }
00070 
00071 
00072 // vim: set ts=8 sw=4 et :