kspread Library API Documentation

kspread_functions_trig.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998-2002 The KSpread Team
00003                            www.koffice.org/kspread
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 // built-in trigonometric functions
00022 
00023 #include <stdlib.h>
00024 #include <math.h>
00025 #include <float.h>
00026 
00027 #include <kdebug.h>
00028 
00029 #include <koscript_parser.h>
00030 #include <koscript_util.h>
00031 #include <koscript_func.h>
00032 #include <koscript_synext.h>
00033 
00034 #include "kspread_functions.h"
00035 #include "kspread_util.h"
00036 
00037 
00038 // prototypes (sort alphabetically)
00039 bool kspreadfunc_acos( KSContext& context );
00040 bool kspreadfunc_acosh( KSContext& context );
00041 bool kspreadfunc_acot( KSContext& context );
00042 bool kspreadfunc_asinh( KSContext& context );
00043 bool kspreadfunc_asin( KSContext& context );
00044 bool kspreadfunc_atan( KSContext& context );
00045 bool kspreadfunc_atan2( KSContext& context );
00046 bool kspreadfunc_atanh( KSContext& context );
00047 bool kspreadfunc_cos( KSContext& context );
00048 bool kspreadfunc_cosh( KSContext& context );
00049 bool kspreadfunc_degrees( KSContext& context );
00050 bool kspreadfunc_radians( KSContext& context );
00051 bool kspreadfunc_sin( KSContext& context );
00052 bool kspreadfunc_sinh( KSContext& context );
00053 bool kspreadfunc_tan( KSContext& context );
00054 bool kspreadfunc_tanh( KSContext& context );
00055 bool kspreadfunc_pi( KSContext& context );
00056 
00057 // registers all trigonometric functions
00058 void KSpreadRegisterTrigFunctions()
00059 {
00060   KSpreadFunctionRepository* repo = KSpreadFunctionRepository::self();
00061 
00062   repo->registerFunction( "ACOS",   kspreadfunc_acos );
00063   repo->registerFunction( "ACOSH",  kspreadfunc_acosh );
00064   repo->registerFunction( "ACOT",  kspreadfunc_acot );
00065   repo->registerFunction( "ASIN",   kspreadfunc_asin );
00066   repo->registerFunction( "ASINH",  kspreadfunc_asinh );
00067   repo->registerFunction( "ATAN",   kspreadfunc_atan );
00068   repo->registerFunction( "ATAN2",  kspreadfunc_atan2 );
00069   repo->registerFunction( "ATANH",  kspreadfunc_atanh );
00070   repo->registerFunction( "COS",    kspreadfunc_cos );
00071   repo->registerFunction( "COSH",   kspreadfunc_cosh );
00072   repo->registerFunction( "DEGREE", kspreadfunc_degrees );  // backward-compatible, remove in 1.4
00073   repo->registerFunction( "DEGREES",kspreadfunc_degrees );
00074   repo->registerFunction( "RADIAN", kspreadfunc_radians ); // backwared-compatible, remove in 1.4
00075   repo->registerFunction( "RADIANS",kspreadfunc_radians );
00076   repo->registerFunction( "SIN",    kspreadfunc_sin );
00077   repo->registerFunction( "SINH",   kspreadfunc_sinh );
00078   repo->registerFunction( "TAN",    kspreadfunc_tan );
00079   repo->registerFunction( "TANH",   kspreadfunc_tanh );
00080   repo->registerFunction( "PI",     kspreadfunc_pi );
00081 }
00082 
00083 
00084 // Function: sin
00085 bool kspreadfunc_sin( KSContext& context )
00086 {
00087   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00088 
00089   if ( !KSUtil::checkArgumentsCount( context, 1, "sin", true ) )
00090     return false;
00091   double val=0.0;
00092   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00093     {
00094       if( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
00095     return false;
00096     }
00097   val=args[0]->doubleValue();
00098 
00099   context.setValue( new KSValue( sin( val ) ) );
00100 
00101   return true;
00102 }
00103 
00104 // Function: cos
00105 bool kspreadfunc_cos( KSContext& context )
00106 {
00107   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00108 
00109   if ( !KSUtil::checkArgumentsCount( context, 1, "cos", true ) )
00110     return false;
00111   double val=0.0;
00112   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00113     {
00114       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00115     return false;
00116     }
00117   else
00118     val=args[0]->doubleValue();
00119 
00120   context.setValue( new KSValue( cos( val ) ) );
00121 
00122   return true;
00123 }
00124 
00125 // Function: tan
00126 bool kspreadfunc_tan( KSContext& context )
00127 {
00128   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00129 
00130   if ( !KSUtil::checkArgumentsCount( context, 1, "tan", true ) )
00131     return false;
00132 
00133   double val=0.0;
00134   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00135     {
00136       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00137     return false;
00138     }
00139   else
00140     val=args[0]->doubleValue();
00141   context.setValue( new KSValue( tan(val) ) );
00142 
00143   return true;
00144 }
00145 
00146 // Function: atan
00147 bool kspreadfunc_atan( KSContext& context )
00148 {
00149   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00150 
00151   if ( !KSUtil::checkArgumentsCount( context, 1, "atan", true ) )
00152     return false;
00153 
00154   double val=0.0;
00155   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00156     {
00157       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00158     return false;
00159     }
00160   else
00161     val=args[0]->doubleValue();
00162 
00163   context.setValue( new KSValue( atan( val ) ) );
00164 
00165   return true;
00166 }
00167 
00168 // Function: asin
00169 bool kspreadfunc_asin( KSContext& context )
00170 {
00171   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00172 
00173   if ( !KSUtil::checkArgumentsCount( context, 1, "asin", true ) )
00174     return false;
00175 
00176   double val=0.0;
00177   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00178     {
00179       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00180     return false;
00181     }
00182   else
00183     val=args[0]->doubleValue();
00184 
00185   context.setValue( new KSValue( asin( val ) ) );
00186 
00187   return true;
00188 }
00189 
00190 // Function: acos
00191 bool kspreadfunc_acos( KSContext& context )
00192 {
00193   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00194 
00195   if ( !KSUtil::checkArgumentsCount( context, 1, "acos", true ) )
00196     return false;
00197 
00198   double val=0.0;
00199   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00200     {
00201       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00202     return false;
00203     }
00204   else
00205     val=args[0]->doubleValue();
00206 
00207 
00208   context.setValue( new KSValue( acos( val ) ) );
00209 
00210   return true;
00211 }
00212 
00213 bool kspreadfunc_acot( KSContext& context )
00214 {
00215   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00216 
00217   if ( !KSUtil::checkArgumentsCount( context, 1, "acot", true ) )
00218     return false;
00219 
00220   double val=0.0;
00221   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00222     {
00223       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00224     return false;
00225     }
00226   else
00227     val=args[0]->doubleValue();
00228 
00229 
00230   context.setValue( new KSValue( M_PI/2 - atan( val ) ) );
00231 
00232   return true;
00233 }
00234 
00235 
00236 // Function: asinh
00237 bool kspreadfunc_asinh( KSContext& context )
00238 {
00239   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00240 
00241   if ( !KSUtil::checkArgumentsCount( context, 1, "asinh", true ) )
00242     return false;
00243 
00244   double val=0.0;
00245   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00246     {
00247       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00248     return false;
00249     }
00250   else
00251     val=args[0]->doubleValue();
00252 
00253   context.setValue( new KSValue( asinh( val ) ) );
00254 
00255   return true;
00256 }
00257 
00258 // Function: acosh
00259 bool kspreadfunc_acosh( KSContext& context )
00260 {
00261   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00262 
00263   if ( !KSUtil::checkArgumentsCount( context, 1, "acosh", true ) )
00264     return false;
00265 
00266   double val=0.0;
00267   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00268     {
00269       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00270     return false;
00271     }
00272   else
00273     val=args[0]->doubleValue();
00274 
00275   context.setValue( new KSValue( acosh( val ) ) );
00276 
00277   return true;
00278 }
00279 
00280 // Function: atanh
00281 bool kspreadfunc_atanh( KSContext& context )
00282 {
00283   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00284 
00285   if ( !KSUtil::checkArgumentsCount( context, 1, "atanh", true ) )
00286     return false;
00287 
00288   double val=0.0;
00289   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00290     {
00291       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00292     return false;
00293     }
00294   else
00295     val=args[0]->doubleValue();
00296 
00297 
00298   context.setValue( new KSValue( atanh( val ) ) );
00299 
00300   return true;
00301 }
00302 
00303 // Function: tanh
00304 bool kspreadfunc_tanh( KSContext& context )
00305 {
00306   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00307 
00308   if ( !KSUtil::checkArgumentsCount( context, 1, "tanh", true ) )
00309     return false;
00310 
00311   double val=0.0;
00312   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00313     {
00314       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00315     return false;
00316     }
00317   else
00318     val=args[0]->doubleValue();
00319 
00320   context.setValue( new KSValue( tanh( val ) ) );
00321 
00322   return true;
00323 }
00324 
00325 // Function: sinh
00326 bool kspreadfunc_sinh( KSContext& context )
00327 {
00328   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00329 
00330   if ( !KSUtil::checkArgumentsCount( context, 1, "sinh", true ) )
00331     return false;
00332 
00333   double val=0.0;
00334   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00335     {
00336       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00337     return false;
00338     }
00339   else
00340     val=args[0]->doubleValue();
00341 
00342   context.setValue( new KSValue( sinh( val ) ) );
00343 
00344   return true;
00345 }
00346 
00347 // Function: cosh
00348 bool kspreadfunc_cosh( KSContext& context )
00349 {
00350   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00351 
00352   if ( !KSUtil::checkArgumentsCount( context, 1, "cosh", true ) )
00353     return false;
00354 
00355   double val=0.0;
00356   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00357     {
00358       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00359     return false;
00360     }
00361   else
00362     val=args[0]->doubleValue();
00363 
00364   context.setValue( new KSValue( cosh( val ) ) );
00365 
00366   return true;
00367 }
00368 
00369 // Function: DEGREES
00370 bool kspreadfunc_degrees( KSContext& context )
00371 {
00372   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00373 
00374   if ( !KSUtil::checkArgumentsCount( context, 1, "degrees", true ) )
00375     return false;
00376 
00377   double val=0.0;
00378   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00379     {
00380       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00381     return false;
00382     }
00383   else
00384     val=args[0]->doubleValue();
00385 
00386   context.setValue( new KSValue( (val*180)/M_PI  ));
00387 
00388   return true;
00389 }
00390 
00391 // Function: RADIANS
00392 bool kspreadfunc_radians( KSContext& context )
00393 {
00394   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00395 
00396   if ( !KSUtil::checkArgumentsCount( context, 1, "radians", true ) )
00397     return false;
00398 
00399   double val=0.0;
00400   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00401     {
00402       if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
00403     return false;
00404     }
00405   else
00406     val=args[0]->doubleValue();
00407 
00408 
00409   context.setValue( new KSValue( (val*M_PI )/180  ));
00410 
00411   return true;
00412 }
00413 
00414 // Function: PI
00415 bool kspreadfunc_pi( KSContext& context )
00416 {
00417     // QValueList<KSValue::Ptr>& args = context.value()->listValue();
00418 
00419     if ( !KSUtil::checkArgumentsCount( context, 0, "PI", true ) )
00420       return false;
00421 
00422     context.setValue( new KSValue(M_PI));
00423     return true;
00424 }
00425 
00426 // Function: atan2
00427 bool kspreadfunc_atan2( KSContext& context )
00428 {
00429   QValueList<KSValue::Ptr>& args = context.value()->listValue();
00430 
00431   if ( !KSUtil::checkArgumentsCount( context, 2, "atan2", true ) )
00432     return false;
00433 
00434   if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
00435     return false;
00436   if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
00437     return false;
00438   context.setValue( new KSValue( atan2( args[1]->doubleValue(),args[0]->doubleValue() ) ) );
00439 
00440   return true;
00441 }
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:13 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003