1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26
27 #include <cmath>
28 #include "file/FNumericFunctions.hxx"
29 #include <rtl/math.hxx>
30
31 using namespace connectivity;
32 using namespace connectivity::file;
33 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const34 ORowSetValue OOp_Abs::operate(const ORowSetValue& lhs) const
35 {
36 if ( lhs.isNull() )
37 return lhs;
38
39 double nVal(lhs);
40 if ( nVal < 0 )
41 nVal *= -1.0;
42 return fabs(nVal);
43 }
44 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const45 ORowSetValue OOp_Sign::operate(const ORowSetValue& lhs) const
46 {
47 if ( lhs.isNull() )
48 return lhs;
49
50 sal_Int32 nRet = 0;
51 double nVal(lhs);
52 if ( nVal < 0 )
53 nRet = -1;
54 else if ( nVal > 0 )
55 nRet = 1;
56
57 return nRet;
58 }
59 //------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const60 ORowSetValue OOp_Mod::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
61 {
62 if ( lhs.isNull() || rhs.isNull() )
63 return ORowSetValue();
64
65 return fmod((double)lhs,(double)rhs);
66 }
67 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const68 ORowSetValue OOp_Floor::operate(const ORowSetValue& lhs) const
69 {
70 if ( lhs.isNull() )
71 return lhs;
72
73 return floor((double)lhs);
74 }
75 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const76 ORowSetValue OOp_Ceiling::operate(const ORowSetValue& lhs) const
77 {
78 if ( lhs.isNull() )
79 return lhs;
80
81 double nVal(lhs);
82 return ceil(nVal);
83 }
84 // -----------------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const85 ORowSetValue OOp_Round::operate(const ::std::vector<ORowSetValue>& lhs) const
86 {
87 if ( lhs.empty() || lhs.size() > 2 )
88 return ORowSetValue();
89
90 size_t nSize = lhs.size();
91 double nVal = lhs[nSize-1];
92
93 sal_Int32 nDec = 0;
94 if ( nSize == 2 && !lhs[0].isNull() )
95 nDec = lhs[0];
96 return ::rtl::math::round(nVal,nDec);
97 }
98 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const99 ORowSetValue OOp_Exp::operate(const ORowSetValue& lhs) const
100 {
101 if ( lhs.isNull() )
102 return lhs;
103
104 double nVal(lhs);
105 return exp(nVal);
106 }
107 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const108 ORowSetValue OOp_Ln::operate(const ORowSetValue& lhs) const
109 {
110 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
111 return lhs;
112
113 double nVal(lhs);
114 nVal = log(nVal);
115 if ( rtl::math::isNan(nVal) )
116 return ORowSetValue();
117 return nVal;
118 }
119 // -----------------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const120 ORowSetValue OOp_Log::operate(const ::std::vector<ORowSetValue>& lhs) const
121 {
122 if ( lhs.empty() || lhs.size() > 2 )
123 return ORowSetValue();
124 size_t nSize = lhs.size();
125 double nVal = log( (double)lhs[nSize-1] );
126
127
128 if ( nSize == 2 && !lhs[0].isNull() )
129 nVal /= log((double)lhs[0]);
130
131 if ( rtl::math::isNan(nVal) )
132 return ORowSetValue();
133 return nVal;
134 }
135 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const136 ORowSetValue OOp_Log10::operate(const ORowSetValue& lhs) const
137 {
138 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
139 return lhs;
140
141 double nVal = log((double)lhs);
142 if ( rtl::math::isNan(nVal) )
143 return ORowSetValue();
144 nVal /= log(10.0);
145 return nVal;
146 }
147 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const148 ORowSetValue OOp_Pow::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
149 {
150 if ( lhs.isNull() || rhs.isNull() )
151 return lhs;
152
153 return pow((double)lhs,(double)rhs);
154 }
155 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const156 ORowSetValue OOp_Sqrt::operate(const ORowSetValue& lhs) const
157 {
158 if ( lhs.isNull() )
159 return lhs;
160
161 double nVal = sqrt((double)lhs);
162 if ( rtl::math::isNan(nVal) )
163 return ORowSetValue();
164 return nVal;
165 }
166 // -----------------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> &) const167 ORowSetValue OOp_Pi::operate(const ::std::vector<ORowSetValue>& /*lhs*/) const
168 {
169 return 3.141592653589793116;
170 }
171 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const172 ORowSetValue OOp_Cos::operate(const ORowSetValue& lhs) const
173 {
174 if ( lhs.isNull() )
175 return lhs;
176
177 return cos((double)lhs);
178 }
179 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const180 ORowSetValue OOp_Sin::operate(const ORowSetValue& lhs) const
181 {
182 if ( lhs.isNull() )
183 return lhs;
184
185 return sin((double)lhs);
186 }
187 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const188 ORowSetValue OOp_Tan::operate(const ORowSetValue& lhs) const
189 {
190 if ( lhs.isNull() )
191 return lhs;
192
193 return tan((double)lhs);
194 }
195 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const196 ORowSetValue OOp_ACos::operate(const ORowSetValue& lhs) const
197 {
198 if ( lhs.isNull() )
199 return lhs;
200
201 return acos((double)lhs);
202 }
203 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const204 ORowSetValue OOp_ASin::operate(const ORowSetValue& lhs) const
205 {
206 if ( lhs.isNull() )
207 return lhs;
208
209 return asin((double)lhs);
210 }
211 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const212 ORowSetValue OOp_ATan::operate(const ORowSetValue& lhs) const
213 {
214 if ( lhs.isNull() )
215 return lhs;
216
217 return atan((double)lhs);
218 }
219 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const220 ORowSetValue OOp_ATan2::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
221 {
222 if ( lhs.isNull() || rhs.isNull() )
223 return lhs;
224
225 return atan2((double)lhs,(double)rhs);
226 }
227 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const228 ORowSetValue OOp_Degrees::operate(const ORowSetValue& lhs) const
229 {
230 if ( lhs.isNull() )
231 return lhs;
232
233 double nLhs = lhs;
234 return nLhs*180*(1.0/3.141592653589793116);
235 }
236 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const237 ORowSetValue OOp_Radians::operate(const ORowSetValue& lhs) const
238 {
239 if ( lhs.isNull() )
240 return lhs;
241
242 double nLhs = lhs;
243 return nLhs*3.141592653589793116*(1.0/180.0);
244 }
245 // -----------------------------------------------------------------------------
246