| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004 Decillion Pty(Ltd) |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file rounding.hpp |
| 21 | \brief Rounding implementation |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_rounding_hpp |
| 25 | #define quantlib_rounding_hpp |
| 26 | |
| 27 | #include <ql/types.hpp> |
| 28 | |
| 29 | namespace QuantLib { |
| 30 | |
| 31 | //! basic rounding class |
| 32 | /*! \test the correctness of the returned values is tested by |
| 33 | checking them against known good results. |
| 34 | */ |
| 35 | class Rounding { |
| 36 | public: |
| 37 | //! rounding methods |
| 38 | /*! The rounding methods follow the OMG specification available |
| 39 | at <http://www.omg.org/cgi-bin/doc?formal/00-06-29.pdf>. |
| 40 | |
| 41 | \warning the names of the Floor and Ceiling methods might |
| 42 | be misleading. Check the provided reference. |
| 43 | */ |
| 44 | enum Type { |
| 45 | None, /*!< do not round: return the number unmodified */ |
| 46 | Up, /*!< the first decimal place past the precision will be |
| 47 | rounded up. This differs from the OMG rule which |
| 48 | rounds up only if the decimal to be rounded is |
| 49 | greater than or equal to the rounding digit */ |
| 50 | Down, /*!< all decimal places past the precision will be |
| 51 | truncated */ |
| 52 | Closest, /*!< the first decimal place past the precision |
| 53 | will be rounded up if greater than or equal |
| 54 | to the rounding digit; this corresponds to |
| 55 | the OMG round-up rule. When the rounding |
| 56 | digit is 5, the result will be the one |
| 57 | closest to the original number, hence the |
| 58 | name. */ |
| 59 | Floor, /*!< positive numbers will be rounded up and negative |
| 60 | numbers will be rounded down using the OMG round up |
| 61 | and round down rules */ |
| 62 | Ceiling /*!< positive numbers will be rounded down and negative |
| 63 | numbers will be rounded up using the OMG round up |
| 64 | and round down rules */ |
| 65 | }; |
| 66 | //! default constructor |
| 67 | /*! Instances built through this constructor don't perform |
| 68 | any rounding. |
| 69 | */ |
| 70 | Rounding() = default; |
| 71 | explicit Rounding(Integer precision, |
| 72 | Type type = Closest, |
| 73 | Integer digit = 5) |
| 74 | : precision_(precision), type_(type), digit_(digit) {} |
| 75 | //! perform rounding |
| 76 | Decimal operator()(Decimal value) const; |
| 77 | //! \name Inspectors |
| 78 | //@{ |
| 79 | Integer precision() const { return precision_; } |
| 80 | Type type() const { return type_; } |
| 81 | Integer roundingDigit() const { return digit_; } |
| 82 | private: |
| 83 | Integer precision_; |
| 84 | Type type_ = None; |
| 85 | Integer digit_; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | //! Up-rounding. |
| 90 | class UpRounding : public Rounding { |
| 91 | public: |
| 92 | explicit UpRounding(Integer precision, |
| 93 | Integer digit = 5) |
| 94 | : Rounding(precision,Up,digit) {} |
| 95 | }; |
| 96 | |
| 97 | //! Down-rounding. |
| 98 | class DownRounding : public Rounding { |
| 99 | public: |
| 100 | explicit DownRounding(Integer precision, |
| 101 | Integer digit = 5) |
| 102 | : Rounding(precision,Down,digit) {} |
| 103 | }; |
| 104 | |
| 105 | //! Closest rounding. |
| 106 | class ClosestRounding : public Rounding { |
| 107 | public: |
| 108 | explicit ClosestRounding(Integer precision, |
| 109 | Integer digit = 5) |
| 110 | : Rounding(precision,Closest,digit) {} |
| 111 | }; |
| 112 | |
| 113 | //! Ceiling truncation. |
| 114 | class CeilingTruncation : public Rounding { |
| 115 | public: |
| 116 | explicit CeilingTruncation(Integer precision, |
| 117 | Integer digit = 5) |
| 118 | : Rounding(precision,Ceiling,digit) {} |
| 119 | }; |
| 120 | |
| 121 | //! %Floor truncation. |
| 122 | class FloorTruncation : public Rounding { |
| 123 | public: |
| 124 | explicit FloorTruncation(Integer precision, |
| 125 | Integer digit = 5) |
| 126 | : Rounding(precision,Floor,digit) {} |
| 127 | }; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | |
| 132 | #endif |
| 133 | |