1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <cmath>
22 #include <file/FNumericFunctions.hxx>
23 #include <rtl/math.hxx>
24 
25 using namespace connectivity;
26 using namespace connectivity::file;
27 
28 const double fPi = 3.14159265358979323846;
29 
operate(const ORowSetValue & lhs) const30 ORowSetValue OOp_Abs::operate(const ORowSetValue& lhs) const
31 {
32     if ( lhs.isNull() )
33         return lhs;
34 
35     double nVal(lhs);
36     if ( nVal < 0 )
37         nVal *= -1.0;
38     return fabs(nVal);
39 }
40 
operate(const ORowSetValue & lhs) const41 ORowSetValue OOp_Sign::operate(const ORowSetValue& lhs) const
42 {
43     if ( lhs.isNull() )
44         return lhs;
45 
46     sal_Int32 nRet = 0;
47     double nVal(lhs);
48     if ( nVal < 0 )
49         nRet = -1;
50     else if ( nVal > 0 )
51         nRet = 1;
52 
53     return nRet;
54 }
55 
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const56 ORowSetValue OOp_Mod::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
57 {
58     if ( lhs.isNull() || rhs.isNull() )
59         return ORowSetValue();
60 
61     return fmod(static_cast<double>(lhs),static_cast<double>(rhs));
62 }
63 
operate(const ORowSetValue & lhs) const64 ORowSetValue OOp_Floor::operate(const ORowSetValue& lhs) const
65 {
66     if ( lhs.isNull() )
67         return lhs;
68 
69     return floor(static_cast<double>(lhs));
70 }
71 
operate(const ORowSetValue & lhs) const72 ORowSetValue OOp_Ceiling::operate(const ORowSetValue& lhs) const
73 {
74     if ( lhs.isNull() )
75         return lhs;
76 
77     double nVal(lhs);
78     return ceil(nVal);
79 }
80 
operate(const std::vector<ORowSetValue> & lhs) const81 ORowSetValue OOp_Round::operate(const std::vector<ORowSetValue>& lhs) const
82 {
83     if ( lhs.empty() || lhs.size() > 2 )
84         return ORowSetValue();
85 
86     size_t nSize = lhs.size();
87     double nVal = lhs[nSize-1];
88 
89     sal_Int32 nDec = 0;
90     if ( nSize == 2 && !lhs[0].isNull() )
91         nDec = lhs[0];
92     return ::rtl::math::round(nVal,nDec);
93 }
94 
operate(const ORowSetValue & lhs) const95 ORowSetValue OOp_Exp::operate(const ORowSetValue& lhs) const
96 {
97     if ( lhs.isNull() )
98         return lhs;
99 
100     double nVal(lhs);
101     return exp(nVal);
102 }
103 
operate(const ORowSetValue & lhs) const104 ORowSetValue OOp_Ln::operate(const ORowSetValue& lhs) const
105 {
106     if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
107         return lhs;
108 
109     double nVal(lhs);
110     nVal = log(nVal);
111     if ( std::isnan(nVal) )
112         return ORowSetValue();
113     return nVal;
114 }
115 
operate(const std::vector<ORowSetValue> & lhs) const116 ORowSetValue OOp_Log::operate(const std::vector<ORowSetValue>& lhs) const
117 {
118     if ( lhs.empty() || lhs.size() > 2 )
119         return ORowSetValue();
120     size_t nSize = lhs.size();
121     double nVal = log( static_cast<double>(lhs[nSize-1]) );
122 
123 
124     if ( nSize == 2 && !lhs[0].isNull() )
125         nVal /= log(static_cast<double>(lhs[0]));
126 
127     if ( std::isnan(nVal) )
128         return ORowSetValue();
129     return nVal;
130 }
131 
operate(const ORowSetValue & lhs) const132 ORowSetValue OOp_Log10::operate(const ORowSetValue& lhs) const
133 {
134     if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
135         return lhs;
136 
137     double nVal = log(static_cast<double>(lhs));
138     if ( std::isnan(nVal) )
139         return ORowSetValue();
140     nVal /= log(10.0);
141     return nVal;
142 }
143 
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const144 ORowSetValue OOp_Pow::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
145 {
146     if ( lhs.isNull() || rhs.isNull() )
147         return lhs;
148 
149     return pow(static_cast<double>(lhs),static_cast<double>(rhs));
150 }
151 
operate(const ORowSetValue & lhs) const152 ORowSetValue OOp_Sqrt::operate(const ORowSetValue& lhs) const
153 {
154     if ( lhs.isNull() )
155         return lhs;
156 
157     double nVal = sqrt(static_cast<double>(lhs));
158     if ( std::isnan(nVal) )
159         return ORowSetValue();
160     return nVal;
161 }
162 
operate(const std::vector<ORowSetValue> &) const163 ORowSetValue OOp_Pi::operate(const std::vector<ORowSetValue>& /*lhs*/) const
164 {
165     return fPi;
166 }
167 
operate(const ORowSetValue & lhs) const168 ORowSetValue OOp_Cos::operate(const ORowSetValue& lhs) const
169 {
170     if ( lhs.isNull() )
171         return lhs;
172 
173     return cos(static_cast<double>(lhs));
174 }
175 
operate(const ORowSetValue & lhs) const176 ORowSetValue OOp_Sin::operate(const ORowSetValue& lhs) const
177 {
178     if ( lhs.isNull() )
179         return lhs;
180 
181     return sin(static_cast<double>(lhs));
182 }
183 
operate(const ORowSetValue & lhs) const184 ORowSetValue OOp_Tan::operate(const ORowSetValue& lhs) const
185 {
186     if ( lhs.isNull() )
187         return lhs;
188 
189     return tan(static_cast<double>(lhs));
190 }
191 
operate(const ORowSetValue & lhs) const192 ORowSetValue OOp_ACos::operate(const ORowSetValue& lhs) const
193 {
194     if ( lhs.isNull() )
195         return lhs;
196 
197     return acos(static_cast<double>(lhs));
198 }
199 
operate(const ORowSetValue & lhs) const200 ORowSetValue OOp_ASin::operate(const ORowSetValue& lhs) const
201 {
202     if ( lhs.isNull() )
203         return lhs;
204 
205     return asin(static_cast<double>(lhs));
206 }
207 
operate(const ORowSetValue & lhs) const208 ORowSetValue OOp_ATan::operate(const ORowSetValue& lhs) const
209 {
210     if ( lhs.isNull() )
211         return lhs;
212 
213     return atan(static_cast<double>(lhs));
214 }
215 
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const216 ORowSetValue OOp_ATan2::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
217 {
218     if ( lhs.isNull() || rhs.isNull() )
219         return lhs;
220 
221     return atan2(static_cast<double>(lhs),static_cast<double>(rhs));
222 }
223 
operate(const ORowSetValue & lhs) const224 ORowSetValue OOp_Degrees::operate(const ORowSetValue& lhs) const
225 {
226     if ( lhs.isNull() )
227         return lhs;
228 
229     double nLhs = lhs;
230     return nLhs*180*(1.0/fPi);
231 }
232 
operate(const ORowSetValue & lhs) const233 ORowSetValue OOp_Radians::operate(const ORowSetValue& lhs) const
234 {
235     if ( lhs.isNull() )
236         return lhs;
237 
238     double nLhs = lhs;
239     return nLhs*fPi*(1.0/180.0);
240 }
241 
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
244