QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
rounding.hpp
Go to the documentation of this file.
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
29namespace 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;
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:
86 };
87
88
89 //! Up-rounding.
90 class UpRounding : public Rounding {
91 public:
93 Integer digit = 5)
94 : Rounding(precision,Up,digit) {}
95 };
96
97 //! Down-rounding.
98 class DownRounding : public Rounding {
99 public:
101 Integer digit = 5)
102 : Rounding(precision,Down,digit) {}
103 };
104
105 //! Closest rounding.
106 class ClosestRounding : public Rounding {
107 public:
109 Integer digit = 5)
110 : Rounding(precision,Closest,digit) {}
111 };
112
113 //! Ceiling truncation.
115 public:
117 Integer digit = 5)
118 : Rounding(precision,Ceiling,digit) {}
119 };
120
121 //! %Floor truncation.
122 class FloorTruncation : public Rounding {
123 public:
125 Integer digit = 5)
126 : Rounding(precision,Floor,digit) {}
127 };
128
129}
130
131
132#endif
Ceiling truncation.
Definition: rounding.hpp:114
CeilingTruncation(Integer precision, Integer digit=5)
Definition: rounding.hpp:116
Closest rounding.
Definition: rounding.hpp:106
ClosestRounding(Integer precision, Integer digit=5)
Definition: rounding.hpp:108
Down-rounding.
Definition: rounding.hpp:98
DownRounding(Integer precision, Integer digit=5)
Definition: rounding.hpp:100
Concrete floor class.
Definition: capfloor.hpp:118
Floor truncation.
Definition: rounding.hpp:122
FloorTruncation(Integer precision, Integer digit=5)
Definition: rounding.hpp:124
basic rounding class
Definition: rounding.hpp:35
Type
rounding methods
Definition: rounding.hpp:44
Integer precision_
Definition: rounding.hpp:83
Rounding(Integer precision, Type type=Closest, Integer digit=5)
Definition: rounding.hpp:71
Decimal operator()(Decimal value) const
perform rounding
Definition: rounding.cpp:29
Integer precision() const
Definition: rounding.hpp:79
Rounding()=default
default constructor
Integer roundingDigit() const
Definition: rounding.hpp:81
Type type() const
Definition: rounding.hpp:80
Up-rounding.
Definition: rounding.hpp:90
UpRounding(Integer precision, Integer digit=5)
Definition: rounding.hpp:92
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Decimal
decimal number
Definition: types.hpp:54
Definition: any.hpp:35
Custom types.