1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include <cmath>
32 #include "file/FNumericFunctions.hxx"
33 #include <rtl/math.hxx>
34 
35 using namespace connectivity;
36 using namespace connectivity::file;
37 //------------------------------------------------------------------
38 ORowSetValue OOp_Abs::operate(const ORowSetValue& lhs) const
39 {
40 	if ( lhs.isNull() )
41 		return lhs;
42 
43 	double nVal(lhs);
44 	if ( nVal < 0 )
45 		nVal *= -1.0;
46 	return fabs(nVal);
47 }
48 //------------------------------------------------------------------
49 ORowSetValue OOp_Sign::operate(const ORowSetValue& lhs) const
50 {
51 	if ( lhs.isNull() )
52 		return lhs;
53 
54 	sal_Int32 nRet = 0;
55 	double nVal(lhs);
56 	if ( nVal < 0 )
57 		nRet = -1;
58 	else if ( nVal > 0 )
59 		nRet = 1;
60 
61 	return nRet;
62 }
63 //------------------------------------------------------------------
64 ORowSetValue OOp_Mod::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
65 {
66 	if ( lhs.isNull() || rhs.isNull() )
67 		return ORowSetValue();
68 
69 	return fmod((double)lhs,(double)rhs);
70 }
71 //------------------------------------------------------------------
72 ORowSetValue OOp_Floor::operate(const ORowSetValue& lhs) const
73 {
74 	if ( lhs.isNull() )
75 		return lhs;
76 
77 	return floor((double)lhs);
78 }
79 // -----------------------------------------------------------------------------
80 ORowSetValue OOp_Ceiling::operate(const ORowSetValue& lhs) const
81 {
82 	if ( lhs.isNull() )
83 		return lhs;
84 
85 	double nVal(lhs);
86 	return ceil(nVal);
87 }
88 // -----------------------------------------------------------------------------
89 ORowSetValue OOp_Round::operate(const ::std::vector<ORowSetValue>& lhs) const
90 {
91 	if ( lhs.empty() || lhs.size() > 2 )
92 		return ORowSetValue();
93 
94 	size_t nSize = lhs.size();
95 	double nVal = lhs[nSize-1];
96 
97 	sal_Int32 nDec = 0;
98 	if ( nSize == 2 && !lhs[0].isNull() )
99 		nDec = lhs[0];
100 	return ::rtl::math::round(nVal,nDec);
101 }
102 // -----------------------------------------------------------------------------
103 ORowSetValue OOp_Exp::operate(const ORowSetValue& lhs) const
104 {
105 	if ( lhs.isNull() )
106 		return lhs;
107 
108 	double nVal(lhs);
109 	return exp(nVal);
110 }
111 // -----------------------------------------------------------------------------
112 ORowSetValue OOp_Ln::operate(const ORowSetValue& lhs) const
113 {
114 	if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
115 		return lhs;
116 
117 	double nVal(lhs);
118 	nVal = log(nVal);
119 	if ( rtl::math::isNan(nVal) )
120 		return ORowSetValue();
121 	return nVal;
122 }
123 // -----------------------------------------------------------------------------
124 ORowSetValue OOp_Log::operate(const ::std::vector<ORowSetValue>& lhs) const
125 {
126 	if ( lhs.empty() || lhs.size() > 2 )
127 		return ORowSetValue();
128 	size_t nSize = lhs.size();
129 	double nVal = log( (double)lhs[nSize-1] );
130 
131 
132 	if ( nSize == 2 && !lhs[0].isNull() )
133 		nVal /= log((double)lhs[0]);
134 
135 	if ( rtl::math::isNan(nVal) )
136 		return ORowSetValue();
137 	return nVal;
138 }
139 // -----------------------------------------------------------------------------
140 ORowSetValue OOp_Log10::operate(const ORowSetValue& lhs) const
141 {
142 	if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
143 		return lhs;
144 
145 	double nVal = log((double)lhs);
146 	if ( rtl::math::isNan(nVal) )
147 		return ORowSetValue();
148 	nVal /= log(10.0);
149 	return nVal;
150 }
151 // -----------------------------------------------------------------------------
152 ORowSetValue OOp_Pow::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
153 {
154 	if ( lhs.isNull() || rhs.isNull() )
155 		return lhs;
156 
157 	return pow((double)lhs,(double)rhs);
158 }
159 //------------------------------------------------------------------
160 ORowSetValue OOp_Sqrt::operate(const ORowSetValue& lhs) const
161 {
162 	if ( lhs.isNull() )
163 		return lhs;
164 
165 	double nVal = sqrt((double)lhs);
166 	if ( rtl::math::isNan(nVal) )
167 		return ORowSetValue();
168 	return nVal;
169 }
170 // -----------------------------------------------------------------------------
171 ORowSetValue OOp_Pi::operate(const ::std::vector<ORowSetValue>& /*lhs*/) const
172 {
173 	return 3.141592653589793116;
174 }
175 // -----------------------------------------------------------------------------
176 ORowSetValue OOp_Cos::operate(const ORowSetValue& lhs) const
177 {
178 	if ( lhs.isNull() )
179 		return lhs;
180 
181 	return cos((double)lhs);
182 }
183 // -----------------------------------------------------------------------------
184 ORowSetValue OOp_Sin::operate(const ORowSetValue& lhs) const
185 {
186 	if ( lhs.isNull() )
187 		return lhs;
188 
189 	return sin((double)lhs);
190 }
191 // -----------------------------------------------------------------------------
192 ORowSetValue OOp_Tan::operate(const ORowSetValue& lhs) const
193 {
194 	if ( lhs.isNull() )
195 		return lhs;
196 
197 	return tan((double)lhs);
198 }
199 // -----------------------------------------------------------------------------
200 ORowSetValue OOp_ACos::operate(const ORowSetValue& lhs) const
201 {
202 	if ( lhs.isNull() )
203 		return lhs;
204 
205 	return acos((double)lhs);
206 }
207 // -----------------------------------------------------------------------------
208 ORowSetValue OOp_ASin::operate(const ORowSetValue& lhs) const
209 {
210 	if ( lhs.isNull() )
211 		return lhs;
212 
213 	return asin((double)lhs);
214 }
215 // -----------------------------------------------------------------------------
216 ORowSetValue OOp_ATan::operate(const ORowSetValue& lhs) const
217 {
218 	if ( lhs.isNull() )
219 		return lhs;
220 
221 	return atan((double)lhs);
222 }
223 // -----------------------------------------------------------------------------
224 ORowSetValue OOp_ATan2::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
225 {
226 	if ( lhs.isNull() || rhs.isNull() )
227 		return lhs;
228 
229 	return atan2((double)lhs,(double)rhs);
230 }
231 // -----------------------------------------------------------------------------
232 ORowSetValue OOp_Degrees::operate(const ORowSetValue& lhs) const
233 {
234 	if ( lhs.isNull() )
235 		return lhs;
236 
237 	double nLhs = lhs;
238 	return nLhs*180*(1.0/3.141592653589793116);
239 }
240 // -----------------------------------------------------------------------------
241 ORowSetValue OOp_Radians::operate(const ORowSetValue& lhs) const
242 {
243 	if ( lhs.isNull() )
244 		return lhs;
245 
246 	double nLhs = lhs;
247 	return nLhs*3.141592653589793116*(1.0/180.0);
248 }
249 // -----------------------------------------------------------------------------
250