Canola  0.8.D001
lib/precision_and_rounding.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2011 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 #include <lib/precision_and_rounding.h>
00022 
00023 
00024 precision_and_rounding::~precision_and_rounding()
00025 {
00026 }
00027 
00028 
00029 precision_and_rounding::precision_and_rounding() :
00030     precision(-1),
00031     round(round_off)
00032 {
00033     assert(precision >= -1);
00034     assert(precision <= 14);
00035 }
00036 
00037 
00038 precision_and_rounding::precision_and_rounding(
00039     int a_precision,
00040     round_t a_round
00041 ) :
00042     precision(a_precision),
00043     round(a_round)
00044 {
00045 }
00046 
00047 
00048 precision_and_rounding::precision_and_rounding(
00049     const precision_and_rounding &rhs
00050 ) :
00051     precision(rhs.precision),
00052     round(rhs.round)
00053 {
00054 }
00055 
00056 
00057 precision_and_rounding &
00058 precision_and_rounding::operator=(const precision_and_rounding &rhs)
00059 {
00060     if (this != &rhs)
00061     {
00062         precision = rhs.precision;
00063         round = rhs.round;
00064     }
00065     return *this;
00066 }
00067 
00068 
00069 void
00070 precision_and_rounding::apply(number &value)
00071 {
00072     if (precision >= 0)
00073     {
00074         switch (round)
00075         {
00076         case round_up:
00077             value = value.round_up(precision);
00078             break;
00079 
00080         default:
00081             assert(!"unknown rounding");
00082         case round_off:
00083             value = value.round_off(precision);
00084             break;
00085 
00086         case round_down:
00087             value = value.round_down(precision);
00088             break;
00089         }
00090     }
00091 }