functions.c 19.5 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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 */
#include <config.h>
#include <gnome.h>
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"

/* Some forward declarations */
static Value *gnumeric_sum         (void *tsheet, GList *expr_node_list,
				    int eval_col, int eval_row,
				    char **error_string);

static Value *gnumeric_count       (void *tsheet, GList *expr_node_list,
				    int eval_col, int eval_row,
				    char **error_string);

static Value *
26
gnumeric_abs (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
27
28
29
30
31
32
33
34
35
36
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = fabs (value_get_as_double (argv [0]));

	return v;
}

static Value *
37
gnumeric_acos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("acos - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = acos (t);

	return v;
}

static Value *
55
gnumeric_acosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 1.0){
		*error_string = _("acosh - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = acosh (t);

	return v;
}

static int
callback_function_and (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		if (value->v.v_int == 0){
			result->v.v_int = 0;
			return FALSE;
		} else
			result->v.v_int = 1;
		break;

	case VALUE_FLOAT:
		if (value->v.v_float == 0.0){
			result->v.v_int = 0;
			return FALSE;
		} else
			result->v.v_int = 1;

	default:
		/* ignore strings */
	}
	
	return TRUE;
}

Donnie Barnes's avatar
Donnie Barnes committed
100
101
102
103
104
105
106
107
108
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#if 0
/* help template */
static char *help_ = {
	N_("<function></function>"
	   "<syntax>(b1, b2, ...)</syntax>n"

	   "<description>"
	   ""
	   "<p>"

	   ""
	   ""
	   "<p>"
	   
	   ""
	   ""
	   ""
	   "</description>"
	   "<seealso></seealso>")
};

#endif

static char *help_floor = {
	N_("<function>FLOOR</function>"
	   "<syntax>FLOOR(b1)</syntax>n"

	   "<description>The FLOOR function rounds b1 down to the next nearest"
	   "integer."
	   "<p>"

	   "Performing this function on a string or empty cell simply does nothing."
	   "<p>"
	   
	   "</description>"
	   "<seealso>CEIL, ABS</seealso>")
};

static char *help_ceil = {
	N_("<function>CEIL</function>"
	   "<syntax>CEIL(b1)</syntax>n"

	   "<description>The CEIL function rounds b1 up to the next nearest"
	   "integer."
	   "<p>"

	   "Performing this function on a string or empty cell simply does nothing."
	   "<p>"
	   
	   "</description>"
	   "<seealso>ABS, FLOOR</seealso>")
};

