QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
rounding.cpp
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
24#include <ql/math/rounding.hpp>
25#include <ql/errors.hpp>
26
27namespace QuantLib {
28
30
31 if (type_ == None)
32 return value;
33
34 Real mult = std::pow(10.0,precision_);
35 bool neg = (value < 0.0);
36 Real lvalue = std::fabs(value)*mult;
37 Real integral = 0.0;
38 Real modVal = std::modf(lvalue,&integral);
39 lvalue -= modVal;
40 switch (type_) {
41 case Down:
42 break;
43 case Up:
44 if (modVal != 0.0)
45 lvalue += 1.0;
46 break;
47 case Closest:
48 if (modVal >= (digit_/10.0))
49 lvalue += 1.0;
50 break;
51 case Floor:
52 if (!neg) {
53 if (modVal >= (digit_/10.0))
54 lvalue += 1.0;
55 }
56 break;
57 case Ceiling:
58 if (neg) {
59 if (modVal >= (digit_/10.0))
60 lvalue += 1.0;
61 }
62 break;
63 default:
64 QL_FAIL("unknown rounding method");
65 }
66 return (neg) ? Real(-(lvalue / mult)) : Real(lvalue / mult);
67 }
68
69}
Integer precision_
Definition: rounding.hpp:83
Decimal operator()(Decimal value) const
perform rounding
Definition: rounding.cpp:29
QL_REAL Real
real number
Definition: types.hpp:50
Real Decimal
decimal number
Definition: types.hpp:54
Definition: any.hpp:35