functions.c 63.4 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1
/*
2
 * fn-math.c:  Built in mathematical functions and functions registration
Arturo Espinosa's avatar
Arturo Espinosa committed
3
 *
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
4
 * Authors:
Arturo Espinosa's avatar
Arturo Espinosa committed
5
 *  Miguel de Icaza (miguel@gnu.org)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
6
 *  Jukka-Pekka Iivonen (iivonen@iki.fi)
Arturo Espinosa's avatar
Arturo Espinosa committed
7
8
 */
#include <config.h>
Morten Welinder's avatar
Morten Welinder committed
9
#include <math.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
10
11
12
13
#include "gnumeric.h"
#include "utils.h"
#include "func.h"

Donnie Barnes's avatar
Donnie Barnes committed
14
15
16
#if 0
/* help template */
static char *help_ = {
17
18
	N_("@FUNCTION=NAME\n"
	   "@SYNTAX=(b1, b2, ...)\n"
Donnie Barnes's avatar
Donnie Barnes committed
19

20
	   "@DESCRIPTION"
Donnie Barnes's avatar
Donnie Barnes committed
21
	   ""
22
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
23
24
25

	   ""
	   ""
26
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
27

Donnie Barnes's avatar
Donnie Barnes committed
28
29
30
	   ""
	   ""
	   ""
31
32
	   ""
	   "@SEEALSO=")
Donnie Barnes's avatar
Donnie Barnes committed
33
34
35
36
};

#endif

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
37
static int
Morten Welinder's avatar
Morten Welinder committed
38
gcd (int a, int b)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
39
{
Morten Welinder's avatar
Morten Welinder committed
40
	/* Euclid's Algorithm.	Assumes non-negative numbers.  */
Morten Welinder's avatar
Morten Welinder committed
41

Morten Welinder's avatar
Morten Welinder committed
42
43
	while (b != 0) {
		int r;
Morten Welinder's avatar
Morten Welinder committed
44

Morten Welinder's avatar
Morten Welinder committed
45
46
47
48
		r = a - (a / b) * b;	/* r = remainder from
					 * dividing a by b	*/
		a = b;
		b = r;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
49
	}
Morten Welinder's avatar
Morten Welinder committed
50
	return a;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
51
}
Morten Welinder's avatar
Morten Welinder committed
52

53
static float_t
Morten Welinder's avatar
Morten Welinder committed
54
gpow10 (int n)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
	float_t res = 1.0;
	float_t p;
	const int maxn = 300;

	if (n >= 0) {
		p = 10.0;
		n = (n > maxn) ? maxn : n;
	} else {
		p = 0.1;
		/* Note carefully that we avoid overflow.  */
		n = (n < -maxn) ? maxn : -n;
	}
	while (n > 0) {
		if (n & 1) res *= p;
		p *= p;
		n >>= 1;
	}
	return res;
}

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

typedef struct {
        GSList *list;
        int    num;
} math_sums_t;

static int
callback_function_sumxy (Sheet *sheet, int col, int row,
			 Cell *cell, void *user_data)
{
        math_sums_t *mm = user_data;
        float_t     x;
	gpointer    p;

	if (cell == NULL || cell->value == NULL)
	        return TRUE;

        switch (cell->value->type) {
	case VALUE_INTEGER:
	        x = cell->value->v.v_int;
		break;
	case VALUE_FLOAT:
	        x = cell->value->v.v_float;
		break;
	default:
	        return TRUE;
	}

	p = g_new(float_t, 1);
	*((float_t *) p) = x;
	mm->list = g_slist_append(mm->list, p);
	mm->num++;

	return TRUE;
}

112
static int
113
114
callback_function_makeslist (const EvalPosition *ep, Value *value,
			     ErrorMessage *error, void *closure)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{
        math_sums_t *mm = closure;
        float_t     x;
	gpointer    p;

        switch (value->type) {
	case VALUE_INTEGER:
	        x = value->v.v_int;
		break;
	case VALUE_FLOAT:
	        x = value->v.v_float;
		break;
	default:
	        return TRUE;
	}

	p = g_new(float_t, 1);
	*((float_t *) p) = x;
	mm->list = g_slist_append(mm->list, p);
	mm->num++;

	return TRUE;
}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
typedef struct {
        GSList              *list;
        criteria_test_fun_t fun;
        Value               *test_value;
        int                 num;
} math_criteria_t;