static char *help_abs = {
	N_("<function>ABS</function>"
	   "<syntax>ABS(b1)</syntax>n"

	   "<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.<p>"

	   "Performing this function on a string or empty cell simply does nothing."
	   "<p>"
	   
	   "</description>"
	   "<seealso>CEIL, FLOOR</seealso>")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
168
169
170
171
172
static char *help_and = {
	N_("<function>AND</function>"
	   "<syntax>AND(b1, b2, ...)</syntax>n"

	   "<description>Implements the logical AND function: the result is TRUE "
Donnie Barnes's avatar
Donnie Barnes committed
173
	   "if all of the expression evaluates to TRUE, otherwise it returns "
Arturo Espinosa's avatar
Arturo Espinosa committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	   "FALSE.<p>"

	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE."
	   "If an integer or floating point value is provided zero is considered "
	   "FALSE and anything else is TRUE.<p>"
	   
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!' "
	   "is returned. "
	   "</description>"
	   "<seealso>OR</seealso>")
};

static Value *
gnumeric_and (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

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

	function_iterate_argument_values (sheet, callback_function_and,
					  result, expr_node_list,
					  eval_col, eval_row, error_string);

	/* See if there was any value worth using */
	if (result->v.v_int == -1){
		value_release (result);
		*error_string = _("#VALUE");
		return NULL;
	}
	return result;
}

static Value *
211
gnumeric_asin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("asin - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = asin (t);

	return v;
}

static Value *
229
gnumeric_asinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
230
231
232
233
234
235
236
237
238
239
240
{
	Value *v;

	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = asinh (value_get_as_double (argv [0]));

	return v;
}

static Value *
241
gnumeric_atan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
242
243
244
245
246
247
248
249
250
251
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = atan (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
252
gnumeric_atanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{
	Value *v = g_new (Value, 1);
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t <= -1.0) || (t >= 1.0)){
		*error_string = _("atanh: domain error");
		return NULL;
	}
	v->type = VALUE_FLOAT;
	v->v.v_float = atanh (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
269
gnumeric_atan2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = atan2 (value_get_as_double (argv [0]),
			      value_get_as_double (argv [1]));
	
	return v;
}

static char *help_average = {
	N_("<function>AVERAGE</function>"
	   "<syntax>AVERAGE(value1, value2,...)</syntax>"

	   "<description>"
	   "Computes the average of all the values and cells referenced in the "
	   "argument list.  This is equivalent to the sum of the arguments divided "
	   "by the count of the arguments."
	   "</description>"
	   "<seealso>SUM, COUNT</seealso>")
};

static Value *
gnumeric_average (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Value *sum, *count;

	sum = gnumeric_sum (tsheet, expr_node_list, eval_col, eval_row, error_string);
	if (!sum)
		return NULL;
	
	count = gnumeric_count (tsheet, expr_node_list, eval_col, eval_row, error_string);
	if (!count){
		value_release (sum);
		return NULL;
	}

	result = g_new (Value, 1);
	result->type = VALUE_FLOAT;
	result->v.v_float = sum->v.v_float / count->v.v_int;
	
	value_release (count);
	value_release (sum);
	
	return result;
}

static Value *
319
gnumeric_ceil (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
320
321
322
323
324
325
326
327
328
329
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = ceil (value_get_as_double (argv [0]));

	return v;
}

static Value *
330
gnumeric_bin2dec (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
{
	Value *value;
	int  result, v, n, bit;
	char *p;

	result = 0;
	switch (argv [0]->type){
	case VALUE_INTEGER:
		v = argv [0]->v.v_int;
		n = 0;
		for (n = 0; v; n++){
			bit = v % 10;
			v   = v / 10;
			result |= bit << n;
		}
		break;
		
	case VALUE_STRING:
		p = argv [0]->v.str->str;
350
		for (;*p; p++){
Arturo Espinosa's avatar
Arturo Espinosa committed
351
352
353
354
			if (!(*p == '0' || *p == '1')){
				*error_string = "#NUM!";
				return NULL;
			}
355
			result = result << 1 | (*p - '0');
Arturo Espinosa's avatar
Arturo Espinosa committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
		}
		break;
		
	default:
		*error_string = "#NUM!";
		return NULL;
	}
	value = g_new (Value, 1);
	value->type = VALUE_INTEGER;
	value->v.v_int = result;
	
	return value;
}

static Value *
371
gnumeric_cos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
372
373
374
375
376
377
378
379
380
381
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = cos (value_get_as_double (argv [0]));

	return v;
}

static Value *
382
gnumeric_cosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = cos (value_get_as_double (argv [0]));

	return v;
}

