functions.c 63.6 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
static int
callback_function_makeslist (Sheet *sheet, Value *value,
			     char **error_string, void *closure)
{
        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
194
gnumeric_gcd (Sheet *sheet, GList *expr_node_list,
	      int eval_col, int eval_row, char **error_string)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
195
{
196
197
198
        math_sums_t p;
	GSList      *current;
        int         a, b, old_gcd, new_gcd;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
199

200
201
202
203
204
205
	p.num  = 0;
	p.list = NULL;
	if (function_iterate_argument_values (sheet, 
					      callback_function_makeslist,
					      &p, expr_node_list,
					      eval_col, eval_row,
206
207
208
					      error_string, TRUE) == FALSE) {
		if (!*error_string)
			*error_string = gnumeric_err_NUM;
209
210
		return NULL;
	}
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
211

212
	if (p.list == NULL || p.list->next == NULL) {
Morten Welinder's avatar
Morten Welinder committed
213
		*error_string = gnumeric_err_NUM;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
214
215
		return NULL;
	}
Morten Welinder's avatar
Morten Welinder committed
216

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
243
244
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
callback_function_lcm (Sheet *sheet, Value *value,
		       char **error_string, void *closure)
{
	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
276

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
277
278
279
280
	return TRUE;
}

static Value *
Morten Welinder's avatar
Morten Welinder committed
281
gnumeric_lcm (Sheet *sheet, GList *expr_node_list,
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
282
283
284
285
286
287
288
289
290
291
	      int eval_col, int eval_row, char **error_string)
{
	Value *result;

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

	if (function_iterate_argument_values (sheet, callback_function_lcm,
					      result, expr_node_list,
Morten Welinder's avatar
Morten Welinder committed
292
					      eval_col, eval_row,
293
294
295
					      error_string, TRUE) == FALSE) {
		if (!*error_string)
			*error_string = gnumeric_err_NUM;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
296
297
298
299
300
301
		return NULL;
	}

	return result;
}

302
303
304
static char *help_abs = {
	N_("@FUNCTION=ABS\n"
	   "@SYNTAX=ABS(b1)\n"
305

306
307
308
	   "@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."
309
	   "\n"
310
	   "Performing this function on a string or empty cell simply does nothing."
311
	   "\n"
312
	   "@SEEALSO=CEIL, FLOOR")
313
314
};

315
static Value *
316
317
gnumeric_abs (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
318
{
Michael Meeks's avatar
Michael Meeks committed
319
	return value_new_float (fabs (value_get_as_float (argv [0])));
320
}
321
322

static char *help_acos = {
323
	N_("@FUNCTION=ACOS\n"
324
	   "@SYNTAX=ACOS(x)\n"
325

326
	   "@DESCRIPTION="
327
328
	   "The ACOS function calculates the arc cosine of x; that "
	   " is the value whose cosine is x.  If x  falls  outside  the "
329
330
	   " range -1 to 1, ACOS fails and returns the error 'acos - domain error'. "
	   " The value it returns is in radians. "
331
	   "\n"
332
333
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
334
335
	   "\n"
	   "@SEEALSO=COS, SIN, DEGREES, RADIANS")
336
337
};

338
static Value *
339
340
gnumeric_acos (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
341
342
{
	float_t t;
Donnie Barnes's avatar
Donnie Barnes committed
343

Michael Meeks's avatar
Michael Meeks committed
344
	t = value_get_as_float (argv [0]);
345
346
347
348
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("acos - domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
349
	return value_new_float (acos (t));
350
351
352
353
}

static char *help_acosh = {
	N_("@FUNCTION=ACOSH\n"
354
	   "@SYNTAX=ACOSH(x)\n"
355
356
357

	   "@DESCRIPTION="
	   "The ACOSH  function  calculates  the inverse hyperbolic "
358
359
	   "cosine of x; that is the value whose hyperbolic cosine is "
	   "x. If x is less than 1.0, acosh() returns the error "
360
	   " 'acosh - domain error'"
Arturo Espinosa's avatar
Arturo Espinosa committed
361
	   "\n"
362
363
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
364
	   "\n"
365
	   "@SEEALSO=ACOS, DEGREES, RADIANS ")
Donnie Barnes's avatar
Donnie Barnes committed
366
367
};

368
static Value *
369
370
gnumeric_acosh (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
371
372
373
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
374
	t = value_get_as_float (argv [0]);
375
376
377
378
	if (t < 1.0){
		*error_string = _("acosh - domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
379
	return value_new_float (acosh (t));
380
381
382
383
}

static char *help_asin = {
	N_("@FUNCTION=ASIN\n"
384
	   "@SYNTAX=ASIN(x)\n"
385
386

	   "@DESCRIPTION="
387
388
	   "The ASIN function calculates the arc sine of x; that is "
	   "the value whose sine is x. If x falls outside  the  range "
389
390
391
392
393
394
395
396
	   "-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
397
static Value *
398
399
gnumeric_asin (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
400
401
402
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
403
	t = value_get_as_float (argv [0]);
Arturo Espinosa's avatar
Arturo Espinosa committed
404
405
406
407
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("asin - domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
408
	return value_new_float (asin (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
409
410
}

411
412
static char *help_asinh = {
	N_("@FUNCTION=ASINH\n"
413
	   "@SYNTAX=ASINH(x)\n"
414
415

	   "@DESCRIPTION="
416
417
	   "The ASINH  function  calculates  the inverse hyperbolic "
	   " sine of x; that is the value whose hyperbolic sine is x. "
418
419
420
421
422
423
424
425
	   "\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
426
static Value *
427
428
gnumeric_asinh (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
429
{
Michael Meeks's avatar
Michael Meeks committed
430
	return value_new_float (asinh (value_get_as_float (argv [0])));
431
}
Arturo Espinosa's avatar
Arturo Espinosa committed
432

433
434
static char *help_atan = {
	N_("@FUNCTION=ATAN\n"
435
	   "@SYNTAX=ATAN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
436

437
	   "@DESCRIPTION="
438
439
	   "The ATAN function calculates the arc tangent of x; that "
	   " is the value whose tangent is x."
440
441
442
443
444
445
446
447
448
	   "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
449
450

static Value *
451
452
gnumeric_atan (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
453
{
Michael Meeks's avatar
Michael Meeks committed
454
	return value_new_float (atan (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
455
456
}

457
458
static char *help_atanh = {
	N_("@FUNCTION=ATANH\n"
459
	   "@SYNTAX=ATANH(x)\n"
460
461
462

	   "@DESCRIPTION="
	   "The  ATANH  function  calculates  the inverse hyperbolic "
463
464
	   "tangent of x; that is the value whose  hyperbolic  tangent "
	   "is  x.   If  the  absolute value of x is greater than 1.0, "
465
466
467
468
469
470
471
472
473
	   " 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
474
static Value *
475
476
gnumeric_atanh (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
477
478
479
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
480
	t = value_get_as_float (argv [0]);
Arturo Espinosa's avatar
Arturo Espinosa committed
481
482
483
484
	if ((t <= -1.0) || (t >= 1.0)){
		*error_string = _("atanh: domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
485
	return value_new_float (atanh (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
486
487
}

488
static char *help_atan2 = {
489
	N_("@FUNCTION=ATAN2\n"
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
	   "@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
505
static Value *
506
507
gnumeric_atan2 (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
508
{
Morten Welinder's avatar
Morten Welinder committed
509
510
	return value_new_float (atan2 (value_get_as_float (argv [1]),
				       value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
511
512
}

513
514
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
515
	   "@SYNTAX=CEIL(x)\n"
516

517
518
	   "@DESCRIPTION=The CEIL function rounds x up to the next nearest "
	   "integer.\n"
519
520
521

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

523
524
525
	   "@SEEALSO=ABS, FLOOR, INT")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
526
static Value *
527
528
gnumeric_ceil (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
529
{
Michael Meeks's avatar
Michael Meeks committed
530
	return value_new_float (ceil (value_get_as_float (argv [0])));
531
532
}

533
534
535
536
537
538
539
540
541
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
542

543
544
545
546
547
548
549
550
	   "@SEEALSO=COUNT,SUMIF")
};

static Value *
gnumeric_countif (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
{
        Value           *range = argv[0];
Morten Welinder's avatar
Morten Welinder committed
551
	Value           *tmpvalue = NULL;
552
553
554
555
556
557
558
559
560
	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)
	    || (range->type != VALUE_CELLRANGE)) {
Morten Welinder's avatar
Morten Welinder committed
561
		*error_string = gnumeric_err_VALUE;
562
563
564
565
566
567
		return NULL;
	}

	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
568
	} else {
569
570
	        parse_criteria(argv[1]->v.str->str,
			       &items.fun, &items.test_value);
Morten Welinder's avatar
Morten Welinder committed
571
572
		tmpvalue = items.test_value;
	}
573
574
575

	ret = sheet_cell_foreach_range (
	  range->v.cell_range.cell_a.sheet, TRUE,
Morten Welinder's avatar
Morten Welinder committed
576
	  range->v.cell_range.cell_a.col,
577
578
579
580
581
	  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
582
583
584
585

	if (tmpvalue)
		value_release (tmpvalue);

586
	if (ret == FALSE) {
Morten Welinder's avatar
Morten Welinder committed
587
		*error_string = gnumeric_err_VALUE;
588
589
590
591
592
593
		return NULL;
	}

        list = items.list;

	while (list != NULL) {
Morten Welinder's avatar
Morten Welinder committed
594
		value_release (list->data);
595
596
597
598
		list = list->next;
	}
	g_slist_free(items.list);

Michael Meeks's avatar
Michael Meeks committed
599
	return value_new_int (items.num);
600
601
602
603
604
605
606
607
608
609
610
}

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
611

612
613
614
615
616
617
618
619
	   "@SEEALSO=COUNTIF,SUM")
};

static Value *
gnumeric_sumif (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
{
        Value           *range = argv[0];
Morten Welinder's avatar
Morten Welinder committed
620
	Value           *tmpvalue = NULL;
621
622
623
624
625
626
627
628
629
630
	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)
	    || (range->type != VALUE_CELLRANGE)) {
Morten Welinder's avatar
Morten Welinder committed
631
		*error_string = gnumeric_err_VALUE;
632
633
634
635
636
637
		return NULL;
	}

	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
638
	} else {
639
640
	        parse_criteria(argv[1]->v.str->str,
			       &items.fun, &items.test_value);
Morten Welinder's avatar
Morten Welinder committed
641
642
		tmpvalue = items.test_value;
	}
643
644
645

	ret = sheet_cell_foreach_range (
	  range->v.cell_range.cell_a.sheet, TRUE,
Morten Welinder's avatar
Morten Welinder committed
646
	  range->v.cell_range.cell_a.col,
647
648
649
650
651
	  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
652
653
654
655

	if (tmpvalue)
		value_release (tmpvalue);

656
	if (ret == FALSE) {
Morten Welinder's avatar
Morten Welinder committed
657
		*error_string = gnumeric_err_VALUE;
658
659
660
661
662
663
664
		return NULL;
	}

        list = items.list;
	sum = 0;

	while (list != NULL) {
Morten Welinder's avatar
Morten Welinder committed
665
	        Value *v = list->data;
666
667

		if (v != NULL)
Michael Meeks's avatar
Michael Meeks committed
668
		       sum += value_get_as_float (v);
Morten Welinder's avatar
Morten Welinder committed
669
		value_release (v);
670
671
672
673
		list = list->next;
	}
	g_slist_free(items.list);

Michael Meeks's avatar
Michael Meeks committed
674
	return value_new_float (sum);
675
676
}

677
678
679
680
681
682
683
684
685
686
687
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
688

689
690
691
692
	   "@SEEALSO=CEIL")
};

static Value *
693
694
gnumeric_ceiling (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
695
{
Morten Welinder's avatar
Morten Welinder committed
696
        float_t number, s;
697

Morten Welinder's avatar
Morten Welinder committed
698
699
700
701
702
	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]);
703
	}
Morten Welinder's avatar
Morten Welinder committed
704
705

	if (s == 0 || number / s < 0) {
Morten Welinder's avatar
Morten Welinder committed
706
		*error_string = gnumeric_err_NUM;
Morten Welinder's avatar
Morten Welinder committed
707
		return NULL;
708
709
	}

Morten Welinder's avatar
Morten Welinder committed
710
	return value_new_float (ceil (number / s) * s);
711
712
}

713
714
static char *help_cos = {
	N_("@FUNCTION=COS\n"
715
	   "@SYNTAX=COS(x)\n"
716
717

	   "@DESCRIPTION="
718
	   "The  COS  function  returns  the cosine of x, where x is "
719
720
721
722
723
724
725
726
           "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
727
static Value *
728
729
gnumeric_cos (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
730
{
Michael Meeks's avatar
Michael Meeks committed
731
	return value_new_float (cos (value_get_as_float (argv [0])));
732
}
Arturo Espinosa's avatar
Arturo Espinosa committed
733

734
735
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
736
	   "@SYNTAX=COSH(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
737

738
	   "@DESCRIPTION="
739
740
741
	   "The COSH  function  returns the hyperbolic cosine of x, "
	   " which is defined mathematically as (exp(x) + exp(-x)) / 2.   "
	   " x is in radians. "
742
743
744
745
746
747
	   "\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
748
749

static Value *
750
751
gnumeric_cosh (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
752
{
Michael Meeks's avatar
Michael Meeks committed
753
	return value_new_float (cosh (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
754
755
}

756
757
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
758
	   "@SYNTAX=DEGREES(x)\n"
759
760

	   "@DESCRIPTION="
761
762
	   "Computes the number of degrees equivalent to "
	   " x radians."
763
764
765
766
	   "\n"

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

768
769
770
	   "@SEEALSO=RADIANS, PI")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
771
static Value *
772
773
gnumeric_degrees (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
774
{
Michael Meeks's avatar
Michael Meeks committed
775
	return value_new_float ((value_get_as_float (argv [0]) * 180.0) / M_PI);
776
}
Arturo Espinosa's avatar
Arturo Espinosa committed
777

778
779
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
780
	   "@SYNTAX=EXP(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
781

782
783
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
784
	   "to the power of x. "
785
786
787
788
789
	   "\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
790
791

static Value *
792
793
gnumeric_exp (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
794
{
Michael Meeks's avatar
Michael Meeks committed
795
	return value_new_float (exp (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
796
797
}

798
float_t
799
800
801
802
803
804
805
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

806
807
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
808
	   "@SYNTAX=FACT(x)\n"
809
810

	   "@DESCRIPTION="
811
	   "Computes the factorial of x. ie, x!"
812
813
814
815
816
817
818
	   "\n"
	   "Performing this function on a string or empty cell returns an error"
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

819
static Value *
820
821
gnumeric_fact (struct FunctionDefinition *id,
	       Value *argv [], char **error_string)
822
{
Morten Welinder's avatar
Morten Welinder committed
823
824
	float_t x;
	gboolean x_is_integer;
825

Morten Welinder's avatar
Morten Welinder committed
826
	if (!VALUE_IS_NUMBER (argv[0])) {
Morten Welinder's avatar
Morten Welinder committed
827
		*error_string = gnumeric_err_NUM;
828
829
830
		return NULL;
	}

Morten Welinder's avatar
Morten Welinder committed
831
832
	x = value_get_as_float (argv[0]);
	if (x < 0){
Morten Welinder's avatar
Morten Welinder committed
833
		*error_string = gnumeric_err_NUM;
834
835
		return NULL;
	}
Morten Welinder's avatar
Morten Welinder committed
836
	x_is_integer = (x == floor (x));
Morten Welinder's avatar
Morten Welinder committed
837

Morten Welinder's avatar
Morten Welinder committed
838
839
840
841
842
843
844
	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));
845
846
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
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)
{
864
865
866
867
868
869
870
871
872
873
874
	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
875
876
877
}

static Value *
878
879
gnumeric_combin (struct FunctionDefinition *id,
		 Value *argv [], char **error_string)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
880
{
881
	int n ,k;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
882

883
884
885
886
	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
887
		return value_new_float (combin (n ,k));
888

Morten Welinder's avatar
Morten Welinder committed
889
	*error_string = gnumeric_err_NUM;
890
	return NULL;
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
891
892
}

893
894
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
Morten Welinder's avatar
Morten Welinder committed
895
	   "@SYNTAX=FLOOR(x,significance)\n"
896

897
	   "@DESCRIPTION=The FLOOR function rounds x down to the next nearest "
Morten Welinder's avatar
Morten Welinder committed
898
	   "multiple of @significance.  @significance defaults to 1."
899
900
901
902
903
904
	   "\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
905
static Value *
906
907
gnumeric_floor (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
908
{
Morten Welinder's avatar
Morten Welinder committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
        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]);
	}

	if (s == 0 || number / s < 0) {
		*error_string = gnumeric_err_NUM;
		return NULL;
	}

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

926
927
928
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
929

930
931
932
	   "@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
933
	   "Equivalent to FLOOR(b1) for b1 >= 0, amd CEIL(b1) "
Morten Welinder's avatar
Morten Welinder committed
934
	   "for b1 < 0. "
935
936
937
938
939
940
	   "\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
941
942

static Value *
943
944
gnumeric_int (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
945
{
Morten Welinder's avatar
Morten Welinder committed
946
	/* FIXME: What about strings and empty cells?  */
Morten Welinder's avatar
Morten Welinder committed
947
	return value_new_float (floor (value_get_as_float (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
948
949
}

950
951
static char *help_log = {
	N_("@FUNCTION=LOG\n"
952
	   "@SYNTAX=LOG(x[,base])\n"
953
954

	   "@DESCRIPTION="
955
956
	   "Computes the logarithm of x in the given base.  If no base is "
	   "given LOG returns the logarithm in base 10. "
957
	   "\n"
958
	   "@SEEALSO=LN, LOG2, LOG10")
959
960
};

Arturo Espinosa's avatar
Arturo Espinosa committed
961
static Value *
962
963
gnumeric_log (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
964
965
966
{
	float_t t, base;

Michael Meeks's avatar
Michael Meeks committed
967
	t = value_get_as_float (argv [0]);
968
969
970
971

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

	if (t <= 0.0) {
Morten Welinder's avatar
Morten Welinder committed
975
		*error_string = gnumeric_err_VALUE;
976
977
978
		return NULL;
	}

Michael Meeks's avatar
Michael Meeks committed
979
	return value_new_float (log (t) / log (base));
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
}

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 *
gnumeric_ln (struct FunctionDefinition *i,
	     Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
995
996
997
{
	float_t t;

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

1000
	if (t <= 0.0){
Morten Welinder's avatar
Morten Welinder committed
1001
		*error_string = gnumeric_err_VALUE;
Arturo Espinosa's avatar
Arturo Espinosa committed
1002
1003
		return NULL;
	}
1004

Michael Meeks's avatar
Michael Meeks committed
1005
	return value_new_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
1006
1007
}

1008
1009
1010
1011
1012
static char *help_power = {
	N_("@FUNCTION=POWER\n"
	   "@SYNTAX=POWER(x,y)\n"

	   "@DESCRIPTION="
Morten Welinder's avatar
Morten Welinder committed
1013
	   "Returns the value of x raised to the power y."
1014
	   "\n"
Morten Welinder's avatar
Morten Welinder committed
1015
	   "Performing this function on a string or empty cell returns an error."
1016
1017
1018
1019
1020
	   "\n"
	   "@SEEALSO=EXP")
};

static Value *
1021
1022
gnumeric_power (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
1023
{
Morten Welinder's avatar
Morten Welinder committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
	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?  */
Morten Welinder's avatar
Morten Welinder committed
1033
	*error_string = gnumeric_err_VALUE;
Morten Welinder's avatar
Morten Welinder committed
1034
	return NULL;
1035
1036
}

1037
static char *help_log2 = {
1038
	N_("@FUNCTION=LOG2\n"
1039
	   "@SYNTAX=LOG2(x)\n"
1040
1041

	   "@DESCRIPTION="
1042
	   "Computes the base-2 logarithm  of x. "
1043
1044
1045
1046
1047
1048
	   "\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
1049
static Value *
1050
1051
gnumeric_log2 (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1052
1053
1054
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
1055
	t = value_get_as_float (argv [0]);
1056
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
1057
1058
1059
		*error_string = _("log2: domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
1060
	return value_new_float (log (t) / M_LN2);
Arturo Espinosa's avatar
Arturo Espinosa committed
1061
1062
}

1063
static char *help_log10 = {
1064
	N_("@FUNCTION=LOG10\n"
1065
	   "@SYNTAX=LOG10(x)\n"
1066
1067

	   "@DESCRIPTION="
1068
	   "Computes the base-10 logarithm  of x. "
1069
1070
1071
1072
1073
1074
1075
	   "\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
1076
static Value *
1077
1078
gnumeric_log10 (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1079
1080
1081
{
	float_t t;

Michael Meeks's avatar
Michael Meeks committed
1082
	t = value_get_as_float (argv [0]);
1083
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
1084
1085
1086
		*error_string = _("log10: domain error");
		return NULL;
	}
Michael Meeks's avatar
Michael Meeks committed
1087
	return value_new_float (log10 (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
1088
1089
}

1090
1091
1092
1093
1094
1095
static char *help_mod = {
	N_("@FUNCTION=MOD\n"
	   "@SYNTAX=MOD(number,divisor)\n"

	   "@DESCRIPTION="
	   "Implements modulo arithmetic."
Morten Welinder's avatar
Morten Welinder committed
1096
	   "Returns the remainder when @divisor is divided into @number."
1097
1098
1099
1100
1101
1102
	   "\n"
	   "Returns #DIV/0! if divisor is zero."
	   "@SEEALSO=INT,FLOOR,CEIL")
};

static Value *
1103
1104
gnumeric_mod (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
1105
1106
{
	int a,b;
1107

1108
1109
1110
	a = value_get_as_int (argv[0]);
	b = value_get_as_int (argv[1]);

Morten Welinder's avatar
Morten Welinder committed
1111
1112
	if (b == 0) {
		*error_string = gnumeric_err_DIV0;
1113
		return NULL;
1114
	}
Morten Welinder's avatar
Morten Welinder committed
1115
1116

	if (b < 0) {
1117
1118
		a = -a;
		b = -b;
Morten Welinder's avatar
Morten Welinder committed
1119
		/* FIXME: check for overflow.  */
1120
	}
Morten Welinder's avatar
Morten Welinder committed
1121

Morten Welinder's avatar
Morten Welinder committed
1122
1123
1124
1125
1126
1127
	if (a >= 0)
		return value_new_int (a % b);
	else {
		int invres = (-a) % b;
		return value_new_int (invres == 0 ? 0 : b - invres);
	}
1128
1129
}

1130
1131
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
1132
	   "@SYNTAX=RADIANS(x)\n"
1133
1134

	   "@DESCRIPTION="
1135
1136
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
1137
1138
1139
1140
	   "\n"

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