update dev300-m58
[ooovba.git] / connectivity / source / drivers / file / FNumericFunctions.cxx
blob84e5ca0809792340bf77a53683baaab2f196feb9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FNumericFunctions.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include <cmath>
35 #include "file/FNumericFunctions.hxx"
36 #include <rtl/math.hxx>
38 using namespace connectivity;
39 using namespace connectivity::file;
40 //------------------------------------------------------------------
41 ORowSetValue OOp_Abs::operate(const ORowSetValue& lhs) const
43 if ( lhs.isNull() )
44 return lhs;
46 double nVal(lhs);
47 if ( nVal < 0 )
48 nVal *= -1.0;
49 return fabs(nVal);
51 //------------------------------------------------------------------
52 ORowSetValue OOp_Sign::operate(const ORowSetValue& lhs) const
54 if ( lhs.isNull() )
55 return lhs;
57 sal_Int32 nRet = 0;
58 double nVal(lhs);
59 if ( nVal < 0 )
60 nRet = -1;
61 else if ( nVal > 0 )
62 nRet = 1;
64 return nRet;
66 //------------------------------------------------------------------
67 ORowSetValue OOp_Mod::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
69 if ( lhs.isNull() || rhs.isNull() )
70 return ORowSetValue();
72 return fmod((double)lhs,(double)rhs);
74 //------------------------------------------------------------------
75 ORowSetValue OOp_Floor::operate(const ORowSetValue& lhs) const
77 if ( lhs.isNull() )
78 return lhs;
80 return floor((double)lhs);
82 // -----------------------------------------------------------------------------
83 ORowSetValue OOp_Ceiling::operate(const ORowSetValue& lhs) const
85 if ( lhs.isNull() )
86 return lhs;
88 double nVal(lhs);
89 return ceil(nVal);
91 // -----------------------------------------------------------------------------
92 ORowSetValue OOp_Round::operate(const ::std::vector<ORowSetValue>& lhs) const
94 if ( lhs.empty() || lhs.size() > 2 )
95 return ORowSetValue();
97 size_t nSize = lhs.size();
98 double nVal = lhs[nSize-1];
100 sal_Int32 nDec = 0;
101 if ( nSize == 2 && !lhs[0].isNull() )
102 nDec = lhs[0];
103 return ::rtl::math::round(nVal,nDec);
105 // -----------------------------------------------------------------------------
106 ORowSetValue OOp_Exp::operate(const ORowSetValue& lhs) const
108 if ( lhs.isNull() )
109 return lhs;
111 double nVal(lhs);
112 return exp(nVal);
114 // -----------------------------------------------------------------------------
115 ORowSetValue OOp_Ln::operate(const ORowSetValue& lhs) const
117 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
118 return lhs;
120 double nVal(lhs);
121 nVal = log(nVal);
122 if ( rtl::math::isNan(nVal) )
123 return ORowSetValue();
124 return nVal;
126 // -----------------------------------------------------------------------------
127 ORowSetValue OOp_Log::operate(const ::std::vector<ORowSetValue>& lhs) const
129 if ( lhs.empty() || lhs.size() > 2 )
130 return ORowSetValue();
131 size_t nSize = lhs.size();
132 double nVal = log( (double)lhs[nSize-1] );
135 if ( nSize == 2 && !lhs[0].isNull() )
136 nVal /= log((double)lhs[0]);
138 if ( rtl::math::isNan(nVal) )
139 return ORowSetValue();
140 return nVal;
142 // -----------------------------------------------------------------------------
143 ORowSetValue OOp_Log10::operate(const ORowSetValue& lhs) const
145 if ( lhs.isNull() || static_cast<double>(lhs) < 0.0 )
146 return lhs;
148 double nVal = log((double)lhs);
149 if ( rtl::math::isNan(nVal) )
150 return ORowSetValue();
151 nVal /= log(10.0);
152 return nVal;
154 // -----------------------------------------------------------------------------
155 ORowSetValue OOp_Pow::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
157 if ( lhs.isNull() || rhs.isNull() )
158 return lhs;
160 return pow((double)lhs,(double)rhs);
162 //------------------------------------------------------------------
163 ORowSetValue OOp_Sqrt::operate(const ORowSetValue& lhs) const
165 if ( lhs.isNull() )
166 return lhs;
168 double nVal = sqrt((double)lhs);
169 if ( rtl::math::isNan(nVal) )
170 return ORowSetValue();
171 return nVal;
173 // -----------------------------------------------------------------------------
174 ORowSetValue OOp_Pi::operate(const ::std::vector<ORowSetValue>& /*lhs*/) const
176 return 3.141592653589793116;
178 // -----------------------------------------------------------------------------
179 ORowSetValue OOp_Cos::operate(const ORowSetValue& lhs) const
181 if ( lhs.isNull() )
182 return lhs;
184 return cos((double)lhs);
186 // -----------------------------------------------------------------------------
187 ORowSetValue OOp_Sin::operate(const ORowSetValue& lhs) const
189 if ( lhs.isNull() )
190 return lhs;
192 return sin((double)lhs);
194 // -----------------------------------------------------------------------------
195 ORowSetValue OOp_Tan::operate(const ORowSetValue& lhs) const
197 if ( lhs.isNull() )
198 return lhs;
200 return tan((double)lhs);
202 // -----------------------------------------------------------------------------
203 ORowSetValue OOp_ACos::operate(const ORowSetValue& lhs) const
205 if ( lhs.isNull() )
206 return lhs;
208 return acos((double)lhs);
210 // -----------------------------------------------------------------------------
211 ORowSetValue OOp_ASin::operate(const ORowSetValue& lhs) const
213 if ( lhs.isNull() )
214 return lhs;
216 return asin((double)lhs);
218 // -----------------------------------------------------------------------------
219 ORowSetValue OOp_ATan::operate(const ORowSetValue& lhs) const
221 if ( lhs.isNull() )
222 return lhs;
224 return atan((double)lhs);
226 // -----------------------------------------------------------------------------
227 ORowSetValue OOp_ATan2::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
229 if ( lhs.isNull() || rhs.isNull() )
230 return lhs;
232 return atan2((double)lhs,(double)rhs);
234 // -----------------------------------------------------------------------------
235 ORowSetValue OOp_Degrees::operate(const ORowSetValue& lhs) const
237 if ( lhs.isNull() )
238 return lhs;
240 double nLhs = lhs;
241 return nLhs*180*(1.0/3.141592653589793116);
243 // -----------------------------------------------------------------------------
244 ORowSetValue OOp_Radians::operate(const ORowSetValue& lhs) const
246 if ( lhs.isNull() )
247 return lhs;
249 double nLhs = lhs;
250 return nLhs*3.141592653589793116*(1.0/180.0);
252 // -----------------------------------------------------------------------------