static int
callback_function_criteria (Sheet *sheet, int col, int row,
			    Cell *cell, void *user_data)
{
        math_criteria_t *mm = user_data;
	Value           *v;

	if (cell == NULL || cell->value == NULL)
	        return TRUE;

        switch (cell->value->type) {
	case VALUE_INTEGER:
Michael Meeks's avatar
Michael Meeks committed
158
	        v = value_new_int (cell->value->v.v_int);
159
160
		break;
	case VALUE_FLOAT:
Michael Meeks's avatar
Michael Meeks committed
161
	        v = value_new_float (cell->value->v.v_float);
162
163
		break;
	case VALUE_STRING:
Michael Meeks's avatar
Michael Meeks committed
164
	        v = value_new_string (cell->value->v.str->str);
165
166
167
168
169
170
		break;
	default:
	        return TRUE;
	}

	if (mm->fun(v, mm->test_value)) {
Morten Welinder's avatar
Morten Welinder committed
171
		mm->list = g_slist_append (mm->list, v);
172
173
174
175
176
177
178
		mm->num++;
	} else
	        value_release(v);

	return TRUE;
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
179
180
static char *help_gcd = {
	N_("@FUNCTION=GCD\n"
181
	   "@SYNTAX=GCD(number1,number2,...)\n"
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
182
183

	   "@DESCRIPTION="
184
	   "GCD returns the greatest common divisor of given numbers. "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
185
186
187
188
189
190
191
192
	   "\n"
	   "If any of the arguments is less than zero, GCD returns #NUM! "
	   "error. "
	   "\n"
	   "@SEEALSO=LCM")
};

static Value *
193
gnumeric_gcd (FunctionEvalInfo *ei, GList *expr_node_list)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
194
{
195
196
197
        math_sums_t p;
	GSList      *current;
        int         a, b, old_gcd, new_gcd;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
198

199
200
	p.num  = 0;
	p.list = NULL;
201
	if (function_iterate_argument_values (&ei->pos, 
202
203
					      callback_function_makeslist,
					      &p, expr_node_list,
204
205
206
					      ei->error, TRUE) == FALSE) {
		if (error_message_is_set (ei->error))
			return function_error (ei, gnumeric_err_NUM);
207
	}
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
208

209
210
	if (p.list == NULL || p.list->next == NULL)
		return function_error (ei, gnumeric_err_NUM);
Morten Welinder's avatar
Morten Welinder committed
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
try_again:
	a = *((float_t *) p.list->data);
	current=p.list->next;
	b = *((float_t *) current->data);
	old_gcd = gcd(a, b);

	for (current=current->next; current!=NULL; current=current->next) {
	        b = *((float_t *) current->data);
		new_gcd = gcd(a, b);
		if (old_gcd != new_gcd) {
		        GSList *tmp;
			for (tmp=p.list; tmp!=NULL; tmp=tmp->next) {
			        b = *((float_t *) current->data);
				if (b % old_gcd == 0)
				        *((float_t *) current->data) = 
					  b / old_gcd;
			}
			goto try_again;
		}
	}

	for (current=p.list; current!=NULL; current=current->next)
	        g_free(current->data);
	g_slist_free(p.list);

	return value_new_int (old_gcd);
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
238
239
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
static char *help_lcm = {
	N_("@FUNCTION=LCM\n"
	   "@SYNTAX=LCM(number1,number2,...)\n"

	   "@DESCRIPTION="
	   "LCM returns the least common multiple of integers.  The least "
	   "common multiple is the smallest positive number that is a "
	   "multiple of all integer arguments given. "
	   "\n"
	   "If any of the arguments is less than one, LCM returns #NUM! "
	   "error. "
	   "\n"
	   "@SEEALSO=GCD")
};

static int
256
257
callback_function_lcm (const EvalPosition *ep, Value *value,
		       ErrorMessage *error, void *closure)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
258
259
260
261
262
263
264
265
266
267
268
269
270
{
	Value *result = closure;

	switch (value->type){
	case VALUE_INTEGER:
	        if (value->v.v_int < 1)
		        return FALSE;
	        result->v.v_int /= gcd(result->v.v_int, value->v.v_int);
		result->v.v_int *= value->v.v_int;
		break;
	default:
	        return FALSE;
	}
Morten Welinder's avatar
Morten Welinder committed
271

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
272
273
274
275
	return TRUE;
}

static Value *
276
gnumeric_lcm (FunctionEvalInfo *ei, GList *nodes)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
277
278
279
280
281
282
283
{
	Value *result;

	result = g_new (Value, 1);
	result->type = VALUE_INTEGER;
	result->v.v_int = 1;

284
285
286
287
288
	if (function_iterate_argument_values (&ei->pos, callback_function_lcm,
					      result, nodes,
					      ei->error, TRUE) == FALSE) {
		if (error_message_is_set (ei->error))
			return function_error (ei, gnumeric_err_NUM);
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
289
290
291
292
293
	}

	return result;
}

294
295
296
static char *help_abs = {
	N_("@FUNCTION=ABS\n"
	   "@SYNTAX=ABS(b1)\n"
297

298
299
300
	   "@DESCRIPTION=Implements the Absolute Value function:  the result is "
	   "to drop the negative sign (if present).  This can be done for "
	   "integers and floating point numbers."
301
	   "\n"
302
	   "Performing this function on a string or empty cell simply does nothing."
303
	   "\n"
304
	   "@SEEALSO=CEIL, FLOOR")
305
306
};

307
static Value *
308
gnumeric_abs (FunctionEvalInfo *ei, Value **args)
309
{
310
	return value_new_float (fabs (value_get_as_float (args [0])));
311
}
312
313

static char *help_acos = {
314
	N_("@FUNCTION=ACOS\n"
315
	   "@SYNTAX=ACOS(x)\n"
316

317
	   "@DESCRIPTION="
318
319
	   "The ACOS function calculates the arc cosine of x; that "
	   " is the value whose cosine is x.  If x  falls  outside  the "
320
321
	   " range -1 to 1, ACOS fails and returns the error 'acos - domain error'. "
	   " The value it returns is in radians. "
322
	   "\n"
323
324
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
325
326
	   "\n"
	   "@SEEALSO=COS, SIN, DEGREES, RADIANS")
327
328
};

329
static Value *
330
gnumeric_acos (FunctionEvalInfo *ei, Value **args)
331
332
{
	float_t t;
Donnie Barnes's avatar
Donnie Barnes committed
333

334
335
336
337
	t = value_get_as_float (args [0]);
	if ((t < -1.0) || (t > 1.0))
		return function_error (ei, _("acos - domain error"));

Michael Meeks's avatar
Michael Meeks committed
338
	return value_new_float (acos (t));
339
340
341
342
}

static char *help_acosh = {
	N_("@FUNCTION=ACOSH\n"
343
	   "@SYNTAX=ACOSH(x)\n"
344
345
346

	   "@DESCRIPTION="
	   "The ACOSH  function  calculates  the inverse hyperbolic "
347
348
	   "cosine of x; that is the value whose hyperbolic cosine is "
	   "x. If x is less than 1.0, acosh() returns the error "
349
	   " 'acosh - domain error'"
Arturo Espinosa's avatar
Arturo Espinosa committed
350
	   "\n"
351
352
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
353
	   "\n"
354
	   "@SEEALSO=ACOS, DEGREES, RADIANS ")
Donnie Barnes's avatar
Donnie Barnes committed
355
356
};

357
static Value *
358
gnumeric_acosh (FunctionEvalInfo *ei, Value **args)
359
360
361
{
	float_t t;

362
363
364
365
	t = value_get_as_float (args [0]);
	if (t < 1.0)
		return function_error (ei, _("acosh - domain error"));

Michael Meeks's avatar
Michael Meeks committed
366
	return value_new_float (acosh (t));
367
368
369
370
}

static char *help_asin = {
	N_("@FUNCTION=ASIN\n"
371
	   "@SYNTAX=ASIN(x)\n"
372
373

	   "@DESCRIPTION="
374
375
	   "The ASIN function calculates the arc sine of x; that is "
	   "the value whose sine is x. If x falls outside  the  range "
376
377
378
379
380
381
382
383
	   "-1 to 1, ASIN fails and returns the error 'asin - domain error'   "
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=SIN, COS, ASINH, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
384
static Value *
385
gnumeric_asin (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
386
387
388
{
	float_t t;

389
390
391
392
	t = value_get_as_float (args [0]);
	if ((t < -1.0) || (t > 1.0))
		return function_error (ei, _("asin - domain error"));

Michael Meeks's avatar
Michael Meeks committed
393
	return value_new_float (asin (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
394
395
}

396
397
static char *help_asinh = {
	N_("@FUNCTION=ASINH\n"
398
	   "@SYNTAX=ASINH(x)\n"
399
400

	   "@DESCRIPTION="
401
402
	   "The ASINH  function  calculates  the inverse hyperbolic "
	   " sine of x; that is the value whose hyperbolic sine is x. "
403
404
405
406
407
408
409
410
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=ASIN, SIN, COS, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
411
static Value *
412
gnumeric_asinh (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
413
{
414
	return value_new_float (asinh (value_get_as_float (args [0])));
415
}
Arturo Espinosa's avatar
Arturo Espinosa committed
416

417
418
static char *help_atan = {
	N_("@FUNCTION=ATAN\n"
419
	   "@SYNTAX=ATAN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
420

421
	   "@DESCRIPTION="
422
423
	   "The ATAN function calculates the arc tangent of x; that "
	   " is the value whose tangent is x."
424
425
426
427
428
429
430
431
432
	   "Return value is in radians."
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"

	   "@SEEALSO=TAN, COS, SIN, DEGREES, RADIANS")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
433
434

static Value *
435
gnumeric_atan (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
436
{
437
	return value_new_float (atan (value_get_as_float (args [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
438
439
}

440
441
static char *help_atanh = {
	N_("@FUNCTION=ATANH\n"
442
	   "@SYNTAX=ATANH(x)\n"
443
444
445

	   "@DESCRIPTION="
	   "The  ATANH  function  calculates  the inverse hyperbolic "
446
447
	   "tangent of x; that is the value whose  hyperbolic  tangent "
	   "is  x.   If  the  absolute value of x is greater than 1.0, "
448
449
450
451
452
453
454
455
456
	   " ATANH returns an error of 'atanh: domain error'      "
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=ATAN, TAN, SIN, COS, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
457
static Value *
458
gnumeric_atanh (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
459
460
461
{
	float_t t;

462
463
464
465
466
	t = value_get_as_float (args [0]);
	if ((t <= -1.0) || (t >= 1.0))
		return function_error (ei, _("atanh: domain error"));

	return value_new_float (atanh (value_get_as_float (args [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
467
468
}

469
static char *help_atan2 = {
470
	N_("@FUNCTION=ATAN2\n"
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
	   "@SYNTAX=ATAN2(b1,b2)\n"

	   "@DESCRIPTION="
	   "The ATAN2 function calculates the arc tangent of the two "
	   "variables b1 and b2.  It is similar to calculating  the  arc "
	   "tangent  of b2 / b1, except that the signs of both arguments "
	   "are used to determine the quadrant of the result. "
	   "The result is in Radians."
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "\n"
	   "@SEEALSO=ATAN, ATANH, COS, SIN, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
486
static Value *
487
gnumeric_atan2 (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
488
{
Morten Welinder's avatar
Morten Welinder committed
489
490
	return value_new_float (atan2 (value_get_as_float (args [1]),
				       value_get_as_float (args [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
491
492
}

493
494
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
495
	   "@SYNTAX=CEIL(x)\n"
496

497
498
	   "@DESCRIPTION=The CEIL function rounds x up to the next nearest "
	   "integer.\n"
499
500
501

	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
502

503
504
505
	   "@SEEALSO=ABS, FLOOR, INT")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
506
static Value *
507
gnumeric_ceil (FunctionEvalInfo *ei, Value **args)
Arturo Espinosa's avatar
Arturo Espinosa committed
508
{
509
	return value_new_float (ceil (value_get_as_float (args [0])));
510
511
}

512
513
514
515
516
517
518
519
520
static char *help_countif = {
	N_("@FUNCTION=COUNTIF\n"
	   "@SYNTAX=COUNTIF(range,criteria)\n"

	   "@DESCRIPTION="
	   "COUNTIF function counts the number of cells in the given range "
	   "that meet the given criteria. "

	   "\n"
Morten Welinder's avatar
Morten Welinder committed
521

522
523
524
525
	   "@SEEALSO=COUNT,SUMIF")
};

static Value *
526
gnumeric_countif (FunctionEvalInfo *ei, Value **argv)
527
528
{
        Value           *range = argv[0];
Morten Welinder's avatar
Morten Welinder committed
529
	Value           *tmpvalue = NULL;
530

531
532
533
534
535
536
537
538
	math_criteria_t items;
	int             ret;
	GSList          *list;

	items.num  = 0;
	items.list = NULL;

	if ((!VALUE_IS_NUMBER(argv[1]) && argv[1]->type != VALUE_STRING)
539
540
	    || (range->type != VALUE_CELLRANGE))
	        return function_error (ei, gnumeric_err_VALUE);
541
542
543
544

	if (VALUE_IS_NUMBER(argv[1])) {
	        items.fun = (criteria_test_fun_t) criteria_test_equal;
		items.test_value = argv[1];
Morten Welinder's avatar
Morten Welinder committed
545
	} else {
546
547
	        parse_criteria(argv[1]->v.str->str,
			       &items.fun, &items.test_value);
Morten Welinder's avatar
Morten Welinder committed
548
549
		tmpvalue = items.test_value;
	}
550
551

	ret = sheet_cell_foreach_range (
552
553
554
555
556
557
558
		range->v.cell_range.cell_a.sheet, TRUE,
		range->v.cell_range.cell_a.col,
		range->v.cell_range.cell_a.row,
		range->v.cell_range.cell_b.col,
		range->v.cell_range.cell_b.row,
		callback_function_criteria,
		&items);
Morten Welinder's avatar
Morten Welinder committed
559
560
561
562

	if (tmpvalue)
		value_release (tmpvalue);

563
564
	if (ret == FALSE)
	        return function_error (ei, gnumeric_err_VALUE);
565
566
567
568

        list = items.list;

	while (list != NULL) {
Morten Welinder's avatar
Morten Welinder committed
569
		value_release (list->data);
570
571
572
573
		list = list->next;
	}
	g_slist_free(items.list);

Michael Meeks's avatar
Michael Meeks committed
574
	return value_new_int (items.num);
575
576
577
578
579
580
581
582
583
584
585
}

static char *help_sumif = {
	N_("@FUNCTION=SUMIF\n"
	   "@SYNTAX=SUMIF(range,criteria)\n"

	   "@DESCRIPTION="
	   "SUMIF function sums the values in the given range that meet "
	   "the given criteria. "

	   "\n"
Morten Welinder's avatar
Morten Welinder committed
586

587
588
589
590
	   "@SEEALSO=COUNTIF,SUM")
};

static Value *
591
gnumeric_sumif (FunctionEvalInfo *ei, Value **argv)
592
593
{
        Value           *range = argv[0];
Morten Welinder's avatar
Morten Welinder committed
594
	Value           *tmpvalue = NULL;
595

596
597
598
599
600
601
602
603
604
	math_criteria_t items;
	int             ret;
	float_t         sum;
	GSList          *list;

	items.num  = 0;
	items.list = NULL;

	if ((!VALUE_IS_NUMBER(argv[1]) && argv[1]->type != VALUE_STRING)
605
606
	    || (range->type != VALUE_CELLRANGE))
	        return function_error (ei, gnumeric_err_VALUE);
607
608
609
610

	if (VALUE_IS_NUMBER(argv[1])) {
	        items.fun = (criteria_test_fun_t) criteria_test_equal;
		items.test_value = argv[1];
Morten Welinder's avatar
Morten Welinder committed
611
	} else {
612
613
	        parse_criteria(argv[1]->v.str->str,
			       &items.fun, &items.test_value);
Morten Welinder's avatar
Morten Welinder committed
614
615
		tmpvalue = items.test_value;
	}
616
617
618

	ret = sheet_cell_foreach_range (
	  range->v.cell_range.cell_a.sheet, TRUE,
Morten Welinder's avatar
Morten Welinder committed
619
	  range->v.cell_range.cell_a.col,
620
621
622
623
624
	  range->v.cell_range.cell_a.row,
	  range->v.cell_range.cell_b.col,
	  range->v.cell_range.cell_b.row,
	  callback_function_criteria,
	  &items);
Morten Welinder's avatar
Morten Welinder committed
625
626
627
628

	if (tmpvalue)
		value_release (tmpvalue);

629
630
	if (ret == FALSE)
	        return function_error (ei, gnumeric_err_VALUE);
631
632
633
634
635

        list = items.list;
	sum = 0;

	while (list != NULL) {
Morten Welinder's avatar
Morten Welinder committed
636
	        Value *v = list->data;
637
638

		if (v != NULL)
Michael Meeks's avatar
Michael Meeks committed
639
		       sum += value_get_as_float (v);
Morten Welinder's avatar
Morten Welinder committed
640
		value_release (v);
641
642
643
644
		list = list->next;
	}
	g_slist_free(items.list);

Michael Meeks's avatar
Michael Meeks committed
645
	return value_new_float (sum);
646
647
}

648
649
650
651
652
653
654
655
656
657
658
static char *help_ceiling = {
	N_("@FUNCTION=CEILING\n"
	   "@SYNTAX=CEILING(x,significance)\n"

	   "@DESCRIPTION=The CEILING function rounds x up to the nearest "
	   "multiple of significance. "
	   "\n"

	   "If x or significance is non-numeric CEILING returns #VALUE! error. "
	   "If n and significance have different signs CEILING returns #NUM! error. "
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
659

660
661
662
663
	   "@SEEALSO=CEIL")
};

static Value *
664
gnumeric_ceiling (FunctionEvalInfo *ei, Value **argv)
665
{
Morten Welinder's avatar
Morten Welinder committed
666
        float_t number, s;
667

Morten Welinder's avatar
Morten Welinder committed
668
669
670
671
672
	number = value_get_as_float (argv[0]);
	if (argv[1] == NULL)
	        s = (number >= 0) ? 1.0 : -1.0;
	else {
	        s = value_get_as_float (argv[1]);
673
	}
Morten Welinder's avatar
Morten Welinder committed
674

675
676
	if (s == 0 || number / s < 0)
		return function_error (ei, gnumeric_err_NUM);
677

Morten Welinder's avatar
Morten Welinder committed
678
	return value_new_float (ceil (number / s) * s);
679
680
}

681
682
static char *help_cos = {
	N_("@FUNCTION=COS\n"
683
	   "@SYNTAX=COS(x)\n"
684
685

	   "@DESCRIPTION="
686
	   "The  COS  function  returns  the cosine of x, where x is "
687
688
689
690
691
692
693
694
           "given in radians.  "
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=COSH, SIN, SINH, TAN, TANH, RADIANS, DEGREES")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
695
static Value *
696
gnumeric_cos (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
697
{
Michael Meeks's avatar
Michael Meeks committed
698
	return value_new_float (cos (value_get_as_float (argv [0])));
699
}
Arturo Espinosa's avatar
Arturo Espinosa committed
700

701
702
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
703
	   "@SYNTAX=COSH(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
704

705
	   "@DESCRIPTION="
706
707
708
	   "The COSH  function  returns the hyperbolic cosine of x, "
	   " which is defined mathematically as (exp(x) + exp(-x)) / 2.   "
	   " x is in radians. "
709
710
711
712
713
714
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=COS, SIN, SINH, TAN, TANH, RADIANS, DEGREES, EXP")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
715
716

static Value *
717
gnumeric_cosh (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
718
{
Michael Meeks's avatar
Michael Meeks committed
719
	return value_new_float (cosh (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
720
721
}

722
723
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
724
	   "@SYNTAX=DEGREES(x)\n"
725
726

	   "@DESCRIPTION="
727
728
	   "Computes the number of degrees equivalent to "
	   " x radians."
729
730
731
732
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
733

734
735
736
	   "@SEEALSO=RADIANS, PI")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
737
static Value *
738
gnumeric_degrees (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
739
{
Michael Meeks's avatar
Michael Meeks committed
740
	return value_new_float ((value_get_as_float (argv [0]) * 180.0) / M_PI);
741
}
Arturo Espinosa's avatar
Arturo Espinosa committed
742

743
744
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
745
	   "@SYNTAX=EXP(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
746

747
748
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
749
	   "to the power of x. "
750
751
752
753
754
	   "\n"
	   "Performing this function on a string or empty cell returns an error."
	   "\n"
	   "@SEEALSO=LOG, LOG2, LOG10")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
755
756

static Value *
757
gnumeric_exp (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
758
{
Michael Meeks's avatar
Michael Meeks committed
759
	return value_new_float (exp (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
760
761
}

762
float_t
763
764
765
766
767
768
769
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

770
771
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
772
	   "@SYNTAX=FACT(x)\n"
773
774

	   "@DESCRIPTION="
775
	   "Computes the factorial of x. ie, x!"
776
777
778
779
780
781
782
	   "\n"
	   "Performing this function on a string or empty cell returns an error"
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

783
static Value *
784
gnumeric_fact (FunctionEvalInfo *ei, Value **argv)
785
{
Morten Welinder's avatar
Morten Welinder committed
786
787
	float_t x;
	gboolean x_is_integer;
788

789
790
	if (!VALUE_IS_NUMBER (argv[0]))
		return function_error (ei, gnumeric_err_NUM);
791

Morten Welinder's avatar
Morten Welinder committed
792
	x = value_get_as_float (argv[0]);
793
794
795
	if (x < 0)
		return function_error (ei, gnumeric_err_NUM);

Morten Welinder's avatar
Morten Welinder committed
796
	x_is_integer = (x == floor (x));
Morten Welinder's avatar
Morten Welinder committed
797

Morten Welinder's avatar
Morten Welinder committed
798
799
800
801
802
803
804
	if (x > 12 || !x_is_integer) {
		float_t res = exp (lgamma (x + 1));
		if (x_is_integer)
			res = floor (res + 0.5);  /* Round, just in case.  */
		return value_new_float (res);
	} else
		return value_new_int (fact (x));
805
806
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
static char *help_combin = {
	N_("@FUNCTION=COMBIN\n"
	   "@SYNTAX=COMBIN(n,k)\n"

	   "@DESCRIPTION="
	   "Computes the number of combinations."
	   "\n"
	   "Performing this function on a non-integer or a negative number "
           "returns an error. Also if n is less than k returns an error."
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

float_t
combin (int n, int k)
{
824
825
826
827
828
829
830
831
832
833
834
	if (n >= 15) {
		float_t res;

		res = exp (lgamma (n + 1) - lgamma (k + 1) - lgamma (n - k + 1));
		return floor (res + 0.5);  /* Round, just in case.  */
	} else {
		float_t res;

		res = fact (n) / fact (k) / fact (n - k);
		return res;
	}
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
835
836
837
}

static Value *
838
gnumeric_combin (FunctionEvalInfo *ei, Value **argv)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
839
{
840
	int n ,k;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
841

842
843
844
845
	n = value_get_as_int (argv[0]);
	k = value_get_as_int (argv[1]);

	if (k >= 0 && n >= k)
Michael Meeks's avatar
Michael Meeks committed
846
		return value_new_float (combin (n ,k));
847

848
	return function_error (ei, gnumeric_err_NUM);
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
849
850
}

851
852
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
Morten Welinder's avatar
Morten Welinder committed
853
	   "@SYNTAX=FLOOR(x,significance)\n"
854

855
	   "@DESCRIPTION=The FLOOR function rounds x down to the next nearest "
Morten Welinder's avatar
Morten Welinder committed
856
	   "multiple of @significance.  @significance defaults to 1."
857
858
859
860
861
862
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=CEIL, ABS, INT")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
863
static Value *
864
gnumeric_floor (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
865
{
Morten Welinder's avatar
Morten Welinder committed
866
867
868
869
870
871
872
873
874
        float_t number, s;

	number = value_get_as_float (argv[0]);
	if (argv[1] == NULL)
	        s = (number >= 0) ? 1.0 : -1.0;
	else {
	        s = value_get_as_float (argv[1]);
	}

875
876
	if (s == 0 || number / s < 0)
		return function_error (ei, gnumeric_err_NUM);
Morten Welinder's avatar
Morten Welinder committed
877
878

	return value_new_float (floor (number / s) * s);
879
}
Arturo Espinosa's avatar
Arturo Espinosa committed
880

881
882
883
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
884

885
886
887
	   "@DESCRIPTION="
	   "The INT function round b1 now to the nearest int. "
	   "Where 'nearest' implies being closer to zero. "
Morten Welinder's avatar
Morten Welinder committed
888
	   "Equivalent to FLOOR(b1) for b1 >= 0, amd CEIL(b1) "
Morten Welinder's avatar
Morten Welinder committed
889
	   "for b1 < 0. "
890
891
892
893
894
895
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing."
	   ""
	   "\n"
	   "@SEEALSO=FLOOR, CEIL, ABS")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
896
897

static Value *
898
gnumeric_int (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
899
{
900
901
	float_t t;

Morten Welinder's avatar
Morten Welinder committed
902
	/* FIXME: What about strings and empty cells?  */
903
904
	t = value_get_as_float (argv [0]);

Morten Welinder's avatar
Morten Welinder committed
905
	return value_new_float (floor (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
906
907
}

908
909
static char *help_log = {
	N_("@FUNCTION=LOG\n"
910
	   "@SYNTAX=LOG(x[,base])\n"
911
912

	   "@DESCRIPTION="
913
914
	   "Computes the logarithm of x in the given base.  If no base is "
	   "given LOG returns the logarithm in base 10. "
915
	   "\n"
916
	   "@SEEALSO=LN, LOG2, LOG10")
917
918
};

Arturo Espinosa's avatar
Arturo Espinosa committed
919
static Value *
920
gnumeric_log (FunctionEvalInfo *ei, Value **argv)
921
922
923
{
	float_t t, base;

Michael Meeks's avatar
Michael Meeks committed
924
	t = value_get_as_float (argv [0]);
925
926
927
928

	if (argv[1] == NULL)
	        base = 10;
	else
Michael Meeks's avatar
Michael Meeks committed
929
	        base = value_get_as_float (argv[1]);
930

931
932
	if (t <= 0.0)
		return function_error (ei, gnumeric_err_VALUE);
933

Michael Meeks's avatar
Michael Meeks committed
934
	return value_new_float (log (t) / log (base));
935
936
937
938
939
940
941
942
943
944
945
946
947
}

static char *help_ln = {
	N_("@FUNCTION=LN\n"
	   "@SYNTAX=LN(x)\n"

	   "@DESCRIPTION="
	   "LN returns the natural logarithm of x. "
	   "\n"
	   "@SEEALSO=EXP, LOG2, LOG10")
};

static Value *
948
gnumeric_ln (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
949
950
951
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
952
	t = value_get_as_float (argv [0]);
953

954
955
	if (t <= 0.0)
		return function_error (ei, gnumeric_err_VALUE);
956

Michael Meeks's avatar
Michael Meeks committed
957
	return value_new_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
958
959
}

960
961
962
963
964
static char *help_power = {
	N_("@FUNCTION=POWER\n"
	   "@SYNTAX=POWER(x,y)\n"

	   "@DESCRIPTION="
Morten Welinder's avatar
Morten Welinder committed
965
	   "Returns the value of x raised to the power y."
966
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
967
	   "Performing this function on a string or empty cell returns an error."
968
969
970
971
972
	   "\n"
	   "@SEEALSO=EXP")
};

static Value *
973
gnumeric_power (FunctionEvalInfo *ei, Value **argv)
974
{
Morten Welinder's avatar
Morten Welinder committed
975
976
977
978
979
980
981
982
983
	float_t x, y;

	x = value_get_as_float (argv [0]);
	y = value_get_as_float (argv [1]);

	if ((x > 0) || (x == 0 && y > 0) || (x < 0 && y == floor (y)))
		return value_new_float (pow (x, y));

	/* FIXME: What is supposed to happen for x=y=0?  */
984
	return function_error (ei, gnumeric_err_VALUE);
985
986
}

987
static char *help_log2 = {
988
	N_("@FUNCTION=LOG2\n"
989
	   "@SYNTAX=LOG2(x)\n"
990
991

	   "@DESCRIPTION="
992
	   "Computes the base-2 logarithm  of x. "
993
994
995
996
997
998
	   "\n"
	   "Performing this function on a string or empty cell returns an error. "
	   "\n"
	   "@SEEALSO=EXP, LOG10, LOG")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
999
static Value *
1000
gnumeric_log2 (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
1001
1002
1003
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
1004
	t = value_get_as_float (argv [0]);
1005
1006
1007
	if (t <= 0.0)
		return function_error (ei, _("log2: domain error"));

Michael Meeks's avatar
Michael Meeks committed
1008
	return value_new_float (log (t) / M_LN2);
Arturo Espinosa's avatar
Arturo Espinosa committed
1009
1010
}

1011
static char *help_log10 = {
1012
	N_("@FUNCTION=LOG10\n"
1013
	   "@SYNTAX=LOG10(x)\n"
1014
1015

	   "@DESCRIPTION="
1016
	   "Computes the base-10 logarithm  of x. "
1017
1018
1019
1020
1021
1022
1023
	   "\n"

	   "Performing this function on a string or empty cell returns an error. "
	   "\n"
	   "@SEEALSO=EXP, LOG2, LOG")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1024
static Value *
1025
gnumeric_log10 (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
1026
1027
1028
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
1029
	t = value_get_as_float (argv [0]);
1030
1031
1032
	if (t <= 0.0)
		return function_error (ei, _("log10: domain error"));

Michael Meeks's avatar
Michael Meeks committed
1033
	return value_new_float (log10 (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
1034
1035
}

1036
1037
1038
1039
1040
1041
static char *help_mod = {
	N_("@FUNCTION=MOD\n"
	   "@SYNTAX=MOD(number,divisor)\n"

	   "@DESCRIPTION="
	   "Implements modulo arithmetic."
Morten Welinder's avatar
Morten Welinder committed
1042
	   "Returns the remainder when @divisor is divided into @number."
1043
1044
1045
1046
1047
1048
	   "\n"
	   "Returns #DIV/0! if divisor is zero."
	   "@SEEALSO=INT,FLOOR,CEIL")
};

static Value *
1049
gnumeric_mod (FunctionEvalInfo *ei, Value **argv)
1050
1051
{
	int a,b;
1052

1053
1054
1055
	a = value_get_as_int (argv[0]);
	b = value_get_as_int (argv[1]);

1056
1057
	if (b == 0)
		return function_error (ei, gnumeric_err_NUM);
Morten Welinder's avatar
Morten Welinder committed
1058
1059

	if (b < 0) {
1060
1061
		a = -a;
		b = -b;
Morten Welinder's avatar
Morten Welinder committed
1062
		/* FIXME: check for overflow.  */
1063
	}
Morten Welinder's avatar
Morten Welinder committed
1064

Morten Welinder's avatar
Morten Welinder committed
1065
1066
1067
1068
1069
1070
	if (a >= 0)
		return value_new_int (a % b);
	else {
		int invres = (-a) % b;
		return value_new_int (invres == 0 ? 0 : b - invres);
	}
1071
1072
}

1073
1074
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
1075
	   "@SYNTAX=RADIANS(x)\n"
1076
1077

	   "@DESCRIPTION="
1078
1079
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
1080
1081
1082
1083
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
1084

1085
1086
	   "@SEEALSO=PI,DEGREES")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
1087
1088

static Value *
1089
gnumeric_radians (FunctionEvalInfo *ei, Value **argv)
Arturo Espinosa's avatar
Arturo Espinosa committed
1090
{
Michael Meeks's avatar
Michael Meeks committed
1091
	return value_new_float ((value_get_as_float (argv [0]) * M_PI) / 180);
1092
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1093

1094
1095
1096
1097
1098
1099
1100
1101
static char *help_rand = {
	N_("@FUNCTION=RAND\n"
	   "@SYNTAX=RAND()\n"

	   "@DESCRIPTION="
	   "Returns a random number greater than or equal to 0 and less than 1."
	   "\n"
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
1102

1103
1104
1105
1106
	   "@SEEALSO=")
};

static Value *