120
140
makeDepends( context, node->branch5(), m, t, depends );
123
/*********************************************************************
125
* Helper function to avoid problems with rounding floating point
126
* values. Idea for this kind of solution taken from Openoffice.
128
*********************************************************************/
130
static bool approx_equal (double a, double b)
135
return (x < 0.0 ? -x : x) < ((a < 0.0 ? -a : a) * DBL_EPSILON);
138
/*********************************************************************
140
* Module with global functions like "sin", "cos", "sum" etc.
142
*********************************************************************/
144
static bool kspreadfunc_sin( KSContext& context )
146
QValueList<KSValue::Ptr>& args = context.value()->listValue();
148
if ( !KSUtil::checkArgumentsCount( context, 1, "sin", true ) )
151
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
153
if( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
156
val=args[0]->doubleValue();
158
context.setValue( new KSValue( sin( val ) ) );
166
static bool kspreadfunc_cos( KSContext& context )
168
QValueList<KSValue::Ptr>& args = context.value()->listValue();
170
if ( !KSUtil::checkArgumentsCount( context, 1, "cos", true ) )
173
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
175
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
179
val=args[0]->doubleValue();
181
context.setValue( new KSValue( cos( val ) ) );
186
static bool kspreadfunc_sqrt( KSContext& context )
188
QValueList<KSValue::Ptr>& args = context.value()->listValue();
190
if ( !KSUtil::checkArgumentsCount( context, 1, "sqrt", true ) )
193
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
195
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
199
val=args[0]->doubleValue();
201
context.setValue( new KSValue( sqrt( val ) ) );
206
static bool kspreadfunc_sqrtn( KSContext& context )
208
QValueList<KSValue::Ptr>& args = context.value()->listValue();
210
if ( !KSUtil::checkArgumentsCount( context, 2, "SQRTn", true ) )
213
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
215
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
218
context.setValue( new KSValue( exp( log(args[0]->doubleValue())/args[1]->intValue() ) ) );
223
static bool kspreadfunc_cur( KSContext& context )
225
QValueList<KSValue::Ptr>& args = context.value()->listValue();
227
if ( !KSUtil::checkArgumentsCount( context, 1, "CUR", true ) )
230
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
233
context.setValue( new KSValue( exp( log(args[0]->doubleValue())/3)) );
239
static bool kspreadfunc_fabs( KSContext& context )
241
QValueList<KSValue::Ptr>& args = context.value()->listValue();
243
if ( !KSUtil::checkArgumentsCount( context, 1, "fabs", true ) )
247
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
249
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
253
val=args[0]->doubleValue();
255
context.setValue( new KSValue( fabs(val ) ) );
260
static bool kspreadfunc_tan( KSContext& context )
262
QValueList<KSValue::Ptr>& args = context.value()->listValue();
264
if ( !KSUtil::checkArgumentsCount( context, 1, "tan", true ) )
268
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
270
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
274
val=args[0]->doubleValue();
275
context.setValue( new KSValue( tan(val) ) );
280
static bool kspreadfunc_exp( KSContext& context )
282
QValueList<KSValue::Ptr>& args = context.value()->listValue();
284
if ( !KSUtil::checkArgumentsCount( context, 1, "exp",true ) )
288
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
290
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
294
val=args[0]->doubleValue();
296
context.setValue( new KSValue( exp( val ) ) );
301
static bool kspreadfunc_ceil( KSContext& context )
303
QValueList<KSValue::Ptr>& args = context.value()->listValue();
305
if ( !KSUtil::checkArgumentsCount( context, 1, "ceil", true ) )
308
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
310
if (approx_equal(floor(args[0]->doubleValue()),args[0]->doubleValue()))
311
context.setValue( new KSValue(args[0]->doubleValue()));
313
context.setValue( new KSValue( ceil( args[0]->doubleValue() ) ) );
318
static bool kspreadfunc_floor( KSContext& context )
320
QValueList<KSValue::Ptr>& args = context.value()->listValue();
322
if ( !KSUtil::checkArgumentsCount( context, 1, "floor", true ) )
326
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
328
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
332
val=args[0]->doubleValue();
334
context.setValue( new KSValue( floor( val ) ) );
339
static bool kspreadfunc_atan( KSContext& context )
341
QValueList<KSValue::Ptr>& args = context.value()->listValue();
343
if ( !KSUtil::checkArgumentsCount( context, 1, "atan", true ) )
347
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
349
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
353
val=args[0]->doubleValue();
355
context.setValue( new KSValue( atan( val ) ) );
360
static bool kspreadfunc_ln( KSContext& context )
362
QValueList<KSValue::Ptr>& args = context.value()->listValue();
364
if ( !KSUtil::checkArgumentsCount( context, 1, "ln", true ) )
368
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
370
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
374
val=args[0]->doubleValue();
376
context.setValue( new KSValue( log( val ) ) );
381
static bool kspreadfunc_logn( KSContext& context )
383
QValueList<KSValue::Ptr>& args = context.value()->listValue();
385
if ( !KSUtil::checkArgumentsCount( context, 2, "LOGn", true ) )
388
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
390
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
393
context.setValue( new KSValue( log( args[0]->doubleValue() ) /log((double)args[1]->intValue() )) );
399
static bool kspreadfunc_asin( KSContext& context )
401
QValueList<KSValue::Ptr>& args = context.value()->listValue();
403
if ( !KSUtil::checkArgumentsCount( context, 1, "asin", true ) )
407
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
409
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
413
val=args[0]->doubleValue();
415
context.setValue( new KSValue( asin( val ) ) );
420
static bool kspreadfunc_acos( KSContext& context )
422
QValueList<KSValue::Ptr>& args = context.value()->listValue();
424
if ( !KSUtil::checkArgumentsCount( context, 1, "acos", true ) )
428
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
430
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
434
val=args[0]->doubleValue();
437
context.setValue( new KSValue( acos( val ) ) );
442
static bool kspreadfunc_log( KSContext& context )
444
QValueList<KSValue::Ptr>& args = context.value()->listValue();
446
if ( !KSUtil::checkArgumentsCount( context, 1, "log", true ) )
450
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
452
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
456
val=args[0]->doubleValue();
458
context.setValue( new KSValue( log10( val ) ) );
463
static bool kspreadfunc_asinh( KSContext& context )
465
QValueList<KSValue::Ptr>& args = context.value()->listValue();
467
if ( !KSUtil::checkArgumentsCount( context, 1, "asinh", true ) )
471
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
473
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
477
val=args[0]->doubleValue();
479
context.setValue( new KSValue( asinh( val ) ) );
484
static bool kspreadfunc_acosh( KSContext& context )
486
QValueList<KSValue::Ptr>& args = context.value()->listValue();
488
if ( !KSUtil::checkArgumentsCount( context, 1, "acosh", true ) )
492
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
494
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
498
val=args[0]->doubleValue();
500
context.setValue( new KSValue( acosh( val ) ) );
505
static bool kspreadfunc_atanh( KSContext& context )
507
QValueList<KSValue::Ptr>& args = context.value()->listValue();
509
if ( !KSUtil::checkArgumentsCount( context, 1, "atanh", true ) )
513
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
515
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
519
val=args[0]->doubleValue();
522
context.setValue( new KSValue( atanh( val ) ) );
527
static bool kspreadfunc_tanh( KSContext& context )
529
QValueList<KSValue::Ptr>& args = context.value()->listValue();
531
if ( !KSUtil::checkArgumentsCount( context, 1, "tanh", true ) )
535
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
537
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
541
val=args[0]->doubleValue();
543
context.setValue( new KSValue( tanh( val ) ) );
548
static bool kspreadfunc_sinh( KSContext& context )
550
QValueList<KSValue::Ptr>& args = context.value()->listValue();
552
if ( !KSUtil::checkArgumentsCount( context, 1, "sinh", true ) )
556
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
558
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
562
val=args[0]->doubleValue();
564
context.setValue( new KSValue( sinh( val ) ) );
569
static bool kspreadfunc_cosh( KSContext& context )
571
QValueList<KSValue::Ptr>& args = context.value()->listValue();
573
if ( !KSUtil::checkArgumentsCount( context, 1, "cosh", true ) )
577
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
579
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
583
val=args[0]->doubleValue();
585
context.setValue( new KSValue( cosh( val ) ) );
590
static bool kspreadfunc_degree( KSContext& context )
592
QValueList<KSValue::Ptr>& args = context.value()->listValue();
594
if ( !KSUtil::checkArgumentsCount( context, 1, "degree", true ) )
598
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
600
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
604
val=args[0]->doubleValue();
606
context.setValue( new KSValue( (val*180)/M_PI ));
611
static bool kspreadfunc_radian( KSContext& context )
613
QValueList<KSValue::Ptr>& args = context.value()->listValue();
615
if ( !KSUtil::checkArgumentsCount( context, 1, "radian", true ) )
619
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
621
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
625
val=args[0]->doubleValue();
628
context.setValue( new KSValue( (val*M_PI )/180 ));
634
static bool kspreadfunc_sum_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
636
QValueList<KSValue::Ptr>::Iterator it = args.begin();
637
QValueList<KSValue::Ptr>::Iterator end = args.end();
639
for( ; it != end; ++it )
641
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
643
if ( !kspreadfunc_sum_helper( context, (*it)->listValue(), result ) )
646
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
648
result += (*it)->doubleValue();
650
/*else if ( !KSUtil::checkType( context, *it, KSValue::Empty, true ) )
657
static bool kspreadfunc_sum( KSContext& context )
660
bool b = kspreadfunc_sum_helper( context, context.value()->listValue(), result );
663
context.setValue( new KSValue( result ) );
668
static bool kspreadfunc_sumsq_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
670
QValueList<KSValue::Ptr>::Iterator it = args.begin();
671
QValueList<KSValue::Ptr>::Iterator end = args.end();
673
for( ; it != end; ++it )
675
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
677
if ( !kspreadfunc_sum_helper( context, (*it)->listValue(), result ) )
680
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
682
result += ((*it)->doubleValue()*(*it)->doubleValue());
684
else if ( !KSUtil::checkType( context, *it, KSValue::Empty, true ) )
691
static bool kspreadfunc_sumsq( KSContext& context )
694
bool b = kspreadfunc_sumsq_helper( context, context.value()->listValue(), result );
697
context.setValue( new KSValue( result ) );
703
static bool kspreadfunc_max_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& inter)
705
QValueList<KSValue::Ptr>::Iterator it = args.begin();
706
QValueList<KSValue::Ptr>::Iterator end = args.end();
708
for( ; it != end; ++it )
710
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
713
if ( !kspreadfunc_max_helper( context, (*it)->listValue(), result,inter ) )
716
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
720
result=(*it)->doubleValue();
723
if(result < (*it)->doubleValue())
724
result =(*it)->doubleValue();
731
static bool kspreadfunc_max( KSContext& context )
738
bool b = kspreadfunc_max_helper( context, context.value()->listValue(), result ,inter );
741
context.setValue( new KSValue( result ) );
746
static bool kspreadfunc_min_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& inter)
748
QValueList<KSValue::Ptr>::Iterator it = args.begin();
749
QValueList<KSValue::Ptr>::Iterator end = args.end();
751
for( ; it != end; ++it )
753
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
756
if ( !kspreadfunc_min_helper( context, (*it)->listValue(), result,inter ) )
759
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
763
result=(*it)->doubleValue();
766
if(result > (*it)->doubleValue())
767
result =(*it)->doubleValue();
774
static bool kspreadfunc_min( KSContext& context )
781
bool b = kspreadfunc_min_helper( context, context.value()->listValue(), result ,inter );
784
context.setValue( new KSValue( result ) );
789
static bool kspreadfunc_average_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& number)
791
QValueList<KSValue::Ptr>::Iterator it = args.begin();
792
QValueList<KSValue::Ptr>::Iterator end = args.end();
794
for( ; it != end; ++it )
796
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
798
if ( !kspreadfunc_average_helper( context, (*it)->listValue(), result ,number) )
801
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
803
result += (*it)->doubleValue();
811
static bool kspreadfunc_average( KSContext& context )
816
bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
820
context.setValue( new KSValue( i18n("#DIV/0") ) );
825
context.setValue( new KSValue( result / (double)number ) );
830
static bool kspreadfunc_variance_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result, double avera)
832
QValueList<KSValue::Ptr>::Iterator it = args.begin();
833
QValueList<KSValue::Ptr>::Iterator end = args.end();
835
for( ; it != end; ++it )
837
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
839
if ( !kspreadfunc_variance_helper( context, (*it)->listValue(), result ,avera) )
842
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
844
result += ( (*it)->doubleValue() - avera ) * ( (*it)->doubleValue() - avera );
851
static bool kspreadfunc_variance( KSContext& context )
856
bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
863
avera = result / (double)number;
865
bool b = kspreadfunc_variance_helper( context, context.value()->listValue(), result, avera );
867
context.setValue( new KSValue(result / (double)number ) );
873
static bool kspreadfunc_stddev_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,double& avera)
875
QValueList<KSValue::Ptr>::Iterator it = args.begin();
876
QValueList<KSValue::Ptr>::Iterator end = args.end();
878
for( ; it != end; ++it )
880
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
882
if ( !kspreadfunc_stddev_helper( context, (*it)->listValue(), result ,avera) )
886
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
888
result += (((*it)->doubleValue()-avera)*((*it)->doubleValue()-avera));
895
static bool kspreadfunc_stddev( KSContext& context )
900
bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
909
bool b = kspreadfunc_stddev_helper( context, context.value()->listValue(), result,avera );
911
context.setValue( new KSValue(sqrt(result/number )) );
917
static bool kspreadfunc_mult_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
919
QValueList<KSValue::Ptr>::Iterator it = args.begin();
920
QValueList<KSValue::Ptr>::Iterator end = args.end();
922
for( ; it != end; ++it )
924
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
926
if ( !kspreadfunc_mult_helper( context, (*it)->listValue(), result ) )
929
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
930
result *= (*it)->doubleValue();
936
static bool kspreadfunc_mult( KSContext& context )
939
bool b = kspreadfunc_mult_helper( context, context.value()->listValue(), result );
942
context.setValue( new KSValue( result ) );
948
static bool kspreadfunc_join_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& tmp )
950
QValueList<KSValue::Ptr>::Iterator it = args.begin();
951
QValueList<KSValue::Ptr>::Iterator end = args.end();
953
for( ; it != end; ++it )
955
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
957
if ( !kspreadfunc_join_helper( context, (*it)->listValue(), tmp ) )
960
else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
961
tmp+= (*it)->stringValue();
962
else if( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
963
tmp+= KGlobal::locale()->formatNumber((*it)->doubleValue());
970
static bool kspreadfunc_join( KSContext& context )
973
bool b = kspreadfunc_join_helper( context, context.value()->listValue(), tmp );
976
context.setValue( new KSValue( tmp ) );
981
static bool kspreadfunc_not( KSContext& context )
983
QValueList<KSValue::Ptr>& args = context.value()->listValue();
985
if ( !KSUtil::checkArgumentsCount( context, 1, "NOT", true ) || !KSUtil::checkArgumentsCount( context, 1, "not", true ) )
988
if ( !KSUtil::checkType( context, args[0], KSValue::BoolType, true ) )
991
bool toto = !args[0]->boolValue();
992
context.setValue( new KSValue(toto));
997
static bool kspreadfunc_or_helper( KSContext& context, QValueList<KSValue::Ptr>& args, bool& first )
999
QValueList<KSValue::Ptr>::Iterator it = args.begin();
1000
QValueList<KSValue::Ptr>::Iterator end = args.end();
1002
for( ; it != end; ++it )
1004
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
1006
if ( !kspreadfunc_or_helper( context, (*it)->listValue(), first ) )
1009
else if ( KSUtil::checkType( context, *it, KSValue::BoolType, true ) )
1010
first = (first || (*it)->boolValue());
1018
static bool kspreadfunc_or( KSContext& context )
1021
bool b = kspreadfunc_or_helper( context, context.value()->listValue(), first );
1024
context.setValue( new KSValue( first ) );
1029
static bool kspreadfunc_nor( KSContext& context )
1032
bool b = kspreadfunc_or_helper( context, context.value()->listValue(), first );
1035
context.setValue( new KSValue( !first ) );
1041
static bool kspreadfunc_and_helper( KSContext& context, QValueList<KSValue::Ptr>& args, bool& first )
1043
QValueList<KSValue::Ptr>::Iterator it = args.begin();
1044
QValueList<KSValue::Ptr>::Iterator end = args.end();
1046
for( ; it != end; ++it )
1048
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
1050
if ( !kspreadfunc_and_helper( context, (*it)->listValue(), first ) )
1053
else if ( KSUtil::checkType( context, *it, KSValue::BoolType, true ) )
1054
first = first && (*it)->boolValue();
1062
static bool kspreadfunc_and( KSContext& context )
1065
bool b = kspreadfunc_and_helper( context, context.value()->listValue(), first );
1068
context.setValue( new KSValue( first ) );
1073
static bool kspreadfunc_nand( KSContext& context )
1076
bool b = kspreadfunc_and_helper( context, context.value()->listValue(), first );
1079
context.setValue( new KSValue( !first ) );
1085
static bool kspreadfunc_if( KSContext& context )
1087
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1089
if ( !KSUtil::checkArgumentsCount( context, 3, "if", true ) || !KSUtil::checkArgumentsCount( context, 3, "IF", true ))
1092
if ( !KSUtil::checkType( context, args[0], KSValue::BoolType, true ) )
1095
if ( args[0]->boolValue() == true )
1096
context.setValue( new KSValue( *(args[1]) ) );
1098
context.setValue( new KSValue( *(args[2]) ) );
1103
static bool kspreadfunc_left( KSContext& context )
1105
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1107
if ( !KSUtil::checkArgumentsCount( context, 2, "left", true ) )
1110
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1113
if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1114
nb = (int) args[1]->doubleValue();
1115
else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1116
nb = args[1]->intValue();
1120
QString tmp = args[0]->stringValue().left(nb);
1121
context.setValue( new KSValue( tmp ) );
1125
static bool kspreadfunc_right( KSContext& context )
1127
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1129
if ( !KSUtil::checkArgumentsCount( context, 2, "right", true ) )
1132
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1135
if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1136
nb = (int) args[1]->doubleValue();
1137
else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1138
nb = args[1]->intValue();
1142
QString tmp = args[0]->stringValue().right(nb);
1143
context.setValue( new KSValue(tmp));
1147
static bool kspreadfunc_upper( KSContext& context )
1149
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1151
if ( !KSUtil::checkArgumentsCount( context, 1, "upper", true ) )
1154
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1157
QString tmp = args[0]->stringValue().upper();
1158
context.setValue( new KSValue( tmp ) );
1162
static bool kspreadfunc_lower( KSContext& context )
1164
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1166
if ( !KSUtil::checkArgumentsCount( context, 1, "lower", true ) )
1169
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1172
QString tmp = args[0]->stringValue().lower();
1173
context.setValue( new KSValue( tmp ) );
1177
static bool kspreadfunc_find( KSContext& context )
1179
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1181
if ( !KSUtil::checkArgumentsCount( context, 2, "find", true ) )
1184
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1187
if ( !KSUtil::checkType( context, args[1], KSValue::StringType, true ) )
1190
QString string_find = args[0]->stringValue();
1191
QString string = args[1]->stringValue();
1192
bool exist=(string.find(string_find)!=-1)?true:false;
1193
context.setValue( new KSValue( exist ) );
1199
static bool kspreadfunc_mid( KSContext& context )
1201
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1203
uint len = 0xffffffff;
1204
if ( KSUtil::checkArgumentsCount( context, 3, "mid", false ) )
1206
if( KSUtil::checkType( context, args[2], KSValue::DoubleType, false ) )
1207
len = (uint) args[2]->doubleValue();
1208
else if( KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1209
len = (uint) args[2]->intValue();
1213
else if ( !KSUtil::checkArgumentsCount( context, 2, "mid", true ) )
1216
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1219
if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1220
pos = (int) args[1]->doubleValue();
1221
else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1222
pos = args[1]->intValue();
1226
QString tmp = args[0]->stringValue().mid( pos, len );
1227
context.setValue( new KSValue(tmp));
1231
static bool kspreadfunc_len( KSContext& context )
1233
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1235
if ( !KSUtil::checkArgumentsCount( context, 1, "len", true ) )
1238
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1241
int nb=args[0]->stringValue().length();
1242
context.setValue( new KSValue(nb));
1246
static bool kspreadfunc_EXACT( KSContext& context )
1248
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1250
if ( !KSUtil::checkArgumentsCount( context, 2, "EXACT", true ) )
1253
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1255
if ( !KSUtil::checkType( context, args[1], KSValue::StringType, true ) )
1257
bool exact = args[1]->stringValue() == args[0]->stringValue();
1258
context.setValue( new KSValue(exact) );
1264
static bool kspreadfunc_INT( KSContext& context )
1266
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1268
if ( !KSUtil::checkArgumentsCount( context, 1, "INT", true ) )
1271
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1273
if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
1277
val=args[0]->doubleValue();
1279
context.setValue( new KSValue((int)val));
1283
static bool kspreadfunc_PI( KSContext& context )
1285
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1287
if ( !KSUtil::checkArgumentsCount( context, 0, "PI", true ) )
1290
context.setValue( new KSValue(M_PI));
1294
static bool kspreadfunc_rand( KSContext& context )
1296
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1298
if ( !KSUtil::checkArgumentsCount( context, 0, "rand", true ) )
1301
context.setValue( new KSValue((double) rand()/(RAND_MAX + 1.0)));
1305
static bool kspreadfunc_randbetween( KSContext& context )
1307
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1309
if ( !KSUtil::checkArgumentsCount( context, 2, "RANDBETWEEN", true ) )
1311
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1313
if( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1315
if(args[0]->intValue()>args[1]->intValue())
1317
context.setValue( new KSValue(i18n("Err")));
1321
context.setValue( new KSValue((double)(((double)args[1]->intValue()-(double)args[0]->intValue())*rand()/RAND_MAX+((double)args[0]->intValue()))));
1327
static bool kspreadfunc_REPT( KSContext& context )
1329
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1331
if ( !KSUtil::checkArgumentsCount( context, 2, "REPT", true ) )
1334
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1336
if( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1339
int nb=(int) args[1]->doubleValue();
1340
QString tmp=args[0]->stringValue();
1342
for (int i=0 ;i<nb;i++)
1344
context.setValue( new KSValue(tmp1));
1348
static bool kspreadfunc_islogic( KSContext& context )
1350
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1352
if ( !KSUtil::checkArgumentsCount( context, 1, "ISLOGIC", true ) )
1355
bool logic = KSUtil::checkType( context, args[0], KSValue::BoolType, true );
1356
context.setValue( new KSValue(logic));
1360
static bool kspreadfunc_istext( KSContext& context )
1362
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1364
if ( !KSUtil::checkArgumentsCount( context, 1, "ISTEXT", true ) )
1367
bool logic = KSUtil::checkType( context, args[0], KSValue::StringType, true );
1368
context.setValue( new KSValue(logic));
1372
static bool kspreadfunc_isnottext( KSContext& context )
1374
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1376
if ( !KSUtil::checkArgumentsCount( context, 1, "ISNOTTEXT", true ) )
1379
bool logic = !KSUtil::checkType( context, args[0], KSValue::StringType, true );
1380
context.setValue( new KSValue(logic));
1384
static bool kspreadfunc_isnum( KSContext& context )
1386
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1388
if ( !KSUtil::checkArgumentsCount( context, 1, "ISNUM", true ) )
1391
bool logic = KSUtil::checkType( context, args[0], KSValue::DoubleType, true );
1392
context.setValue( new KSValue(logic));
1396
static bool kspreadfunc_istime( KSContext& context )
1398
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1400
if ( !KSUtil::checkArgumentsCount( context, 1, "ISTIME", true ) )
1403
bool logic = KSUtil::checkType( context, args[0], KSValue::TimeType, true );
1404
context.setValue( new KSValue(logic));
1408
static bool kspreadfunc_isdate( KSContext& context )
1410
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1412
if ( !KSUtil::checkArgumentsCount( context, 1, "ISDATE", true ) )
1415
bool logic = KSUtil::checkType( context, args[0], KSValue::DateType, true );
1416
context.setValue( new KSValue(logic));
1421
static bool kspreadfunc_pow( KSContext& context )
1423
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1425
if ( !KSUtil::checkArgumentsCount( context, 2, "pow",true ) )
1428
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1431
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1434
context.setValue( new KSValue( pow( args[0]->doubleValue(),args[1]->doubleValue() ) ) );
1439
static bool kspreadfunc_mod( KSContext& context )
1442
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1444
if ( !KSUtil::checkArgumentsCount( context, 2, "MOD",true ) )
1447
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1450
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1452
if( (int)args[1]->doubleValue()!=0)
1454
result=(int)args[0]->doubleValue() % (int)args[1]->doubleValue();
1455
context.setValue( new KSValue( result ) );
1459
context.setValue( new KSValue( i18n("#DIV/0") ) );
1465
static bool kspreadfunc_hours( KSContext& context )
1467
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1468
if ( !KSUtil::checkArgumentsCount( context,1, "hours",true ) )
1470
if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1472
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1474
QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1476
context.setValue(new KSValue(tmp.hour()));
1478
context.setValue(new KSValue(i18n("Err")));
1483
QTime tmp=args[0]->timeValue();
1484
context.setValue(new KSValue(tmp.hour()));
1489
static bool kspreadfunc_minutes( KSContext& context )
1491
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1492
if ( !KSUtil::checkArgumentsCount( context,1, "minutes",true ) )
1494
if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1496
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1498
QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1500
context.setValue(new KSValue(tmp.minute()));
1502
context.setValue(new KSValue(i18n("Err")));
1507
QTime tmp=args[0]->timeValue();
1508
context.setValue(new KSValue(tmp.minute()));
1513
static bool kspreadfunc_seconds( KSContext& context )
1515
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1516
if ( !KSUtil::checkArgumentsCount( context,1, "seconds",true ) )
1518
if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1520
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1522
QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1524
context.setValue(new KSValue(tmp.second()));
1526
context.setValue(new KSValue(i18n("Err")));
1531
QTime tmp=args[0]->timeValue();
1532
context.setValue(new KSValue(tmp.second()));
1537
static bool kspreadfunc_date( KSContext& context )
1539
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1541
if ( !KSUtil::checkArgumentsCount( context,3, "date",true ) )
1544
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1547
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1550
if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1554
if( _date.setYMD(args[0]->intValue(), args[1]->intValue(), args[2]->intValue()) )
1555
context.setValue( new KSValue(KGlobal::locale()->formatDate(_date)));
1557
context.setValue( new KSValue(QString(i18n("Err"))) );
1562
static bool kspreadfunc_day( KSContext& context )
1564
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1566
if ( !KSUtil::checkArgumentsCount( context,1, "day",true ) )
1569
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1572
if(KGlobal::locale()->weekDayName(args[0]->intValue()).isNull())
1575
tmp= KGlobal::locale()->weekDayName(args[0]->intValue());
1577
//context.setValue( new KSValue(KGlobal::locale()->weekDayName(args[0]->intValue())));
1578
context.setValue( new KSValue(tmp));
1582
static bool kspreadfunc_month( KSContext& context )
1584
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1586
if ( !KSUtil::checkArgumentsCount( context,1, "month",true ) )
1589
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1592
if(KGlobal::locale()->monthName(args[0]->intValue()).isNull())
1595
tmp=KGlobal::locale()->monthName(args[0]->intValue());
1597
context.setValue( new KSValue(tmp));
1598
//context.setValue( new KSValue(KGlobal::locale()->monthName(args[0]->intValue())));
1603
static bool kspreadfunc_time( KSContext& context )
1605
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1607
if ( !KSUtil::checkArgumentsCount( context,3, "time",true ) )
1610
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1613
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1616
if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1619
context.setValue( new KSValue(KGlobal::locale()->formatTime(
1620
QTime(args[0]->intValue(),
1621
args[1]->intValue(),args[2]->intValue()),true )));
1626
static bool kspreadfunc_currentDate( KSContext& context )
1628
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1630
if ( !KSUtil::checkArgumentsCount( context,0, "currentDate",true ) )
1633
context.setValue( new KSValue(KGlobal::locale()->formatDate(QDate::currentDate())));
1638
static bool kspreadfunc_shortcurrentDate( KSContext& context )
1640
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1642
if ( !KSUtil::checkArgumentsCount( context,0, "shortcurrentDate",true ) )
1645
context.setValue( new KSValue(KGlobal::locale()->formatDate(QDate::currentDate(),true)));
1651
static bool kspreadfunc_currentTime( KSContext& context )
1653
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1655
if ( !KSUtil::checkArgumentsCount( context,0, "currentTime",true ) )
1658
context.setValue( new KSValue(KGlobal::locale()->formatTime(QTime::currentTime())));
1663
static bool kspreadfunc_currentDateTime( KSContext& context )
1665
// QValueList<KSValue::Ptr>& args = context.value()->listValue();
1667
if ( !KSUtil::checkArgumentsCount( context,0, "currentDateTime",true ) )
1670
context.setValue( new KSValue(KGlobal::locale()->formatDateTime(QDateTime::currentDateTime())));
1675
static bool kspreadfunc_dayOfYear( KSContext& context )
1677
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1679
if ( !KSUtil::checkArgumentsCount( context,3, "dayOfYear",true ) )
1682
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1685
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1688
if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1691
context.setValue( new KSValue(QDate(args[0]->intValue(),
1692
args[1]->intValue(),args[2]->intValue()).dayOfYear() ));
1697
static double fact( double val, double end )
1699
/* fact =i*(i-1)*(i-2)*...*1 */
1706
/*val==end => you don't multiplie it */
1708
return(val*fact((double)(val-1),end));
1712
static bool kspreadfunc_fact( KSContext& context )
1716
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1718
if ( !KSUtil::checkArgumentsCount( context,1, "fact",true ) )
1721
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1724
result=fact((double)args[0]->intValue(),0);
1725
//In fact function val must be positive
1728
context.setValue( new KSValue(tmp));
1730
context.setValue( new KSValue(result ));
1735
static bool kspreadfunc_arrang( KSContext& context )
1736
{ /* arrang : fact(n)/(fact(n-m) */
1739
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1741
if ( !KSUtil::checkArgumentsCount( context,2, "PERMUT",true ) )
1744
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1747
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1751
if((double)args[0]->intValue()<(double)args[1]->intValue())
1752
context.setValue( new KSValue(tmp ));
1754
else if((double)args[1]->intValue()<0)
1755
context.setValue( new KSValue(tmp ));
1759
result=fact((double)args[0]->intValue(),
1760
((double)args[0]->intValue()-(double)args[1]->intValue()));
1761
//In fact function val must be positive
1764
context.setValue( new KSValue(tmp));
1766
context.setValue( new KSValue(result ));
1771
static bool kspreadfunc_combin( KSContext& context )
1772
{ /*combin : fact(n)/(fact(n-m)*fact(m)) */
1775
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1777
if ( !KSUtil::checkArgumentsCount( context,2, "COMBIN",true ) )
1780
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1783
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1787
if((double)args[0]->intValue()<(double)args[1]->intValue())
1788
context.setValue( new KSValue(tmp ));
1790
else if((double)args[1]->intValue()<0)
1791
context.setValue( new KSValue(tmp ));
1795
result=(fact((double)args[0]->intValue(),
1796
((double)args[0]->intValue()-(double)args[1]->intValue()))
1797
/fact((double)args[1]->intValue(),0));
1798
//In fact function val must be positive
1801
context.setValue( new KSValue(tmp));
1803
context.setValue( new KSValue(result ));
1808
static bool kspreadfunc_bino( KSContext& context )
1812
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1814
if ( !KSUtil::checkArgumentsCount( context,3, "BINO",true ) )
1817
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1820
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1823
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1827
if(args[0]->doubleValue()<args[1]->doubleValue())
1828
context.setValue( new KSValue(tmp ));
1830
else if(args[1]->doubleValue()<0)
1831
context.setValue( new KSValue(tmp ));
1834
else if((args[2]->doubleValue()<0)||(args[2]->doubleValue()>1))
1835
context.setValue( new KSValue(tmp ));
1838
result=(fact(args[0]->doubleValue(),
1839
(args[0]->doubleValue()-args[1]->doubleValue()))
1840
/fact(args[1]->doubleValue(),0));
1841
//In fact function val must be positive
1844
context.setValue( new KSValue(tmp));
1847
result=result*pow(args[2]->doubleValue(),(int)args[1]->doubleValue())*
1848
pow((1-args[2]->doubleValue()),((int)args[0]->doubleValue()-
1849
((int)args[1]->doubleValue())));
1850
context.setValue( new KSValue(result ));
1859
static bool kspreadfunc_bino_inv( KSContext& context )
1863
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1865
if ( !KSUtil::checkArgumentsCount( context,3, "INVBINO",true ) )
1868
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1871
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1874
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1878
if(args[0]->doubleValue()<args[1]->doubleValue())
1879
context.setValue( new KSValue(tmp ));
1881
else if(args[1]->doubleValue()<0)
1882
context.setValue( new KSValue(tmp ));
1885
else if((args[2]->doubleValue()<0)||(args[2]->doubleValue()>1))
1886
context.setValue( new KSValue(tmp ));
1889
result=(fact(args[0]->doubleValue(),
1890
(args[0]->doubleValue()-args[1]->doubleValue()))
1891
/fact(args[1]->doubleValue(),0));
1892
//In fact function val must be positive
1895
context.setValue( new KSValue(tmp));
1898
result=result*pow((1-args[2]->doubleValue()),((int)args[0]->doubleValue()-
1899
(int)args[1]->doubleValue()))*pow(args[2]->doubleValue(),(
1900
(int)args[1]->doubleValue()));
1901
context.setValue( new KSValue(result ));
1909
static bool kspreadfunc_fv( KSContext& context )
1911
/* Returns future value, given current value, interest rate and time */
1912
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1914
if ( !KSUtil::checkArgumentsCount( context, 3, "FV", true ) )
1917
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1919
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1921
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1923
double present = args[0]->doubleValue();
1924
double interest = args[1]->doubleValue();
1925
double periods = args[2]->doubleValue();
1928
context.setValue( new KSValue( present * pow(1+interest, periods)));
1932
static bool kspreadfunc_compound( KSContext& context )
1934
/* Returns value after compounded interest, given principal, rate, periods
1935
per year and year */
1936
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1938
if ( !KSUtil::checkArgumentsCount( context, 4, "compound", true ) )
1941
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1943
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1945
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1947
if ( !KSUtil::checkType( context, args[3], KSValue::DoubleType, true ) )
1949
double principal = args[0]->doubleValue();
1950
double interest = args[1]->doubleValue();
1951
double periods = args[2]->doubleValue();
1952
double years = args[3]->doubleValue();
1954
context.setValue( new KSValue( principal * pow(1+(interest/periods),
1960
static bool kspreadfunc_continuous( KSContext& context )
1962
// If you still don't understand this, let me know! ;-) jsinger@leeta.net
1964
/* Returns value after continuous compounding of interest, given prinicpal,
1966
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1968
if ( !KSUtil::checkArgumentsCount( context, 3, "continuous", true ) )
1971
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1973
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1975
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1977
double principal = args[0]->doubleValue();
1978
double interest = args[1]->doubleValue();
1979
double years = args[2]->doubleValue();
1982
context.setValue( new KSValue( principal * exp(interest * years)));
1986
static bool kspreadfunc_pv( KSContext& context )
1988
/* Returns presnt value, given future value, interest rate and years */
1989
QValueList<KSValue::Ptr>& args = context.value()->listValue();
1991
if ( !KSUtil::checkArgumentsCount( context, 3, "PV", true ) )
1994
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1996
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1998
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2000
double future = args[0]->doubleValue();
2001
double interest = args[1]->doubleValue();
2002
double periods = args[2]->doubleValue();
2005
context.setValue( new KSValue( future / pow(1+interest, periods)));
2009
static bool kspreadfunc_pv_annuity( KSContext& context )
2011
/* Returns present value of an annuity or cash flow, given payment,
2013
periods, initial amount and whether payments are made at the start (TRUE)
2014
or end of a period */
2016
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2018
if ( !KSUtil::checkArgumentsCount( context, 3, "PV_annuity", true ) )
2021
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2023
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2025
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2027
double amount = args[0]->doubleValue();
2028
double interest = args[1]->doubleValue();
2029
double periods = args[2]->doubleValue();
2032
result = amount * (1 - 1/(pow( (1+interest), periods ))) / interest ;
2034
context.setValue( new KSValue( result ) );
2040
static bool kspreadfunc_fv_annuity( KSContext& context )
2042
/* Returns future value of an annuity or cash flow, given payment, interest
2045
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2047
if ( !KSUtil::checkArgumentsCount( context, 3, "FV_annuity", true ) )
2050
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2052
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2054
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2056
double amount= args[0]->doubleValue();
2057
double interest = args[1]->doubleValue();
2058
double periods = args[2]->doubleValue();
2062
result = amount * ((pow( (1+interest),periods))/interest - 1/interest) ;
2064
context.setValue( new KSValue( result ) );
2070
static bool kspreadfunc_effective( KSContext& context )
2072
/* Returns effective interest rate given nominal rate and periods per year */
2074
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2076
if ( !KSUtil::checkArgumentsCount( context, 2, "effective", true ) )
2079
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2081
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2083
double nominal = args[0]->doubleValue();
2084
double periods = args[1]->doubleValue();
2086
context.setValue( new KSValue( pow( 1 + (nominal/periods), periods )- 1 ) );
2092
static bool kspreadfunc_zero_coupon( KSContext& context )
2094
/* Returns effective interest rate given nominal rate and periods per year */
2096
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2098
if ( !KSUtil::checkArgumentsCount( context, 3, "zero_coupon", true ) )
2101
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2103
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2105
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2107
double face = args[0]->doubleValue();
2108
double rate = args[1]->doubleValue();
2109
double years = args[2]->doubleValue();
2111
context.setValue( new KSValue( face / pow( (1 + rate), years ) ) );
2117
static bool kspreadfunc_level_coupon( KSContext& context )
2119
/* Returns effective interest rate given nominal rate and periods per year */
2121
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2123
if ( !KSUtil::checkArgumentsCount( context, 5, "level_coupon", true ) )
2126
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2128
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2130
if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2132
if ( !KSUtil::checkType( context, args[3], KSValue::DoubleType, true ) )
2134
if ( !KSUtil::checkType( context, args[4], KSValue::DoubleType, true ) )
2136
double face = args[0]->doubleValue();
2137
double coupon_rate = args[1]->doubleValue();
2138
double coupon_year = args[2]->doubleValue();
2139
double years = args[3]->doubleValue();
2140
double market_rate = args[4]->doubleValue();
2142
double coupon = coupon_rate * face / coupon_year;
2143
double interest = market_rate/coupon_year;
2144
double pv_annuity = (1 - 1/(pow( (1+interest), (years*coupon_year) ))) / interest ;
2145
context.setValue( new KSValue( coupon * pv_annuity + (face/ pow( (1+interest), (years*coupon_year) ) ) ) );
2151
static bool kspreadfunc_nominal( KSContext& context )
2153
/* Returns nominal interest rate given effective rate and periods per year */
2155
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2157
if ( !KSUtil::checkArgumentsCount( context, 2, "nominal", true ) )
2160
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2162
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2164
double effective = args[0]->doubleValue();
2165
double periods = args[1]->doubleValue();
2167
if ( periods == 0.0 ) // Check null
2170
context.setValue( new KSValue( periods * (pow( (effective + 1), (1 / periods) ) -1) ) );
2176
static bool kspreadfunc_sign( KSContext& context )
2179
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2181
if ( !KSUtil::checkArgumentsCount( context, 1, "sign", true ) )
2184
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2186
if ( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
2189
val=args[0]->doubleValue();
2198
context.setValue( new KSValue( value ) );
2203
static bool kspreadfunc_atan2( KSContext& context )
2205
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2207
if ( !KSUtil::checkArgumentsCount( context, 2, "atan2", true ) )
2210
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2212
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2214
context.setValue( new KSValue( atan2( args[1]->doubleValue(),args[0]->doubleValue() ) ) );
2219
static bool kspreadfunc_inv( KSContext& context )
2221
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2223
if ( !KSUtil::checkArgumentsCount( context, 1, "INV",true ) )
2226
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2228
if ( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
2231
val=args[0]->doubleValue();
2234
context.setValue( new KSValue( val*(-1) ) );
2239
static bool kspreadfunc_dec2hex( KSContext& context )
2241
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2243
if ( !KSUtil::checkArgumentsCount( context, 1, "DECHEX", true ) ||!KSUtil::checkArgumentsCount( context, 1, "DEC2HEX", true ))
2246
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2250
tmp=tmp.setNum( args[0]->intValue(),16);
2251
context.setValue( new KSValue( tmp ));
2256
static bool kspreadfunc_dec2oct( KSContext& context )
2258
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2260
if ( !KSUtil::checkArgumentsCount( context, 1, "DEC2OCT", true ) || !KSUtil::checkArgumentsCount( context, 1, "DECOCT", true ))
2263
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2267
tmp=tmp.setNum( args[0]->intValue(),8);
2268
context.setValue( new KSValue( tmp ));
2273
static bool kspreadfunc_dec2bin( KSContext& context )
2275
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2277
if ( !KSUtil::checkArgumentsCount( context, 1, "DEC2BIN", true ) || !KSUtil::checkArgumentsCount( context, 1, "DECBIN", true ))
2280
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2284
tmp=tmp.setNum( args[0]->intValue(),2);
2285
context.setValue( new KSValue( tmp ));
2290
static bool kspreadfunc_bin2dec( KSContext& context )
2292
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2294
if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2DEC", true ) )
2297
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2300
QString tmp=args[0]->stringValue();
2302
long val=tmp.toLong(&ok,2);
2304
context.setValue( new KSValue( QString(i18n("Err") )));
2306
context.setValue( new KSValue(val));
2311
static bool kspreadfunc_bin2oct( KSContext& context )
2313
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2315
if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2OCT", true ) )
2318
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2321
QString tmp=args[0]->stringValue();
2323
long val=tmp.toLong(&ok,2);
2325
context.setValue( new KSValue( QString(i18n("Err") )));
2328
tmp=tmp.setNum(val,8);
2329
context.setValue( new KSValue(tmp));
2335
static bool kspreadfunc_bin2hex( KSContext& context )
2337
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2339
if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2HEX", true ) )
2342
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2345
QString tmp=args[0]->stringValue();
2347
long val=tmp.toLong(&ok,2);
2349
context.setValue( new KSValue( QString(i18n("Err") )));
2352
tmp=tmp.setNum(val,16);
2353
context.setValue( new KSValue(tmp));
2359
static bool kspreadfunc_oct2dec( KSContext& context )
2361
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2363
if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2DEC", true ) )
2366
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2369
QString tmp=args[0]->stringValue();
2371
long val=tmp.toLong(&ok,8);
2373
context.setValue( new KSValue( QString(i18n("Err") )));
2375
context.setValue( new KSValue(val));
2380
static bool kspreadfunc_oct2bin( KSContext& context )
2382
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2384
if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2BIN", true ) )
2387
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2390
QString tmp=args[0]->stringValue();
2392
long val=tmp.toLong(&ok,8);
2394
context.setValue( new KSValue( QString(i18n("Err") )));
2397
tmp=tmp.setNum(val,2);
2398
context.setValue( new KSValue(tmp));
2404
static bool kspreadfunc_oct2hex( KSContext& context )
2406
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2408
if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2HEX", true ) )
2411
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2414
QString tmp=args[0]->stringValue();
2416
long val=tmp.toLong(&ok,8);
2418
context.setValue( new KSValue( QString(i18n("Err") )));
2421
tmp=tmp.setNum(val,16);
2422
context.setValue( new KSValue(tmp));
2428
static bool kspreadfunc_hex2dec( KSContext& context )
2430
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2432
if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2DEC", true ) )
2435
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2438
QString tmp=args[0]->stringValue();
2440
long val=tmp.toLong(&ok,16);
2442
context.setValue( new KSValue( QString(i18n("Err") )));
2444
context.setValue( new KSValue(val));
2449
static bool kspreadfunc_hex2bin( KSContext& context )
2451
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2453
if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2BIN", true ) )
2456
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2459
QString tmp=args[0]->stringValue();
2461
long val=tmp.toLong(&ok,16);
2463
context.setValue( new KSValue( QString(i18n("Err") )));
2466
tmp=tmp.setNum(val,2);
2467
context.setValue( new KSValue(tmp));
2473
static bool kspreadfunc_hex2oct( KSContext& context )
2475
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2477
if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2OCT", true ) )
2480
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2483
QString tmp=args[0]->stringValue();
2485
long val=tmp.toLong(&ok,16);
2487
context.setValue( new KSValue( QString(i18n("Err") )));
2490
tmp=tmp.setNum(val,8);
2491
context.setValue( new KSValue(tmp));
2498
static bool kspreadfunc_rounddown( KSContext& context )
2500
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2503
if ( !KSUtil::checkArgumentsCount( context, 2, "ROUNDDOWN", true ) )
2505
//just 1 argument => number of decimal =0 by default
2506
if ( !KSUtil::checkArgumentsCount( context, 1, "ROUNDDOWN", true ) )
2508
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2514
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2516
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2518
digits=args[1]->intValue();
2520
result=floor(args[0]->doubleValue()*pow(10.0,digits))/pow(10.0,digits);
2521
context.setValue( new KSValue( result) );
2526
static bool kspreadfunc_roundup( KSContext& context )
2528
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2531
if ( !KSUtil::checkArgumentsCount( context, 2, "ROUNDUP", true ) )
2533
//just 1 argument => number of decimal =0 by default
2534
if ( !KSUtil::checkArgumentsCount( context, 1, "ROUNDUP", true ) )
2536
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2542
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2544
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2546
digits=args[1]->intValue();
2548
// This is not correct solution for problem with floating point numbers and probably
2549
// will fail in platforms where float and double lenghts are same.
2550
if (approx_equal(floor(args[0]->doubleValue()*pow(10,digits)), args[0]->doubleValue()*pow(10,digits)))
2551
result = args[0]->doubleValue();
2553
result=floor(args[0]->doubleValue()*pow(10,digits)+1)/pow(10,digits);
2554
context.setValue( new KSValue( result) );
2559
static bool kspreadfunc_round( KSContext& context )
2561
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2564
if ( !KSUtil::checkArgumentsCount( context, 2, "ROUND", true ) )
2566
//just 1 argument => number of decimal =0 by default
2567
if ( !KSUtil::checkArgumentsCount( context, 1, "ROUND", true ) )
2569
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2575
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2577
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2579
digits=args[1]->intValue();
2581
result=floor(args[0]->doubleValue()*pow(10.0,digits)+0.5)/pow(10.0,digits);
2582
context.setValue( new KSValue( result) );
2587
static QString kspreadfunc_create_complex( double real,double imag )
2592
return KGlobal::locale()->formatNumber( real);
2595
tmp=KGlobal::locale()->formatNumber(real);
2597
return KGlobal::locale()->formatNumber(imag)+"i";
2599
tmp=tmp+"+"+KGlobal::locale()->formatNumber(imag)+"i";
2601
tmp=tmp+KGlobal::locale()->formatNumber(imag)+"i";
2606
static bool kspreadfunc_complex( KSContext& context )
2608
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2610
if ( !KSUtil::checkArgumentsCount( context,2, "COMPLEX",true ) )
2613
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2616
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2618
if(args[1]->doubleValue() ==0)
2620
context.setValue( new KSValue(args[0]->doubleValue()));
2623
QString tmp=kspreadfunc_create_complex(args[0]->doubleValue(),args[1]->doubleValue());
2625
double result=KGlobal::locale()->readNumber(tmp, &ok);
2628
context.setValue( new KSValue(result));
2631
context.setValue( new KSValue(tmp));
2637
static double imag_complexe(QString str, bool &ok)
2640
if(tmp.find('i')==-1)
2645
else if( tmp.length()==1)
2651
else if( tmp.length()==2 )
2655
if((pos1=tmp.find('+'))!=-1&& pos1==0)
2660
else if( (pos1=tmp.find('-'))!=-1 && pos1==0 )
2665
else if(tmp[0].isDigit())
2668
return KGlobal::locale()->readNumber(tmp.left(1));
2679
if((pos1=tmp.find('i'))!=-1)
2684
if((pos2=tmp.findRev('+'))!=-1 && pos2!=0)
2693
tmpStr=tmp.mid(pos2,(pos1-pos2));
2694
val=KGlobal::locale()->readNumber(tmpStr, &ok);
2700
else if( (pos2=tmp.findRev('-'))!=-1&& pos2!=0)
2709
tmpStr=tmp.mid(pos2,(pos1-pos2));
2710
val=KGlobal::locale()->readNumber(tmpStr, &ok);
2718
tmpStr=tmp.left(pos1);
2719
val=KGlobal::locale()->readNumber(tmpStr, &ok);
2730
static bool kspreadfunc_complex_imag( KSContext& context )
2732
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2734
if ( !KSUtil::checkArgumentsCount( context,1, "IMAGINARY",true ) )
2737
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2739
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2741
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
2745
tmp=args[0]->stringValue();
2748
double result=imag_complexe(tmp, good);
2750
context.setValue( new KSValue(result));
2752
context.setValue( new KSValue(i18n("Err")));
2758
static double real_complexe(QString str, bool &ok)
2764
if((pos1=tmp.find('i'))==-1)
2766
val=KGlobal::locale()->readNumber(tmp, &ok);
2773
if((pos2=tmp.findRev('-'))!=-1 && pos2!=0)
2775
tmpStr=tmp.left(pos2);
2776
val=KGlobal::locale()->readNumber(tmpStr, &ok);
2781
else if((pos2=tmp.findRev('+'))!=-1)
2783
tmpStr=tmp.left(pos2);
2784
val=KGlobal::locale()->readNumber(tmpStr, &ok);
2800
static bool kspreadfunc_complex_real( KSContext& context )
2802
QValueList<KSValue::Ptr>& args = context.value()->listValue();
2804
if ( !KSUtil::checkArgumentsCount( context,1, "IMREAL",true ) )
2807
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2809
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2811
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
2814
tmp=args[0]->stringValue();
2816
double result=real_complexe(tmp, good);
2818
context.setValue( new KSValue(result));
2820
context.setValue( new KSValue(i18n("Err")));
2826
static bool kspreadfunc_imsum_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2828
QValueList<KSValue::Ptr>::Iterator it = args.begin();
2829
QValueList<KSValue::Ptr>::Iterator end = args.end();
2831
for( ; it != end; ++it )
2833
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2835
if ( !kspreadfunc_imsum_helper( context, (*it)->listValue(), result ) )
2838
else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2840
double imag,real,imag1,real1;
2842
imag=imag_complexe(result, ok);
2843
real=real_complexe(result, ok);
2844
imag1=imag_complexe((*it)->stringValue(), ok);
2845
real1=real_complexe((*it)->stringValue(), ok);
2846
result=kspreadfunc_create_complex(real+real1,imag+imag1);
2848
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2850
double imag,real,imag1,real1;
2852
imag=imag_complexe(result, ok);
2853
real=real_complexe(result, ok);
2855
real1=(*it)->doubleValue();
2856
result=kspreadfunc_create_complex(real+real1,imag+imag1);
2865
static bool kspreadfunc_imsum( KSContext& context )
2868
bool b = kspreadfunc_imsum_helper( context, context.value()->listValue(), result );
2871
double val=KGlobal::locale()->readNumber(result, &ok);
2873
context.setValue( new KSValue( val ) );
2875
context.setValue( new KSValue( result ) );
2880
static bool kspreadfunc_imsub_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2882
QValueList<KSValue::Ptr>::Iterator it = args.begin();
2883
QValueList<KSValue::Ptr>::Iterator end = args.end();
2885
for( ; it != end; ++it )
2887
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2889
if ( !kspreadfunc_imsub_helper( context, (*it)->listValue(), result ) )
2892
else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2894
double imag,real,imag1,real1;
2896
if(!result.isEmpty())
2898
imag=imag_complexe(result, ok);
2899
real=real_complexe(result, ok);
2900
imag1=imag_complexe((*it)->stringValue(), ok);
2901
real1=real_complexe((*it)->stringValue(), ok);
2902
result=kspreadfunc_create_complex(real-real1,imag-imag1);
2906
imag1=imag_complexe((*it)->stringValue(), ok);
2907
real1=real_complexe((*it)->stringValue(), ok);
2908
result=kspreadfunc_create_complex(real1,imag1);
2911
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2913
double imag,real,imag1,real1;
2915
imag=imag_complexe(result, ok);
2916
real=real_complexe(result, ok);
2918
real1=(*it)->doubleValue();
2919
if(!result.isEmpty())
2920
result=kspreadfunc_create_complex(real-real1,imag-imag1);
2922
result=kspreadfunc_create_complex(real1,imag1);
2931
static bool kspreadfunc_imsub( KSContext& context )
2934
bool b = kspreadfunc_imsub_helper( context, context.value()->listValue(), result );
2937
double val=KGlobal::locale()->readNumber(result, &ok);
2939
context.setValue( new KSValue( val ) );
2941
context.setValue( new KSValue( result ) );
2947
static bool kspreadfunc_improduct_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2949
QValueList<KSValue::Ptr>::Iterator it = args.begin();
2950
QValueList<KSValue::Ptr>::Iterator end = args.end();
2952
for( ; it != end; ++it )
2954
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2956
if ( !kspreadfunc_improduct_helper( context, (*it)->listValue(), result ) )
2959
else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2961
double imag,real,imag1,real1;
2963
if(!result.isEmpty())
2965
imag=imag_complexe(result, ok);
2966
real=real_complexe(result, ok);
2967
imag1=imag_complexe((*it)->stringValue(), ok);
2968
real1=real_complexe((*it)->stringValue(), ok);
2969
result=kspreadfunc_create_complex(real*real1+(imag*imag1)*-1,real*imag1+real1*imag);
2973
imag1=imag_complexe((*it)->stringValue(), ok);
2974
real1=real_complexe((*it)->stringValue(), ok);
2975
result=kspreadfunc_create_complex(real1,imag1);
2978
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2980
double imag,real,imag1,real1;
2982
imag=imag_complexe(result, ok);
2983
real=real_complexe(result, ok);
2985
real1=(*it)->doubleValue();
2986
if(!result.isEmpty())
2987
result=kspreadfunc_create_complex(real*real1+(imag*imag1)*-1,real*imag1+real1*imag);
2989
result=kspreadfunc_create_complex(real1,imag1);
2998
static bool kspreadfunc_improduct( KSContext& context )
3001
bool b = kspreadfunc_improduct_helper( context, context.value()->listValue(), result );
3004
double val=KGlobal::locale()->readNumber(result, &ok);
3006
context.setValue( new KSValue( val ) );
3008
context.setValue( new KSValue( result ) );
3014
static bool kspreadfunc_imconjugate( KSContext& context )
3016
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3018
if ( !KSUtil::checkArgumentsCount( context,1, "IMCONJUGATE",true ) )
3021
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3023
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3025
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3029
tmp=args[0]->stringValue();
3032
double real=real_complexe(tmp,ok);
3035
context.setValue( new KSValue(i18n("Err")));
3038
double imag=imag_complexe(tmp,ok);
3041
context.setValue( new KSValue(i18n("Err")));
3044
tmp=kspreadfunc_create_complex(real,-imag);
3046
double result=KGlobal::locale()->readNumber(tmp, &ok);
3049
context.setValue( new KSValue(result));
3052
context.setValue( new KSValue(tmp));
3057
static bool kspreadfunc_imargument( KSContext& context )
3059
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3061
if ( !KSUtil::checkArgumentsCount( context,1, "IMARGUMENT",true ) )
3064
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3066
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3068
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3072
tmp=args[0]->stringValue();
3075
double real=real_complexe(tmp,ok);
3078
context.setValue( new KSValue(i18n("Err")));
3081
double imag=imag_complexe(tmp,ok);
3084
context.setValue( new KSValue(i18n("Err")));
3089
context.setValue( new KSValue(i18n("#Div/0")));
3092
double arg=atan2(imag,real);
3094
context.setValue( new KSValue(arg));
3099
static bool kspreadfunc_imabs( KSContext& context )
3101
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3103
if ( !KSUtil::checkArgumentsCount( context,1, "IMABS",true ) )
3106
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3108
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3110
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3114
tmp=args[0]->stringValue();
3117
double real=real_complexe(tmp,ok);
3120
context.setValue( new KSValue(i18n("Err")));
3123
double imag=imag_complexe(tmp,ok);
3126
context.setValue( new KSValue(i18n("Err")));
3129
double arg=sqrt(pow(imag,2)+pow(real,2));
3131
context.setValue( new KSValue(arg));
3136
static bool kspreadfunc_imcos( KSContext& context )
3138
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3140
if ( !KSUtil::checkArgumentsCount( context,1, "IMCOS",true ) )
3143
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3145
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3147
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3151
tmp=args[0]->stringValue();
3154
double real=real_complexe(tmp,ok);
3157
context.setValue( new KSValue(i18n("Err")));
3160
double imag=imag_complexe(tmp,ok);
3163
context.setValue( new KSValue(i18n("Err")));
3166
double imag_res=sin(real)*sinh(imag);
3167
double real_res=cos(real)*cosh(imag);
3170
tmp=kspreadfunc_create_complex(real_res,-imag_res);
3172
double result=KGlobal::locale()->readNumber(tmp, &ok);
3175
context.setValue( new KSValue(result));
3178
context.setValue( new KSValue(tmp));
3183
static bool kspreadfunc_imsin( KSContext& context )
3185
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3187
if ( !KSUtil::checkArgumentsCount( context,1, "IMSIN",true ) )
3190
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3192
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3194
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3198
tmp=args[0]->stringValue();
3201
double real=real_complexe(tmp,ok);
3204
context.setValue( new KSValue(i18n("Err")));
3207
double imag=imag_complexe(tmp,ok);
3210
context.setValue( new KSValue(i18n("Err")));
3213
double imag_res=cos(real)*sinh(imag);
3214
double real_res=sin(real)*cosh(imag);
3217
tmp=kspreadfunc_create_complex(real_res,imag_res);
3219
double result=KGlobal::locale()->readNumber(tmp, &ok);
3222
context.setValue( new KSValue(result));
3225
context.setValue( new KSValue(tmp));
3230
static bool kspreadfunc_imln( KSContext& context )
3232
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3234
if ( !KSUtil::checkArgumentsCount( context,1, "IMLN",true ) )
3237
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3239
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3241
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3245
tmp=args[0]->stringValue();
3248
double real=real_complexe(tmp,ok);
3251
context.setValue( new KSValue(i18n("Err")));
3254
double imag=imag_complexe(tmp,ok);
3257
context.setValue( new KSValue(i18n("Err")));
3262
double arg=sqrt(pow(imag,2)+pow(real,2));
3263
double real_res=log(arg);
3264
double imag_res=atan(imag/real);
3265
tmp=kspreadfunc_create_complex(real_res,imag_res);
3267
double result=KGlobal::locale()->readNumber(tmp, &ok);
3270
context.setValue( new KSValue(result));
3273
context.setValue( new KSValue(tmp));
3277
static bool kspreadfunc_imexp( KSContext& context )
3279
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3281
if ( !KSUtil::checkArgumentsCount( context,1, "IMEXP",true ) )
3284
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3286
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3288
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3292
tmp=args[0]->stringValue();
3295
double real=real_complexe(tmp,ok);
3298
context.setValue( new KSValue(i18n("Err")));
3301
double imag=imag_complexe(tmp,ok);
3304
context.setValue( new KSValue(i18n("Err")));
3307
double imag_res=exp(real)*sin(imag);
3308
double real_res=exp(real)*cos(imag);
3311
tmp=kspreadfunc_create_complex(real_res,imag_res);
3313
double result=KGlobal::locale()->readNumber(tmp, &ok);
3316
context.setValue( new KSValue(result));
3319
context.setValue( new KSValue(tmp));
3324
static bool kspreadfunc_imsqrt( KSContext& context )
3326
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3328
if ( !KSUtil::checkArgumentsCount( context,1, "IMSQRT",true ) )
3331
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3333
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3335
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3339
tmp=args[0]->stringValue();
3342
double real=real_complexe(tmp,ok);
3345
context.setValue( new KSValue(i18n("Err")));
3348
double imag=imag_complexe(tmp,ok);
3351
context.setValue( new KSValue(i18n("Err")));
3354
double arg=sqrt(sqrt(pow(imag,2)+pow(real,2)));
3355
double angle=atan(imag/real);
3357
double real_res=arg*cos((angle/2));
3358
double imag_res=arg*sin((angle/2));
3360
tmp=kspreadfunc_create_complex(real_res,imag_res);
3362
double result=KGlobal::locale()->readNumber(tmp, &ok);
3365
context.setValue( new KSValue(result));
3368
context.setValue( new KSValue(tmp));
3373
static bool kspreadfunc_impower( KSContext& context )
3375
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3377
if ( !KSUtil::checkArgumentsCount( context,2, "IMPOWER",true ) )
3380
if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3382
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3384
tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3388
tmp=args[0]->stringValue();
3390
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
3394
double real=real_complexe(tmp,ok);
3397
context.setValue( new KSValue(i18n("Err")));
3400
double imag=imag_complexe(tmp,ok);
3403
context.setValue( new KSValue(i18n("Err")));
3407
double arg=pow(sqrt(pow(imag,2)+pow(real,2)),args[1]->intValue());
3408
double angle=atan(imag/real);
3410
double real_res=arg*cos(angle*args[1]->intValue());
3411
double imag_res=arg*sin(angle*args[1]->intValue());
3413
tmp=kspreadfunc_create_complex(real_res,imag_res);
3415
double result=KGlobal::locale()->readNumber(tmp, &ok);
3418
context.setValue( new KSValue(result));
3421
context.setValue( new KSValue(tmp));
3427
static bool kspreadfunc_imdiv_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
3429
QValueList<KSValue::Ptr>::Iterator it = args.begin();
3430
QValueList<KSValue::Ptr>::Iterator end = args.end();
3432
for( ; it != end; ++it )
3434
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3436
if ( !kspreadfunc_imdiv_helper( context, (*it)->listValue(), result ) )
3439
else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
3441
double imag,real,imag1,real1;
3443
if(!result.isEmpty())
3445
imag=imag_complexe(result, ok);
3446
real=real_complexe(result, ok);
3447
imag1=imag_complexe((*it)->stringValue(), ok);
3448
real1=real_complexe((*it)->stringValue(), ok);
3449
result=kspreadfunc_create_complex((real*real1+imag*imag1)/(real1*real1+imag1*imag1),(real1*imag-real*imag1)/(real1*real1+imag1*imag1));
3453
imag1=imag_complexe((*it)->stringValue(), ok);
3454
real1=real_complexe((*it)->stringValue(), ok);
3455
result=kspreadfunc_create_complex(real1,imag1);
3458
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
3460
double imag,real,imag1,real1;
3462
imag=imag_complexe(result, ok);
3463
real=real_complexe(result, ok);
3465
real1=(*it)->doubleValue();
3466
if(!result.isEmpty())
3467
result=kspreadfunc_create_complex((real*real1+imag*imag1)/(real1*real1+imag1*imag1),(real1*imag-real*imag1)/(real1*real1+imag1*imag1));
3469
result=kspreadfunc_create_complex(real1,imag1);
3478
static bool kspreadfunc_imdiv( KSContext& context )
3481
bool b = kspreadfunc_imdiv_helper( context, context.value()->listValue(), result );
3484
double val=KGlobal::locale()->readNumber(result, &ok);
3486
context.setValue( new KSValue( val ) );
3488
context.setValue( new KSValue( result ) );
3495
static bool kspreadfunc_polr( KSContext& context )
3497
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3499
if ( !KSUtil::checkArgumentsCount( context,2, "POLR",true ) )
3502
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3505
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3507
double result=sqrt(pow(args[0]->doubleValue(),2)+pow(args[1]->doubleValue(),2));
3508
context.setValue( new KSValue(result));
3513
static bool kspreadfunc_pola( KSContext& context )
3515
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3517
if ( !KSUtil::checkArgumentsCount( context,2, "POLA",true ) )
3520
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3523
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3525
double result=acos(args[0]->doubleValue()/(sqrt(pow(args[0]->doubleValue(),2)+pow(args[1]->doubleValue(),2))));
3526
context.setValue( new KSValue(result));
3531
static bool kspreadfunc_carx( KSContext& context )
3533
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3535
if ( !KSUtil::checkArgumentsCount( context,2, "CARX",true ) )
3538
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3541
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3543
double result=args[0]->doubleValue()*cos(args[1]->doubleValue());
3544
context.setValue( new KSValue(result));
3549
static bool kspreadfunc_cary( KSContext& context )
3551
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3553
if ( !KSUtil::checkArgumentsCount( context,2, "CARY",true ) )
3556
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3559
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3561
double result=args[0]->doubleValue()*sin(args[1]->doubleValue());
3562
context.setValue( new KSValue(result));
3568
static bool kspreadfunc_sumproduct_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3570
QValueList<KSValue::Ptr>::Iterator it = list.begin();
3571
QValueList<KSValue::Ptr>::Iterator end = list.end();
3572
QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3573
QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3575
for( ; it != end,it2!=end2; ++it,++it2 )
3577
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3579
if ( !kspreadfunc_sumproduct_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3582
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3584
result +=( (*it)->doubleValue()*(*it2)->doubleValue());
3586
else if (!( KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3593
static bool kspreadfunc_sumproduct( KSContext& context )
3595
double result = 0.0;
3596
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3597
if ( !KSUtil::checkArgumentsCount( context, 2, "SUMPRODUCT", true ) )
3600
if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3602
if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3604
if(args[0]->listValue().count() !=args[1]->listValue() .count())
3606
context.setValue( new KSValue( i18n("Err") ) );
3609
bool b = kspreadfunc_sumproduct_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3612
context.setValue( new KSValue( result ) );
3617
static bool kspreadfunc_sumx2py2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3619
QValueList<KSValue::Ptr>::Iterator it = list.begin();
3620
QValueList<KSValue::Ptr>::Iterator end = list.end();
3621
QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3622
QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3624
for( ; it != end,it2!=end2; ++it,++it2 )
3626
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3628
if ( !kspreadfunc_sumx2py2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3631
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3633
result +=( pow((*it)->doubleValue(),2)+pow((*it2)->doubleValue(),2));
3635
else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3642
static bool kspreadfunc_sumx2py2( KSContext& context )
3644
double result = 0.0;
3645
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3646
if ( !KSUtil::checkArgumentsCount( context, 2, "SUMX2PY2", true ) )
3649
if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3651
if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3653
if(args[0]->listValue().count() !=args[1]->listValue() .count())
3655
context.setValue( new KSValue( i18n("Err") ) );
3658
bool b = kspreadfunc_sumx2py2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3661
context.setValue( new KSValue( result ) );
3667
static bool kspreadfunc_sumx2my2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3669
QValueList<KSValue::Ptr>::Iterator it = list.begin();
3670
QValueList<KSValue::Ptr>::Iterator end = list.end();
3671
QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3672
QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3674
for( ; it != end,it2!=end2; ++it,++it2 )
3676
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3678
if ( !kspreadfunc_sumx2my2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3681
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3683
result +=( pow((*it)->doubleValue(),2)-pow((*it2)->doubleValue(),2));
3685
else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3692
static bool kspreadfunc_sumx2my2( KSContext& context )
3694
double result = 0.0;
3695
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3696
if ( !KSUtil::checkArgumentsCount( context, 2, "SUMX2MY2", true ) )
3699
if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3701
if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3703
if(args[0]->listValue().count() !=args[1]->listValue() .count())
3705
context.setValue( new KSValue( i18n("Err") ) );
3708
bool b = kspreadfunc_sumx2my2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3711
context.setValue( new KSValue( result ) );
3716
static bool kspreadfunc_sumxmy2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3718
QValueList<KSValue::Ptr>::Iterator it = list.begin();
3719
QValueList<KSValue::Ptr>::Iterator end = list.end();
3720
QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3721
QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3723
for( ; it != end,it2!=end2; ++it,++it2 )
3725
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3727
if ( !kspreadfunc_sumxmy2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3730
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3732
result +=pow(( (*it)->doubleValue()-(*it2)->doubleValue()),2);
3734
else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3741
static bool kspreadfunc_sumxmy2( KSContext& context )
3743
double result = 0.0;
3744
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3745
if ( !KSUtil::checkArgumentsCount( context, 2, "SUM2XMY", true ) )
3748
if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3750
if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3752
if(args[0]->listValue().count() !=args[1]->listValue() .count())
3754
context.setValue( new KSValue( i18n("Err") ) );
3757
bool b = kspreadfunc_sumxmy2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3760
context.setValue( new KSValue( result ) );
3766
static bool kspreadfunc_delta( KSContext& context )
3768
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3770
if ( !KSUtil::checkArgumentsCount( context,2, "DELTA",true ) )
3773
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3775
if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3777
if(approx_equal(args[0]->doubleValue(), args[1]->doubleValue()))
3781
context.setValue( new KSValue(result));
3786
static bool kspreadfunc_even( KSContext& context )
3788
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3789
if ( !KSUtil::checkArgumentsCount( context,1, "EVEN",true ) )
3792
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3795
double val=args[0]->doubleValue();
3802
if (approx_equal(val, floor(val)))
3804
double valsup=ceil( val );
3805
if(fmod(valsup,2.0)==0)
3808
result=(int)(sign*(valsup+2));
3810
result=(int)(sign*valsup);
3814
result=(int)(sign*(valsup+1));
3816
context.setValue( new KSValue(result));
3821
static bool kspreadfunc_odd( KSContext& context )
3823
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3824
if ( !KSUtil::checkArgumentsCount( context,1, "ODD",true ) )
3827
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3832
double val= args[0]->doubleValue();
3838
if (approx_equal(val, floor(val)))
3841
if (fmod(valsup, 2.0) == 1)
3844
result=(int) (sign * (valsup + 2));
3846
result=(int) (sign * valsup);
3849
result=(int) (sign * (valsup + 1));
3851
context.setValue( new KSValue(result));
3856
static bool kspreadfunc_isodd( KSContext& context )
3858
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3859
if ( !KSUtil::checkArgumentsCount( context,1, "ISODD",true ) )
3862
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3866
//it's a integer => test if it's an odd integer
3867
if(args[0]->intValue() & 1)
3873
context.setValue( new KSValue(result));
3878
static bool kspreadfunc_iseven( KSContext& context )
3880
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3881
if ( !KSUtil::checkArgumentsCount( context,1, "ISEVEN",true ) )
3884
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3888
//it's a integer => test if it's an even integer
3889
if(args[0]->intValue() & 1)
3895
context.setValue( new KSValue(result));
3900
static bool isLeapYear_helper(int _year)
3902
return (((_year % 4) == 0) && ((_year % 100) != 0) || ((_year % 400) == 0));
3905
static bool kspreadfunc_isLeapYear ( KSContext& context )
3907
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3908
if ( !KSUtil::checkArgumentsCount( context,1,"isLeapYear",true ) )
3911
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3916
int nYear = args[0]->intValue();
3917
result = isLeapYear_helper(nYear);
3920
context.setValue( new KSValue(result));
3925
static bool kspreadfunc_daysInYear ( KSContext& context )
3927
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3928
if ( !KSUtil::checkArgumentsCount( context,1,"daysInYear",true ) )
3930
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3933
int nYear = args[0]->intValue();
3934
bool leap = isLeapYear_helper(nYear);
3942
context.setValue( new KSValue(result));
3947
static bool kspreadfunc_weeksInYear( KSContext& context )
3949
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3950
if ( !KSUtil::checkArgumentsCount( context,1,"weeksInYear",true ) )
3952
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3955
int nYear = args[0]->intValue();
3957
QDate _date(nYear, 1, 1);
3958
int nJan1DayOfWeek = _date.dayOfWeek(); //first day of the year
3960
if ( nJan1DayOfWeek == 4 ) { // Thursday
3962
} else if ( nJan1DayOfWeek == 3 ) { // Wednesday
3963
result = isLeapYear_helper(nYear) ? 53 : 52 ;
3968
context.setValue( new KSValue(result));
3973
static bool kspreadfunc_daysInMonth( KSContext& context )
3975
QValueList<KSValue::Ptr>& args = context.value()->listValue();
3976
if ( !KSUtil::checkArgumentsCount( context,2,"daysInMonth",true ) )
3979
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3981
context.setValue( new KSValue( i18n("Err") ) );
3984
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
3986
context.setValue( new KSValue( i18n("Err") ) );
3990
static uint aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
3991
int nYear = args[0]->intValue();
3992
int nMonth = args[1]->intValue();
3996
result = aDaysInMonth[nMonth-1];
3999
if (isLeapYear_helper(nYear))
4000
result = aDaysInMonth[nMonth-1] + 1;
4002
result = aDaysInMonth[nMonth-1];
4005
context.setValue( new KSValue(result));
4011
static bool kspreadfunc_count_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result)
4013
QValueList<KSValue::Ptr>::Iterator it = args.begin();
4014
QValueList<KSValue::Ptr>::Iterator end = args.end();
4016
for( ; it != end; ++it )
4018
if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
4021
if ( !kspreadfunc_count_helper( context, (*it)->listValue(), result ) )
4024
else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
4033
static bool kspreadfunc_count( KSContext& context )
4035
double result = 0.0;
4037
bool b = kspreadfunc_count_helper( context, context.value()->listValue(), result );
4040
context.setValue( new KSValue( result ) );
4046
static bool kspreadfunc_decsex( KSContext& context )
4048
QValueList<KSValue::Ptr>& args = context.value()->listValue();
4049
if ( !KSUtil::checkArgumentsCount( context,1, "DECSEX",true ) )
4052
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
4055
double val=args[0]->doubleValue();
4056
int hours,minutes,second;
4061
hours=inter*(int)(fabs(val));
4062
minutes=(int)(60*val-60*(int)(val));
4063
second=(int)(3600*val-3600*(int)(val)-60*(int)(60*val-60*(int)(val)));
4064
QTime _time(hours,minutes,second);
4065
context.setValue( new KSValue(_time));
4070
static bool kspreadfunc_sexdec( KSContext& context )
4072
QValueList<KSValue::Ptr>& args = context.value()->listValue();
4074
if ( !KSUtil::checkArgumentsCount( context,3, "SEXDEC",true ) )
4076
if ( !KSUtil::checkArgumentsCount( context,1, "SEXDEC",true ) )
4078
if ( !KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
4081
result=args[0]->timeValue().hour()+(double)args[0]->timeValue().minute()/60.0+(double)args[0]->timeValue().second()/3600.0;
4083
context.setValue( new KSValue(result));
4087
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
4089
if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
4091
if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
4093
result=args[0]->intValue()+(double)args[1]->intValue()/60.0+(double)args[2]->intValue()/3600.0;
4095
context.setValue( new KSValue(result));
4101
static bool kspreadfunc_roman( KSContext& context )
4103
const QCString RNUnits[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
4104
const QCString RNTens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
4105
const QCString RNHundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
4106
const QCString RNThousands[] = {"", "M", "MM", "MMM"};
4109
QValueList<KSValue::Ptr>& args = context.value()->listValue();
4110
if ( !KSUtil::checkArgumentsCount( context,1, "ROMAN",true ) )
4113
if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
4115
if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
4118
value=(int)args[0]->doubleValue();
4121
value=(int)args[0]->intValue();
4124
context.setValue( new KSValue(i18n("Err")));
4129
context.setValue( new KSValue(i18n("Value too big")));
4134
result= QString::fromLatin1( RNThousands[ ( value / 1000 ) ] +
4135
RNHundreds[ ( value / 100 ) % 10 ] +
4136
RNTens[ ( value / 10 ) % 10 ] +
4137
RNUnits[ ( value ) % 10 ] );
4138
context.setValue( new KSValue(result));
4142
144
static bool kspreadfunc_cell( KSContext& context )