KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1145 lines
32KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 1998-2002 The KSpread Team
  3. www.koffice.org/kspread
  4. Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public License
  14. along with this library; see the file COPYING.LIB. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. */
  18. // built-in math functions
  19. #include <kdebug.h>
  20. #include <klocale.h>
  21. #include "functions.h"
  22. #include "valuecalc.h"
  23. #include "valueconverter.h"
  24. // these two are needed for SUBTOTAL:
  25. #include "kspread_cell.h"
  26. #include "kspread_sheet.h"
  27. // needed for RANDBINOM and so
  28. #include <math.h>
  29. using namespace KSpread;
  30. // RANDBINOM and RANDNEGBINOM won't support arbitrary precision
  31. // prototypes
  32. Value func_abs (valVector args, ValueCalc *calc, FuncExtra *);
  33. Value func_ceil (valVector args, ValueCalc *calc, FuncExtra *);
  34. Value func_ceiling (valVector args, ValueCalc *calc, FuncExtra *);
  35. Value func_count (valVector args, ValueCalc *calc, FuncExtra *);
  36. Value func_counta (valVector args, ValueCalc *calc, FuncExtra *);
  37. Value func_countblank (valVector args, ValueCalc *calc, FuncExtra *);
  38. Value func_countif (valVector args, ValueCalc *calc, FuncExtra *);
  39. Value func_cur (valVector args, ValueCalc *calc, FuncExtra *);
  40. Value func_div (valVector args, ValueCalc *calc, FuncExtra *);
  41. Value func_eps (valVector args, ValueCalc *calc, FuncExtra *);
  42. Value func_even (valVector args, ValueCalc *calc, FuncExtra *);
  43. Value func_exp (valVector args, ValueCalc *calc, FuncExtra *);
  44. Value func_fact (valVector args, ValueCalc *calc, FuncExtra *);
  45. Value func_factdouble (valVector args, ValueCalc *calc, FuncExtra *);
  46. Value func_fib (valVector args, ValueCalc *calc, FuncExtra *);
  47. Value func_floor (valVector args, ValueCalc *calc, FuncExtra *);
  48. Value func_gcd (valVector args, ValueCalc *calc, FuncExtra *);
  49. Value func_int (valVector args, ValueCalc *calc, FuncExtra *);
  50. Value func_inv (valVector args, ValueCalc *calc, FuncExtra *);
  51. Value func_kproduct (valVector args, ValueCalc *calc, FuncExtra *);
  52. Value func_lcm (valVector args, ValueCalc *calc, FuncExtra *);
  53. Value func_ln (valVector args, ValueCalc *calc, FuncExtra *);
  54. Value func_log2 (valVector args, ValueCalc *calc, FuncExtra *);
  55. Value func_log10 (valVector args, ValueCalc *calc, FuncExtra *);
  56. Value func_logn (valVector args, ValueCalc *calc, FuncExtra *);
  57. Value func_max (valVector args, ValueCalc *calc, FuncExtra *);
  58. Value func_maxa (valVector args, ValueCalc *calc, FuncExtra *);
  59. Value func_mdeterm (valVector args, ValueCalc *calc, FuncExtra *);
  60. Value func_min (valVector args, ValueCalc *calc, FuncExtra *);
  61. Value func_mina (valVector args, ValueCalc *calc, FuncExtra *);
  62. Value func_mmult (valVector args, ValueCalc *calc, FuncExtra *);
  63. Value func_mod (valVector args, ValueCalc *calc, FuncExtra *);
  64. Value func_mround (valVector args, ValueCalc *calc, FuncExtra *);
  65. Value func_mult (valVector args, ValueCalc *calc, FuncExtra *);
  66. Value func_multinomial (valVector args, ValueCalc *calc, FuncExtra *);
  67. Value func_odd (valVector args, ValueCalc *calc, FuncExtra *);
  68. Value func_pow (valVector args, ValueCalc *calc, FuncExtra *);
  69. Value func_quotient (valVector args, ValueCalc *calc, FuncExtra *);
  70. Value func_product (valVector args, ValueCalc *calc, FuncExtra *);
  71. Value func_rand (valVector args, ValueCalc *calc, FuncExtra *);
  72. Value func_randbetween (valVector args, ValueCalc *calc, FuncExtra *);
  73. Value func_randbernoulli (valVector args, ValueCalc *calc, FuncExtra *);
  74. Value func_randbinom (valVector args, ValueCalc *calc, FuncExtra *);
  75. Value func_randexp (valVector args, ValueCalc *calc, FuncExtra *);
  76. Value func_randnegbinom (valVector args, ValueCalc *calc, FuncExtra *);
  77. Value func_randnorm (valVector args, ValueCalc *calc, FuncExtra *);
  78. Value func_randpoisson (valVector args, ValueCalc *calc, FuncExtra *);
  79. Value func_rootn (valVector args, ValueCalc *calc, FuncExtra *);
  80. Value func_round (valVector args, ValueCalc *calc, FuncExtra *);
  81. Value func_rounddown (valVector args, ValueCalc *calc, FuncExtra *);
  82. Value func_roundup (valVector args, ValueCalc *calc, FuncExtra *);
  83. Value func_sign (valVector args, ValueCalc *calc, FuncExtra *);
  84. Value func_sqrt (valVector args, ValueCalc *calc, FuncExtra *);
  85. Value func_sqrtpi (valVector args, ValueCalc *calc, FuncExtra *);
  86. Value func_subtotal (valVector args, ValueCalc *calc, FuncExtra *);
  87. Value func_sum (valVector args, ValueCalc *calc, FuncExtra *);
  88. Value func_suma (valVector args, ValueCalc *calc, FuncExtra *);
  89. Value func_sumif (valVector args, ValueCalc *calc, FuncExtra *);
  90. Value func_sumsq (valVector args, ValueCalc *calc, FuncExtra *);
  91. Value func_trunc (valVector args, ValueCalc *calc, FuncExtra *);
  92. // Value func_multipleOP (valVector args, ValueCalc *calc, FuncExtra *);
  93. // registers all math functions
  94. void RegisterMathFunctions()
  95. {
  96. FunctionRepository* repo = FunctionRepository::self();
  97. Function *f;
  98. /*
  99. f = new Function ("MULTIPLEOPERATIONS", func_multipleOP);
  100. repo->add (f);
  101. */
  102. // functions that don't take array parameters
  103. f = new Function ("ABS", func_abs);
  104. repo->add (f);
  105. f = new Function ("CEIL", func_ceil);
  106. repo->add (f);
  107. f = new Function ("CEILING", func_ceiling);
  108. f->setParamCount (1, 2);
  109. repo->add (f);
  110. f = new Function ("CUR", func_cur);
  111. repo->add (f);
  112. f = new Function ("EPS", func_eps);
  113. f->setParamCount (0);
  114. repo->add (f);
  115. f = new Function ("EVEN", func_even);
  116. repo->add (f);
  117. f = new Function ("EXP", func_exp);
  118. repo->add (f);
  119. f = new Function ("FACT", func_fact);
  120. repo->add (f);
  121. f = new Function ("FACTDOUBLE", func_factdouble);
  122. repo->add (f);
  123. f = new Function ("FIB", func_fib); // KSpread-specific, like Quattro-Pro's FIB
  124. repo->add (f);
  125. f = new Function ("FLOOR", func_floor);
  126. repo->add (f);
  127. f = new Function ("INT", func_int);
  128. repo->add (f);
  129. f = new Function ("INV", func_inv);
  130. repo->add (f);
  131. f = new Function ("LN", func_ln);
  132. repo->add (f);
  133. f = new Function ("LOG", func_log10);
  134. repo->add (f);
  135. f = new Function ("LOG2", func_log2);
  136. repo->add (f);
  137. f = new Function ("LOG10", func_log10); // same as LOG
  138. repo->add (f);
  139. f = new Function ("LOGN", func_logn);
  140. f->setParamCount (2);
  141. repo->add (f);
  142. f = new Function ("MOD", func_mod);
  143. f->setParamCount (2);
  144. repo->add (f);
  145. f = new Function ("MROUND", func_mround);
  146. f->setParamCount (2);
  147. repo->add (f);
  148. f = new Function ("MULTINOMIAL", func_multinomial);
  149. f->setParamCount (1, -1);
  150. repo->add (f);
  151. f = new Function ("ODD", func_odd);
  152. repo->add (f);
  153. f = new Function ("POW", func_pow);
  154. f->setParamCount (2);
  155. repo->add (f);
  156. f = new Function ("POWER", func_pow);
  157. f->setParamCount (2);
  158. repo->add (f);
  159. f = new Function ("TQUOTIENT", func_quotient);
  160. f->setParamCount (2);
  161. repo->add (f);
  162. f = new Function ("RAND", func_rand);
  163. f->setParamCount (0);
  164. repo->add (f);
  165. f = new Function ("RANDBERNOULLI", func_randbernoulli);
  166. repo->add (f);
  167. f = new Function ("RANDBETWEEN", func_randbetween);
  168. f->setParamCount (2);
  169. repo->add (f);
  170. f = new Function ("RANDBINOM", func_randbinom);
  171. f->setParamCount (2);
  172. repo->add (f);
  173. f = new Function ("RANDEXP", func_randexp);
  174. repo->add (f);
  175. f = new Function ("RANDNEGBINOM", func_randnegbinom);
  176. f->setParamCount (2);
  177. repo->add (f);
  178. f = new Function ("RANDNORM", func_randnorm);
  179. f->setParamCount (2);
  180. repo->add (f);
  181. f = new Function ("RANDPOISSON", func_randpoisson);
  182. repo->add (f);
  183. f = new Function ("ROOTN", func_rootn);
  184. f->setParamCount (2);
  185. repo->add (f);
  186. f = new Function ("ROUND", func_round);
  187. f->setParamCount (1, 2);
  188. repo->add (f);
  189. f = new Function ("ROUNDDOWN", func_rounddown);
  190. f->setParamCount (1, 2);
  191. repo->add (f);
  192. f = new Function ("ROUNDUP", func_roundup);
  193. f->setParamCount (1, 2);
  194. repo->add (f);
  195. f = new Function ("SIGN", func_sign);
  196. repo->add (f);
  197. f = new Function ("SQRT", func_sqrt);
  198. repo->add (f);
  199. f = new Function ("SQRTPI", func_sqrtpi);
  200. repo->add (f);
  201. f = new Function ("TRUNC", func_trunc);
  202. f->setParamCount (1, 2);
  203. repo->add (f);
  204. // functions that operate over arrays
  205. f = new Function ("COUNT", func_count);
  206. f->setParamCount (1, -1);
  207. f->setAcceptArray ();
  208. repo->add (f);
  209. f = new Function ("COUNTA", func_counta);
  210. f->setParamCount (1, -1);
  211. f->setAcceptArray ();
  212. repo->add (f);
  213. f = new Function ("COUNTBLANK", func_countblank);
  214. f->setParamCount (1, -1);
  215. f->setAcceptArray ();
  216. repo->add (f);
  217. f = new Function ("COUNTIF", func_countif);
  218. f->setParamCount (2);
  219. f->setAcceptArray ();
  220. repo->add (f);
  221. f = new Function ("DIV", func_div);
  222. f->setParamCount (1, -1);
  223. f->setAcceptArray ();
  224. repo->add (f);
  225. f = new Function ("G_PRODUCT", func_kproduct); // Gnumeric compatibility
  226. f->setParamCount (1, -1);
  227. f->setAcceptArray ();
  228. repo->add (f);
  229. f = new Function ("GCD", func_gcd);
  230. f->setParamCount (1, -1);
  231. f->setAcceptArray ();
  232. repo->add (f);
  233. f = new Function ("KPRODUCT", func_kproduct);
  234. f->setParamCount (1, -1);
  235. f->setAcceptArray ();
  236. repo->add (f);
  237. f = new Function ("LCM", func_lcm);
  238. f->setParamCount (1, -1);
  239. f->setAcceptArray ();
  240. repo->add (f);
  241. f = new Function ("MAX", func_max);
  242. f->setParamCount (1, -1);
  243. f->setAcceptArray ();
  244. repo->add (f);
  245. f = new Function ("MAXA", func_maxa);
  246. f->setParamCount (1, -1);
  247. f->setAcceptArray ();
  248. repo->add (f);
  249. f = new Function ("MDETERM", func_mdeterm);
  250. f->setParamCount (1);
  251. f->setAcceptArray ();
  252. repo->add (f);
  253. f = new Function ("MIN", func_min);
  254. f->setParamCount (1, -1);
  255. f->setAcceptArray ();
  256. repo->add (f);
  257. f = new Function ("MINA", func_mina);
  258. f->setParamCount (1, -1);
  259. f->setAcceptArray ();
  260. repo->add (f);
  261. f = new Function ("MMULT", func_mmult);
  262. f->setParamCount (2);
  263. f->setAcceptArray ();
  264. repo->add (f);
  265. f = new Function ("MULTIPLY", func_product); // same as PRODUCT
  266. f->setParamCount (1, -1);
  267. f->setAcceptArray ();
  268. repo->add (f);
  269. f = new Function ("PRODUCT", func_product);
  270. f->setParamCount (1, -1);
  271. f->setAcceptArray ();
  272. repo->add (f);
  273. f = new Function ("SUM", func_sum);
  274. f->setParamCount (1, -1);
  275. f->setAcceptArray ();
  276. repo->add (f);
  277. f = new Function ("SUMA", func_suma);
  278. f->setParamCount (1, -1);
  279. f->setAcceptArray ();
  280. repo->add (f);
  281. f = new Function ("SUBTOTAL", func_subtotal);
  282. f->setParamCount (2);
  283. f->setAcceptArray ();
  284. f->setNeedsExtra (true);
  285. repo->add (f);
  286. f = new Function ("SUMIF", func_sumif);
  287. f->setParamCount (2, 3);
  288. f->setAcceptArray ();
  289. repo->add (f);
  290. f = new Function ("SUMSQ", func_sumsq);
  291. f->setParamCount (1, -1);
  292. f->setAcceptArray ();
  293. repo->add (f);
  294. }
  295. // Function: SQRT
  296. Value func_sqrt (valVector args, ValueCalc *calc, FuncExtra *)
  297. {
  298. return calc->sqrt (args[0]);
  299. }
  300. // Function: SQRTPI
  301. Value func_sqrtpi (valVector args, ValueCalc *calc, FuncExtra *)
  302. {
  303. // sqrt (val * PI)
  304. return calc->sqrt (calc->mul (args[0], calc->pi()));
  305. }
  306. // Function: ROOTN
  307. Value func_rootn (valVector args, ValueCalc *calc, FuncExtra *)
  308. {
  309. return calc->pow (args[0], calc->div (1, args[1]));
  310. }
  311. // Function: CUR
  312. Value func_cur (valVector args, ValueCalc *calc, FuncExtra *)
  313. {
  314. return calc->pow (args[0], 1.0/3.0);
  315. }
  316. // Function: ABS
  317. Value func_abs (valVector args, ValueCalc *calc, FuncExtra *)
  318. {
  319. return calc->abs (args[0]);
  320. }
  321. // Function: exp
  322. Value func_exp (valVector args, ValueCalc *calc, FuncExtra *)
  323. {
  324. return calc->exp (args[0]);
  325. }
  326. // Function: ceil
  327. Value func_ceil (valVector args, ValueCalc *calc, FuncExtra *)
  328. {
  329. return calc->roundUp (args[0], 0);
  330. }
  331. // Function: ceiling
  332. Value func_ceiling (valVector args, ValueCalc *calc, FuncExtra *)
  333. {
  334. Value number = args[0];
  335. Value res;
  336. if (args.count() == 2)
  337. res = args[1];
  338. else
  339. res = calc->gequal (number, 0.0) ? 1.0 : -1.0;
  340. if (calc->isZero(res))
  341. return Value::errorDIV0();
  342. Value d = calc->div (number, res);
  343. if (calc->greater (0, d))
  344. return Value::errorVALUE();
  345. Value rud = calc->roundDown (d);
  346. if (calc->approxEqual (rud, d))
  347. d = calc->mul (rud, res);
  348. else
  349. d = calc->mul (calc->roundUp (d), res);
  350. return d;
  351. }
  352. // Function: floor
  353. Value func_floor (valVector args, ValueCalc *calc, FuncExtra *)
  354. {
  355. return calc->roundDown (args[0], 0);
  356. }
  357. // Function: ln
  358. Value func_ln (valVector args, ValueCalc *calc, FuncExtra *)
  359. {
  360. return calc->ln (args[0]);
  361. }
  362. // Function: LOGn
  363. Value func_logn (valVector args, ValueCalc *calc, FuncExtra *)
  364. {
  365. return calc->log (args[0], args[1]);
  366. }
  367. // Function: LOG2
  368. Value func_log2 (valVector args, ValueCalc *calc, FuncExtra *)
  369. {
  370. return calc->log (args[0], 2.0);
  371. }
  372. // Function: LOG10
  373. Value func_log10 (valVector args, ValueCalc *calc, FuncExtra *)
  374. {
  375. return calc->log (args[0]);
  376. }
  377. // Function: sum
  378. Value func_sum (valVector args, ValueCalc *calc, FuncExtra *)
  379. {
  380. return calc->sum (args, false);
  381. }
  382. // Function: suma
  383. Value func_suma (valVector args, ValueCalc *calc, FuncExtra *)
  384. {
  385. return calc->sum (args, true);
  386. }
  387. Value func_sumif (valVector args, ValueCalc *calc, FuncExtra *)
  388. {
  389. Value checkRange = args[0];
  390. TQString condition = calc->conv()->asString (args[1]).asString();
  391. Value sumRange = checkRange;
  392. if (args.count() == 3)
  393. sumRange = args[2];
  394. Condition cond;
  395. calc->getCond (cond, condition);
  396. return calc->sumIf (sumRange, checkRange, cond);
  397. }
  398. // Function: product
  399. Value func_product (valVector args, ValueCalc *calc, FuncExtra *)
  400. {
  401. return calc->product (args, 0.0);
  402. }
  403. // Function: kproduct
  404. Value func_kproduct (valVector args, ValueCalc *calc, FuncExtra *)
  405. {
  406. return calc->product (args, 1.0);
  407. }
  408. // Function: DIV
  409. Value func_div (valVector args, ValueCalc *calc, FuncExtra *)
  410. {
  411. Value val = args[0];
  412. for (unsigned int i = 1; i < args.count(); ++i)
  413. {
  414. val = calc->div (val, args[i]);
  415. if (val.isError())
  416. return val;
  417. }
  418. return val;
  419. }
  420. // Function: SUMSQ
  421. Value func_sumsq (valVector args, ValueCalc *calc, FuncExtra *)
  422. {
  423. Value res;
  424. calc->arrayWalk (args, res, calc->awFunc ("sumsq"), 0);
  425. return res;
  426. }
  427. // Function: MAX
  428. Value func_max (valVector args, ValueCalc *calc, FuncExtra *)
  429. {
  430. Value m = calc->max (args, false);
  431. return m.isEmpty() ? Value(0.0) : m;
  432. }
  433. // Function: MAXA
  434. Value func_maxa (valVector args, ValueCalc *calc, FuncExtra *)
  435. {
  436. Value m = calc->max (args);
  437. return m.isEmpty() ? Value(0.0) : m;
  438. }
  439. // Function: MIN
  440. Value func_min (valVector args, ValueCalc *calc, FuncExtra *)
  441. {
  442. Value m = calc->min (args, false);
  443. return m.isEmpty() ? Value(0.0) : m;
  444. }
  445. // Function: MINA
  446. Value func_mina (valVector args, ValueCalc *calc, FuncExtra *)
  447. {
  448. Value m = calc->min (args);
  449. return m.isEmpty() ? Value(0.0) : m;
  450. }
  451. // Function: INT
  452. Value func_int (valVector args, ValueCalc *calc, FuncExtra *)
  453. {
  454. return calc->conv()->asInteger (args[0]);
  455. }
  456. // Function: TQUOTIENT
  457. Value func_quotient (valVector args, ValueCalc *calc, FuncExtra *)
  458. {
  459. if (calc->isZero (args[1]))
  460. return Value::errorDIV0();
  461. return calc->conv()->asInteger (calc->div (args[0], args[1]));
  462. }
  463. // Function: eps
  464. Value func_eps (valVector, ValueCalc *calc, FuncExtra *)
  465. {
  466. return calc->eps ();
  467. }
  468. Value func_randexp (valVector args, ValueCalc *calc, FuncExtra *)
  469. {
  470. // -1 * d * log (random)
  471. return calc->mul (calc->mul (args[0], -1), calc->random());
  472. }
  473. Value func_randbinom (valVector args, ValueCalc *calc, FuncExtra *)
  474. {
  475. // this function will not support arbitrary precision
  476. double d = calc->conv()->asFloat (args[0]).asFloat();
  477. int tr = calc->conv()->asInteger (args[1]).asInteger();
  478. if ( d < 0 || d > 1 )
  479. return Value::errorVALUE();
  480. if ( tr < 0 )
  481. return Value::errorVALUE();
  482. // taken from gnumeric
  483. double x = pow(1 - d, tr);
  484. double r = (double) rand() / ( RAND_MAX + 1.0 );
  485. double t = x;
  486. int i = 0;
  487. while (r > t)
  488. {
  489. x *= (((tr - i) * d) / ((1 + i) * (1 - d)));
  490. i++;
  491. t += x;
  492. }
  493. return Value (i);
  494. }
  495. Value func_randnegbinom (valVector args, ValueCalc *calc, FuncExtra *)
  496. {
  497. // this function will not support arbitrary precision
  498. double d = calc->conv()->asFloat (args[0]).asFloat();
  499. int f = calc->conv()->asInteger (args[1]).asInteger();
  500. if ( d < 0 || d > 1 )
  501. return Value::errorVALUE();
  502. if ( f < 0 )
  503. return Value::errorVALUE();
  504. // taken from Gnumeric
  505. double x = pow(d, f);
  506. double r = (double) rand() / ( RAND_MAX + 1.0 );
  507. double t = x;
  508. int i = 0;
  509. while (r > t)
  510. {
  511. x *= ( ( ( f + i ) * ( 1 - d ) ) / (1 + i) ) ;
  512. i++;
  513. t += x;
  514. }
  515. return Value (i);
  516. }
  517. Value func_randbernoulli (valVector args, ValueCalc *calc, FuncExtra *)
  518. {
  519. Value rnd = calc->random ();
  520. return Value (calc->greater (rnd, args[0]) ? 1.0 : 0.0);
  521. }
  522. Value func_randnorm (valVector args, ValueCalc *calc, FuncExtra *)
  523. {
  524. Value mu = args[0];
  525. Value sigma = args[1];
  526. //using polar form of the Box-Muller transformation
  527. //refer to http://www.taygeta.com/random/gaussian.html for more info
  528. Value x1, x2, w;
  529. do {
  530. // x1,x2 = 2 * random() - 1
  531. x1 = calc->random (2.0);
  532. x2 = calc->random (2.0);
  533. x1 = calc->sub (x1, 1);
  534. x1 = calc->sub (x2, 1);
  535. w = calc->add (calc->sqr(x1), calc->sqr (x2));
  536. } while (calc->gequal (w, 1.0)); // w >= 1.0
  537. //sqrt ((-2.0 * log (w)) / w) :
  538. w = calc->sqrt (calc->div (calc->mul (-2.0, calc->ln (w)), w));
  539. Value res = calc->mul (x1, w);
  540. res = calc->add (calc->mul (res, sigma), mu); // res*sigma + mu
  541. return res;
  542. }
  543. Value func_randpoisson (valVector args, ValueCalc *calc, FuncExtra *)
  544. {
  545. if (calc->lower (args[0], 0))
  546. return Value::errorVALUE();
  547. // taken from Gnumeric...
  548. Value x = calc->exp (calc->mul (-1, args[0])); // e^(-A)
  549. Value r = calc->random ();
  550. Value t = x;
  551. int i = 0;
  552. while (calc->greater (r, t)) { // r > t
  553. x = calc->mul (x, calc->div (args[0], i + 1)); // x *= (A/(i+1))
  554. t = calc->add (t, x); //t += x
  555. i++;
  556. }
  557. return Value (i);
  558. }
  559. // Function: rand
  560. Value func_rand (valVector, ValueCalc *calc, FuncExtra *)
  561. {
  562. return calc->random ();
  563. }
  564. // Function: RANDBETWEEN
  565. Value func_randbetween (valVector args, ValueCalc *calc, FuncExtra *)
  566. {
  567. Value v1 = args[0];
  568. Value v2 = args[1];
  569. if (calc->greater (v2, v1)) {
  570. v1 = args[1];
  571. v2 = args[0];
  572. }
  573. return calc->add (v1, calc->random (calc->sub (v2, v1)));
  574. }
  575. // Function: POW
  576. Value func_pow (valVector args, ValueCalc *calc, FuncExtra *)
  577. {
  578. return calc->pow (args[0], args[1]);
  579. }
  580. // Function: MOD
  581. Value func_mod (valVector args, ValueCalc *calc, FuncExtra *)
  582. {
  583. return calc->mod (args[0], args[1]);
  584. }
  585. // Function: fact
  586. Value func_fact (valVector args, ValueCalc *calc, FuncExtra *)
  587. {
  588. return calc->fact (args[0]);
  589. }
  590. // Function: FACTDOUBLE
  591. Value func_factdouble (valVector args, ValueCalc *calc, FuncExtra *)
  592. {
  593. return calc->factDouble (args[0]);
  594. }
  595. // Function: MULTINOMIAL
  596. Value func_multinomial (valVector args, ValueCalc *calc, FuncExtra *)
  597. {
  598. // (a+b+c)! / a!b!c! (any number of params possible)
  599. Value num = 0, den = 1;
  600. for (unsigned int i = 0; i < args.count(); ++i) {
  601. num = calc->add (num, args[i]);
  602. den = calc->mul (den, calc->fact (args[i]));
  603. }
  604. num = calc->fact (num);
  605. return calc->div (num, den);
  606. }
  607. // Function: sign
  608. Value func_sign (valVector args, ValueCalc *calc, FuncExtra *)
  609. {
  610. return Value (calc->sign (args[0]));
  611. }
  612. // Function: INV
  613. Value func_inv (valVector args, ValueCalc *calc, FuncExtra *)
  614. {
  615. return calc->mul (args[0], -1);
  616. }
  617. Value func_mround (valVector args, ValueCalc *calc, FuncExtra *)
  618. {
  619. Value d = args[0];
  620. Value m = args[1];
  621. // signs must be the same
  622. if ((calc->greater (d, 0) && calc->lower (m, 0))
  623. || (calc->lower (d, 0) && calc->greater (m, 0)))
  624. return Value::errorVALUE();
  625. int sign = 1;
  626. if (calc->lower (d, 0))
  627. {
  628. sign = -1;
  629. d = calc->mul (d, -1);
  630. m = calc->mul (m, -1);
  631. }
  632. // from gnumeric:
  633. Value mod = calc->mod (d, m);
  634. Value div = calc->sub (d, mod);
  635. Value result = div;
  636. if (calc->greater (mod, calc->div (m, 2))) // mod > m/2
  637. result = calc->add (result, m); // result += m
  638. result = calc->mul (result, sign); // add the sign
  639. return result;
  640. }
  641. // Function: ROUNDDOWN
  642. Value func_rounddown (valVector args, ValueCalc *calc, FuncExtra *)
  643. {
  644. if (args.count() == 2)
  645. return calc->roundDown (args[0], args[1]);
  646. return calc->roundDown (args[0], 0);
  647. }
  648. // Function: ROUNDUP
  649. Value func_roundup (valVector args, ValueCalc *calc, FuncExtra *)
  650. {
  651. if (args.count() == 2)
  652. return calc->roundUp (args[0], args[1]);
  653. return calc->roundUp (args[0], 0);
  654. }
  655. // Function: ROUND
  656. Value func_round (valVector args, ValueCalc *calc, FuncExtra *)
  657. {
  658. if (args.count() == 2)
  659. return calc->round (args[0], args[1]);
  660. return calc->round (args[0], 0);
  661. }
  662. // Function: EVEN
  663. Value func_even (valVector args, ValueCalc *calc, FuncExtra *)
  664. {
  665. const Value value = calc->roundUp (args[0], 0);
  666. return calc->isZero( calc->mod(value, 2) ) ? value : calc->add(value, 1);
  667. }
  668. // Function: ODD
  669. Value func_odd (valVector args, ValueCalc *calc, FuncExtra *)
  670. {
  671. const Value value = calc->roundUp (args[0], 0);
  672. return calc->isZero( calc->mod(value, 2) ) ? calc->add(value, 1) : value;
  673. }
  674. Value func_trunc (valVector args, ValueCalc *calc, FuncExtra *)
  675. {
  676. if (args.count() == 1)
  677. return calc->roundDown (args[0]);
  678. return calc->roundDown (args[0], args[1]);
  679. }
  680. // Function: COUNT
  681. Value func_count (valVector args, ValueCalc *calc, FuncExtra *)
  682. {
  683. return calc->count (args, false);
  684. }
  685. // Function: COUNTA
  686. Value func_counta (valVector args, ValueCalc *calc, FuncExtra *)
  687. {
  688. return calc->count (args);
  689. }
  690. // Function: COUNTBLANK
  691. Value func_countblank (valVector args, ValueCalc *, FuncExtra *)
  692. {
  693. int cnt = 0;
  694. for (unsigned int i = 0; i < args.count(); ++i)
  695. if (args[i].isArray()) {
  696. int rows = args[i].rows();
  697. int cols = args[i].columns();
  698. for (int r = 0; r < rows; ++r)
  699. for (int c = 0; c < cols; ++c)
  700. if (args[i].element (c, r).isEmpty())
  701. cnt++;
  702. } else
  703. if (args[i].isEmpty())
  704. cnt++;
  705. return Value (cnt);
  706. }
  707. // Function: COUNTIF
  708. Value func_countif (valVector args, ValueCalc *calc, FuncExtra *)
  709. {
  710. Value range = args[0];
  711. TQString condition = calc->conv()->asString (args[1]).asString();
  712. Condition cond;
  713. calc->getCond (cond, condition);
  714. return calc->countIf (range, cond);
  715. }
  716. // Function: FIB
  717. Value func_fib (valVector args, ValueCalc *calc, FuncExtra *)
  718. {
  719. /*
  720. Lucas' formula for the nth Fibonacci number F(n) is given by
  721. ((1+sqrt(5))/2)^n - ((1-sqrt(5))/2)^n
  722. F(n) = ------------------------------------- .
  723. sqrt(5)
  724. */
  725. Value n = args[0];
  726. Value s = calc->sqrt (5.0);
  727. // u1 = ((1+sqrt(5))/2)^n
  728. Value u1 = calc->pow (calc->div (calc->add (1, s), 2), n);
  729. // u2 = ((1-sqrt(5))/2)^n
  730. Value u2 = calc->pow (calc->div (calc->sub (1, s), 2), n);
  731. Value result = calc->div (calc->sub (u1, u2), s);
  732. return result;
  733. }
  734. static Value func_gcd_helper(const Value &val, ValueCalc *calc)
  735. {
  736. Value res = 0;
  737. if (!val.isArray ())
  738. return val;
  739. for (unsigned int row = 0; row < val.rows(); ++row)
  740. for (unsigned int col = 0; col < val.columns(); ++col)
  741. {
  742. Value v = val.element (col, row);
  743. if (v.isArray ())
  744. v = func_gcd_helper (v, calc);
  745. res = calc->gcd (res, v);
  746. }
  747. return res;
  748. }
  749. // Function: GCD
  750. Value func_gcd (valVector args, ValueCalc *calc, FuncExtra *)
  751. {
  752. Value result = 0;
  753. for (unsigned int i = 0; i < args.count(); ++i)
  754. if (args[i].isArray())
  755. result = calc->gcd (result, func_gcd_helper (args[i], calc));
  756. else
  757. result = calc->gcd (result, args[i]);
  758. return result;
  759. }
  760. static Value func_lcm_helper(const Value &val, ValueCalc *calc)
  761. {
  762. Value res = 0;
  763. if (!val.isArray ())
  764. return val;
  765. for (unsigned int row = 0; row < val.rows(); ++row)
  766. for (unsigned int col = 0; col < val.columns(); ++col)
  767. {
  768. Value v = val.element (col, row);
  769. if (v.isArray ())
  770. v = func_lcm_helper (v, calc);
  771. res = calc->lcm (res, v);
  772. }
  773. return res;
  774. }
  775. // Function: lcm
  776. Value func_lcm (valVector args, ValueCalc *calc, FuncExtra *)
  777. {
  778. Value result = 0;
  779. for (unsigned int i = 0; i < args.count(); ++i)
  780. if (args[i].isArray())
  781. result = calc->lcm (result, func_lcm_helper (args[i], calc));
  782. else
  783. result = calc->lcm (result, args[i]);
  784. return result;
  785. }
  786. Value determinant (ValueCalc *calc, Value matrix)
  787. {
  788. // this is a --- SLOOOW --- recursive function
  789. // using this for something bigger than 10x10 or so = suicide :P
  790. // but I'm too lazy to adjust gnumeric's code - remains as a TODO then
  791. // as a note, gnumeric uses LUP decomposition to compute this
  792. // take first row, generate smaller matrices, recursion, multiply
  793. Value res = 0.0;
  794. int n = matrix.columns();
  795. if (n == 1) return matrix.element (0, 0);
  796. if (n == 2) return calc->sub (
  797. calc->mul (matrix.element (1,1), matrix.element (0,0)),
  798. calc->mul (matrix.element (1,0), matrix.element (0,1)));
  799. // n >= 3
  800. for (int i = 0; i < n; ++i) {
  801. Value smaller (n-1, n-1);
  802. int col = 0;
  803. for (int c = 0; c < n; ++c)
  804. if (c != i) {
  805. // copy column c to column col in new matrix
  806. for (int r = 1; r < n; r++)
  807. smaller.setElement (col, r-1, matrix.element (c, r));
  808. col++;
  809. }
  810. Value minor = determinant (calc, smaller);
  811. if (i % 2 == 1) minor = calc->mul (minor, -1);
  812. res = calc->add (res, calc->mul (minor, matrix.element (i, 0)));
  813. }
  814. return res;
  815. }
  816. // Function: mdeterm
  817. Value func_mdeterm (valVector args, ValueCalc *calc, FuncExtra *)
  818. {
  819. Value m = args[0];
  820. unsigned r = m.rows ();
  821. unsigned c = m.columns ();
  822. if (r != c) // must be a square matrix
  823. return Value::errorVALUE();
  824. return determinant (calc, args[0]);
  825. }
  826. // Function: mmult
  827. Value func_mmult (valVector args, ValueCalc *calc, FuncExtra *)
  828. {
  829. Value m1 = args[0];
  830. Value m2 = args[1];
  831. unsigned r1 = m1.rows ();
  832. unsigned c1 = m1.columns ();
  833. unsigned r2 = m2.rows ();
  834. unsigned c2 = m2.columns ();
  835. if (c1 != r2) // row/column counts must match
  836. return Value::errorVALUE();
  837. // create the resulting matrix
  838. Value res (c2, r1);
  839. // perform the multiplication - O(n^3) algorithm
  840. for (uint row = 0; row < r1; ++row)
  841. for (uint col = 0; col < c2; ++col) {
  842. Value val = 0.0;
  843. for (uint pos = 0; pos < c1; ++pos)
  844. val = calc->add (val,
  845. calc->mul (m1.element (pos, row), m2.element (col, pos)));
  846. res.setElement (col, row, val);
  847. }
  848. return res;
  849. }
  850. // Function: SUBTOTAL
  851. // This function requires access to the Sheet and so on, because
  852. // it needs to check whether cells contain the SUBTOTAL formula or not ...
  853. // Cells containing a SUBTOTAL formula must be ignored.
  854. Value func_subtotal (valVector args, ValueCalc *calc, FuncExtra *e)
  855. {
  856. int function = calc->conv()->asInteger (args[0]).asInteger();
  857. Value range = args[1];
  858. int r1 = -1, c1 = -1, r2 = -1, c2 = -1;
  859. if (e) {
  860. r1 = e->ranges[1].row1;
  861. c1 = e->ranges[1].col1;
  862. r2 = e->ranges[1].row2;
  863. c2 = e->ranges[1].col2;
  864. }
  865. // if we have a range, run through it, and put an empty value to the place
  866. // of all occurences of the SUBTOTAL function
  867. Value empty;
  868. if ((r1 > 0) && (c1 > 0) && (r2 > 0) && (c2 > 0)) {
  869. for (int r = r1; r <= r2; ++r)
  870. for (int c = c1; c <= c2; ++c) {
  871. Cell *cell = e->sheet->cellAt (c, r);
  872. if (cell->isDefault())
  873. continue;
  874. if (cell->isFormula() && cell->text().find ("SUBTOTAL", 0, false) != -1)
  875. // cell contains the word SUBTOTAL - replace value with empty
  876. range.setElement (c-c1, r-r1, empty);
  877. }
  878. }
  879. // Good. Now we can execute the necessary function on the range.
  880. Value res;
  881. Function *f;
  882. valVector a;
  883. switch (function) {
  884. case 1: // Average
  885. res = calc->avg (range, false);
  886. break;
  887. case 2: // Count
  888. res = calc->count (range, false);
  889. break;
  890. case 3: // CountA
  891. res = calc->count (range);
  892. break;
  893. case 4: // MAX
  894. res = calc->max (range, false);
  895. break;
  896. case 5: // Min
  897. res = calc->min (range, false);
  898. break;
  899. case 6: // Product
  900. res = calc->product (range, 0.0, false);
  901. break;
  902. case 7: // StDev
  903. res = calc->stddev (range, false);
  904. break;
  905. case 8: // StDevP
  906. res = calc->stddevP (range, false);
  907. break;
  908. case 9: // Sum
  909. res = calc->sum (range, false);
  910. break;
  911. case 10: // Var
  912. f = FunctionRepository::self()->function ("VAR");
  913. if (!f) return Value::errorVALUE();
  914. a.reserve (1);
  915. a[0] = range;
  916. res = f->exec (a, calc, 0);
  917. break;
  918. case 11: // VarP
  919. f = FunctionRepository::self()->function ("VARP");
  920. if (!f) return Value::errorVALUE();
  921. a.reserve (1);
  922. a[0] = range;
  923. res = f->exec (a, calc, 0);
  924. break;
  925. default:
  926. return Value::errorVALUE();
  927. }
  928. return res;
  929. }
  930. /*
  931. Commented out.
  932. Absolutely no idea what this thing is supposed to do.
  933. To anyone who would enable this code: it still uses koscript calls - you need
  934. to convert it to the new style prior to uncommenting.
  935. // Function: MULTIPLEOPERATIONS
  936. Value func_multipleOP (valVector args, ValueCalc *calc, FuncExtra *)
  937. {
  938. if (gCell)
  939. {
  940. context.setValue( new KSValue( ((Interpreter *) context.interpreter() )->cell()->value().asFloat() ) );
  941. return true;
  942. }
  943. gCell = ((Interpreter *) context.interpreter() )->cell();
  944. TQValueList<KSValue::Ptr>& args = context.value()->listValue();
  945. TQValueList<KSValue::Ptr>& extra = context.extraData()->listValue();
  946. if ( !KSUtil::checkArgumentsCount( context, 5, "MULTIPLEOPERATIONS", true ) )
  947. {
  948. gCell = 0;
  949. return false;
  950. }
  951. // 0: cell must contain formula with double/int result
  952. // 0, 1, 2, 3, 4: must contain integer/double
  953. for (int i = 0; i < 5; ++i)
  954. {
  955. if ( !KSUtil::checkType( context, args[i], KSValue::DoubleType, true ) )
  956. {
  957. gCell = 0;
  958. return false;
  959. }
  960. }
  961. // ((Interpreter *) context.interpreter() )->document()->emitBeginOperation();
  962. double oldCol = args[1]->doubleValue();
  963. double oldRow = args[3]->doubleValue();
  964. kdDebug() << "Old values: Col: " << oldCol << ", Row: " << oldRow << endl;
  965. Cell * cell;
  966. Sheet * sheet = ((Interpreter *) context.interpreter() )->sheet();
  967. Point point( extra[1]->stringValue() );
  968. Point point2( extra[3]->stringValue() );
  969. Point point3( extra[0]->stringValue() );
  970. if ( ( args[1]->doubleValue() != args[2]->doubleValue() )
  971. || ( args[3]->doubleValue() != args[4]->doubleValue() ) )
  972. {
  973. cell = sheet->cellAt( point.pos.x(), point.pos.y() );
  974. cell->setValue( args[2]->doubleValue() );
  975. kdDebug() << "Setting value " << args[2]->doubleValue() << " on cell " << point.pos.x()
  976. << ", " << point.pos.y() << endl;
  977. cell = sheet->cellAt( point2.pos.x(), point.pos.y() );
  978. cell->setValue( args[4]->doubleValue() );
  979. kdDebug() << "Setting value " << args[4]->doubleValue() << " on cell " << point2.pos.x()
  980. << ", " << point2.pos.y() << endl;
  981. }
  982. Cell * cell1 = sheet->cellAt( point3.pos.x(), point3.pos.y() );
  983. cell1->calc( false );
  984. double d = cell1->value().asFloat();
  985. kdDebug() << "Cell: " << point3.pos.x() << "; " << point3.pos.y() << " with value "
  986. << d << endl;
  987. kdDebug() << "Resetting old values" << endl;
  988. cell = sheet->cellAt( point.pos.x(), point.pos.y() );
  989. cell->setValue( oldCol );
  990. cell = sheet->cellAt( point2.pos.x(), point2.pos.y() );
  991. cell->setValue( oldRow );
  992. cell1->calc( false );
  993. // ((Interpreter *) context.interpreter() )->document()->emitEndOperation();
  994. context.setValue( new KSValue( (double) d ) );
  995. gCell = 0;
  996. return true;
  997. }
  998. */