static int
callback_function_count (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = (Value *) closure;

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

static Value *
gnumeric_count (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

	result = g_new (Value, 1);
	result->type = VALUE_INTEGER;
	result->v.v_int = 0;
	
	function_iterate_argument_values (sheet, callback_function_count, result, expr_node_list,
					  eval_col, eval_row, error_string);

	return result;
}

static Value *
429
gnumeric_degrees (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
430
431
432
433
434
435
436
437
438
439
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = (value_get_as_double (argv [0]) * 180.0) / M_PI;

	return v;
}

static Value *
440
gnumeric_exp (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
441
442
443
444
445
446
447
448
449
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = exp (value_get_as_double (argv [0]));

	return v;
}

450
451
452
453
454
455
456
457
458
static float_t
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

static Value *
459
gnumeric_fact (struct FunctionDefinition *id, Value *argv [], char **error_string)
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
{
	Value *res;
	int i;

	switch (argv [0]->type){
	case VALUE_FLOAT:
		i = argv [0]->v.v_float;
		break;
	case VALUE_INTEGER:
		i = argv [0]->v.v_int;
		break;
	default:
		*error_string = "#NUM!";
		return NULL;
	}

	if (i < 0){
		*error_string = "#NUM!";
		return NULL;
	}
	
	res = g_new (Value, 1);
	if (i > 69){
		i = 69;
	}
	res->type = VALUE_FLOAT;
	res->v.v_float = fact (i);
	return res;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
490
static Value *
491
gnumeric_floor (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
492
493
494
495
496
497
498
499
500
501
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = floor (value_get_as_double (argv [0]));

	return v;
}

static Value *
502
gnumeric_int (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
503
504
505
506
507
508
509
510
511
512
513
514
515
{
	Value *v = g_new (Value, 1);
	float_t t;

	t = value_get_as_double (argv [0]);
	
	v->type = VALUE_FLOAT;
	v->v.v_float = t > 0.0 ? floor (t) : ceil (t);

	return v;
}

static Value *
516
gnumeric_log (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log (t);

	return v;
}

static Value *
534
gnumeric_log2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log2: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log (t) / M_LN2;

	return v;
}

static Value *
552
gnumeric_log10 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log10: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log10 (t);

	return v;
}

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
enum {
	OP_MIN,
	OP_MAX
};

typedef struct {
	int   operation;
	int   found;
	Value *result;
} min_max_closure_t;

static int
callback_function_min_max (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	min_max_closure_t *mm = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		if (mm->found){
			if (mm->operation == OP_MIN){
				if (value->v.v_int < mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_int;
			} else {
				if (value->v.v_int > mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_int;
			}
		} else {
			mm->found = 1;
			mm->result->v.v_float = value->v.v_int;
		}
		break;

	case VALUE_FLOAT:
		if (mm->found){
			if (mm->operation == OP_MIN){
				if (value->v.v_float < mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_float;
			} else {
				if (value->v.v_float > mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_float;
			}
		} else {
			mm->found = 1;
			mm->result->v.v_float = value->v.v_float;
		}

	default:
		/* ignore strings */
	}
	
	return TRUE;
}

static Value *
gnumeric_min (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	min_max_closure_t closure;
	Sheet *sheet = (Sheet *) tsheet;

	closure.operation = OP_MIN;
	closure.found  = 0;
	closure.result = g_new (Value, 1);
	closure.result->type = VALUE_FLOAT;
	closure.result->v.v_float = 0;

	function_iterate_argument_values (sheet, callback_function_min_max,
					  &closure, expr_node_list,
					  eval_col, eval_row, error_string);

	return 	closure.result;
}

static Value *
gnumeric_max (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	min_max_closure_t closure;
	Sheet *sheet = (Sheet *) tsheet;

	closure.operation = OP_MAX;
	closure.found  = 0;
	closure.result = g_new (Value, 1);
	closure.result->type = VALUE_FLOAT;
	closure.result->v.v_float = 0;

	function_iterate_argument_values (sheet, callback_function_min_max,
					  &closure, expr_node_list,
					  eval_col, eval_row, error_string);
	return 	closure.result;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
static char *help_or = {
	N_("<function>OR</function>"
	   "<syntax>OR(b1, b2, ...)</syntax>"

	   "<description>"
	   "Implements the logical OR function: the result is TRUE if any of the"
	   "values evaluated to TRUE.<p>"
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE."
	   "If an integer or floating point value is provided zero is considered"
	   "FALSE and anything else is TRUE.<p>"
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!'"
	   "is returned."
	   "</description>"
	   "<seealso>AND</seealso>")
};


static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		if (value->v.v_int == 1){
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
		if (value->v.v_float == 0.0){
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

	default:
		/* ignore strings */
	}
	
	return TRUE;
}

static Value *
gnumeric_or (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

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

	function_iterate_argument_values (sheet, callback_function_or,
					  result, expr_node_list,
					  eval_col, eval_row, error_string);

	/* See if there was any value worth using */
	if (result->v.v_int == -1){
		value_release (result);
		*error_string = _("#VALUE");
		return NULL;
	}
	return result;
}


static Value *
730
gnumeric_radians (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
731
732
733
734
735
736
737
738
739
740
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = (value_get_as_double (argv [0]) * M_PI) / 180;

	return v;
}

static Value *
741
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
742
743
744
745
746
747
748
749
750
751
{
	Value *v = g_new (Value, 1);
	
	v->type = VALUE_FLOAT;
	v->v.v_float = sin (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
752
gnumeric_sinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
{
	Value *v = g_new (Value, 1);
	
	v->type = VALUE_FLOAT;
	v->v.v_float = sinh (value_get_as_double (argv [0]));
	
	return v;
}

static int
callback_function_sum (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = (Value *) closure;

	switch (value->type){
	case VALUE_INTEGER:
		result->v.v_float += value->v.v_int;
		break;
		
	case VALUE_FLOAT:
		result->v.v_float += value->v.v_float;
		break;
		
	case VALUE_STRING:
		result->v.v_float += atof (value->v.str->str);
		break;

	default:
		g_warning ("Unknown VALUE type in callback_function_sum");
		break;
	}		
	return TRUE;
}

static Value *
gnumeric_sum (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

	result = g_new (Value, 1);
	result->type = VALUE_FLOAT;
	result->v.v_float = 0.0;
	
	function_iterate_argument_values (sheet, callback_function_sum, result, expr_node_list,
					  eval_col, eval_row, error_string);

	return result;
}

static Value *
804
gnumeric_tan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
805
806
807
808
809
810
811
812
813
814
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = tan (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
815
gnumeric_tanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
816
817
818
819
820
821
822
823
824
825
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = tanh (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
826
gnumeric_pi (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
827
828
829
830
831
832
833
834
835
836
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = M_PI;

	return v;
}

FunctionDefinition math_functions [] = {
837
838
839
	{ "abs",     "f",    "number",    &help_abs, NULL, gnumeric_abs },
	{ "acos",    "f",    "number",    NULL,      NULL, gnumeric_acos },
	{ "acosh",   "f",    "number",    NULL,      NULL, gnumeric_acosh },
Arturo Espinosa's avatar
Arturo Espinosa committed
840
	{ "and",     0,      "",          &help_and, gnumeric_and, NULL },
841
842
843
844
845
	{ "asin",    "f",    "number",    NULL,      NULL, gnumeric_asin },
	{ "asinh",   "f",    "number",    NULL,      NULL, gnumeric_asinh },
	{ "atan",    "f",    "number",    NULL,      NULL, gnumeric_atan },
	{ "atanh",   "f",    "number",    NULL,      NULL, gnumeric_atanh },
	{ "atan2",   "ff",   "xnum,ynum", NULL,      NULL, gnumeric_atan2 },
Arturo Espinosa's avatar
Arturo Espinosa committed
846
847
848
849
850
851
	/* avedev */
	{ "average", 0,      "",          &help_average, gnumeric_average, NULL },
	/* besseli */
	/* besselj */
	/* besselk */
	/* bessely */
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
	{ "bin2dec", "?",    "number",    NULL,       NULL, gnumeric_bin2dec },
	{ "cos",     "f",    "number",    NULL,       NULL, gnumeric_cos },
	{ "cosh",    "f",    "number",    NULL,       NULL, gnumeric_cosh },
	{ "count",   0,      "",          NULL,       gnumeric_count, NULL },
	{ "ceil",    "f",    "number",    &help_ceil, NULL, gnumeric_ceil },
	{ "degrees", "f",    "number",    NULL,       NULL, gnumeric_degrees },
	{ "exp",     "f",    "number",    NULL,       NULL, gnumeric_exp },
	{ "fact",    "f",    "number",    NULL,       NULL, gnumeric_fact },
	{ "floor",   "f",    "number",    &help_floor,NULL, gnumeric_floor },
	{ "int",     "f",    "number",    NULL,       NULL, gnumeric_int },
	{ "log",     "f",    "number",    NULL,       NULL, gnumeric_log },
	{ "log2",    "f",    "number",    NULL,       NULL, gnumeric_log2 },
	{ "log10",   "f",    "number",    NULL,       NULL, gnumeric_log10 },
	{ "min",     0,      "",          NULL,       gnumeric_min, NULL },
	{ "max",     0,      "",          NULL,       gnumeric_max, NULL },
	{ "or",      0,      "",          &help_or,   gnumeric_or, NULL },
	{ "radians", "f",    "number",    NULL,       NULL, gnumeric_radians },
	{ "sin",     "f",    "number",    NULL,       NULL, gnumeric_sin },
	{ "sinh",    "f",    "number",    NULL,       NULL, gnumeric_sinh },
	{ "sum",     0,      "number",    NULL,       gnumeric_sum, NULL },
	{ "tan",     "f",    "number",    NULL,       NULL, gnumeric_tan },
	{ "tanh",    "f",    "number",    NULL,       NULL, gnumeric_tanh },
	{ "pi",      "",     "", NULL,    NULL,       gnumeric_pi },
Arturo Espinosa's avatar
Arturo Espinosa committed
875
876
877
	{ NULL, NULL },
};