Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / file / FNumericFunctions.cxx
blob99710645fc51b6d87c65879cc04ee1ae2e5574d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <cmath>
22 #include "file/FNumericFunctions.hxx"
23 #include <rtl/math.hxx>
25 using namespace connectivity;
26 using namespace connectivity::file;
28 ORowSetValue OOp_Abs::operate(const ORowSetValue& lhs) const
30 if ( lhs.isNull() )
31 return lhs;
33 double nVal(lhs);
34 if ( nVal < 0 )
35 nVal *= -1.0;
36 return fabs(nVal);
39 ORowSetValue OOp_Sign::operate(const ORowSetValue& lhs) const
41 if ( lhs.isNull() )
42 return lhs;
44 sal_Int32 nRet = 0;
45 double nVal(lhs);
46 if ( nVal < 0 )
47 nRet = -1;
48 else if ( nVal > 0 )
49 nRet = 1;
51 return nRet;
54 ORowSetValue OOp_Mod::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
56 if ( lhs.isNull() || rhs.isNull() )
57 return ORowSetValue();
59 return fmod((double)lhs,(double)rhs);
62 ORowSetValue OOp_Floor::operate(const ORowSetValue& lhs) const
64 if ( lhs.isNull() )
65 return lhs;
67 return floor((double)lhs);
70 ORowSetValue OOp_Ceiling::operate(const ORowSetValue& lhs) const
72 if ( lhs.isNull() )
73 return lhs;
75 double nVal(lhs);
76 return ceil(nVal);
79 ORowSetValue OOp_Round::operate(const ::std::vector<ORowSetValue>& lhs) const
81 if ( lhs.empty() || lhs.size() > 2 )
82 return ORowSetValue();
84 size_t nSize = lhs.size();
85 double nVal = lhs[nSize-1];
87 sal_Int32 nDec = 0;
88 if ( nSize == 2 && !lhs[0].isNull() )
89 nDec = lhs[0];
90 return ::rtl::math::round(nVal,nDec);
93 ORowSetValue OOp_Exp::operate(const ORowSetValue& lhs) const
95 if ( lhs.isNull() )
96 return lhs;
98 double nVal(lhs);
99 return exp(nVal);
102 ORowSetValue OOp_Ln::operate(const ORowSetValue& lhs) const
104 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
105 return lhs;
107 double nVal(lhs);
108 nVal = log(nVal);
109 if ( rtl::math::isNan(nVal) )
110 return ORowSetValue();
111 return nVal;
114 ORowSetValue OOp_Log::operate(const ::std::vector<ORowSetValue>& lhs) const
116 if ( lhs.empty() || lhs.size() > 2 )
117 return ORowSetValue();
118 size_t nSize = lhs.size();
119 double nVal = log( (double)lhs[nSize-1] );
122 if ( nSize == 2 && !lhs[0].isNull() )
123 nVal /= log((double)lhs[0]);
125 if ( rtl::math::isNan(nVal) )
126 return ORowSetValue();
127 return nVal;
130 ORowSetValue OOp_Log10::operate(const ORowSetValue& lhs) const
132 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
133 return lhs;
135 double nVal = log((double)lhs);
136 if ( rtl::math::isNan(nVal) )
137 return ORowSetValue();
138 nVal /= log(10.0);
139 return nVal;
142 ORowSetValue OOp_Pow::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
144 if ( lhs.isNull() || rhs.isNull() )
145 return lhs;
147 return pow((double)lhs,(double)rhs);
150 ORowSetValue OOp_Sqrt::operate(const ORowSetValue& lhs) const
152 if ( lhs.isNull() )
153 return lhs;
155 double nVal = sqrt((double)lhs);
156 if ( rtl::math::isNan(nVal) )
157 return ORowSetValue();
158 return nVal;
161 ORowSetValue OOp_Pi::operate(const ::std::vector<ORowSetValue>& /*lhs*/) const
163 return 3.141592653589793116;
166 ORowSetValue OOp_Cos::operate(const ORowSetValue& lhs) const
168 if ( lhs.isNull() )
169 return lhs;
171 return cos((double)lhs);
174 ORowSetValue OOp_Sin::operate(const ORowSetValue& lhs) const
176 if ( lhs.isNull() )
177 return lhs;
179 return sin((double)lhs);
182 ORowSetValue OOp_Tan::operate(const ORowSetValue& lhs) const
184 if ( lhs.isNull() )
185 return lhs;
187 return tan((double)lhs);
190 ORowSetValue OOp_ACos::operate(const ORowSetValue& lhs) const
192 if ( lhs.isNull() )
193 return lhs;
195 return acos((double)lhs);
198 ORowSetValue OOp_ASin::operate(const ORowSetValue& lhs) const
200 if ( lhs.isNull() )
201 return lhs;
203 return asin((double)lhs);
206 ORowSetValue OOp_ATan::operate(const ORowSetValue& lhs) const
208 if ( lhs.isNull() )
209 return lhs;
211 return atan((double)lhs);
214 ORowSetValue OOp_ATan2::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
216 if ( lhs.isNull() || rhs.isNull() )
217 return lhs;
219 return atan2((double)lhs,(double)rhs);
222 ORowSetValue OOp_Degrees::operate(const ORowSetValue& lhs) const
224 if ( lhs.isNull() )
225 return lhs;
227 double nLhs = lhs;
228 return nLhs*180*(1.0/3.141592653589793116);
231 ORowSetValue OOp_Radians::operate(const ORowSetValue& lhs) const
233 if ( lhs.isNull() )
234 return lhs;
236 double nLhs = lhs;
237 return nLhs*3.141592653589793116*(1.0/180.0);
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */