functions.c 33.2 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
9
10
11
12
13
14
15
16
17
18
19
20
21
 */
#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);


Donnie Barnes's avatar
Donnie Barnes committed
22
23
24
#if 0
/* help template */
static char *help_ = {
25
26
	N_("@FUNCTION=NAME\n"
	   "@SYNTAX=(b1, b2, ...)\n"
Donnie Barnes's avatar
Donnie Barnes committed
27

28
	   "@DESCRIPTION"
Donnie Barnes's avatar
Donnie Barnes committed
29
	   ""
30
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
31
32
33

	   ""
	   ""
34
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
35
36
37
38
	   
	   ""
	   ""
	   ""
39
40
	   ""
	   "@SEEALSO=")
Donnie Barnes's avatar
Donnie Barnes committed
41
42
43
44
};

#endif

45
46
47
static char *help_abs = {
	N_("@FUNCTION=ABS\n"
	   "@SYNTAX=ABS(b1)\n"
48

49
50
51
	   "@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."
52
	   "\n"
53
	   "Performing this function on a string or empty cell simply does nothing."
54
	   "\n"
55
	   "@SEEALSO=CEIL, FLOOR")
56
57
};

58
59
60
61
62
static Value *
gnumeric_abs (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	return value_float (fabs (value_get_as_double (argv [0])));
}
63
64

static char *help_acos = {
65
	N_("@FUNCTION=ACOS\n"
66
	   "@SYNTAX=ACOS(x)\n"
67

68
	   "@DESCRIPTION="
69
70
	   "The ACOS function calculates the arc cosine of x; that "
	   " is the value whose cosine is x.  If x  falls  outside  the "
71
72
	   " range -1 to 1, ACOS fails and returns the error 'acos - domain error'. "
	   " The value it returns is in radians. "
73
	   "\n"
74
75
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
76
77
	   "\n"
	   "@SEEALSO=COS, SIN, DEGREES, RADIANS")
78
79
};

80
81
82
83
static Value *
gnumeric_acos (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	float_t t;
Donnie Barnes's avatar
Donnie Barnes committed
84

85
86
87
88
89
90
91
92
93
94
	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("acos - domain error");
		return NULL;
	}
	return value_float (acos (t));
}

static char *help_acosh = {
	N_("@FUNCTION=ACOSH\n"
95
	   "@SYNTAX=ACOSH(x)\n"
96
97
98

	   "@DESCRIPTION="
	   "The ACOSH  function  calculates  the inverse hyperbolic "
99
100
	   "cosine of x; that is the value whose hyperbolic cosine is "
	   "x. If x is less than 1.0, acosh() returns the error "
101
	   " 'acosh - domain error'"
Arturo Espinosa's avatar
Arturo Espinosa committed
102
	   "\n"
103
104
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
105
	   "\n"
106
	   "@SEEALSO=ACOS, DEGREES, RADIANS ")
Donnie Barnes's avatar
Donnie Barnes committed
107
108
};

109
110
111
112
113
114
115
116
117
118
119
120
121
static Value *
gnumeric_acosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 1.0){
		*error_string = _("acosh - domain error");
		return NULL;
	}
	return value_float (acosh (t));
}

Arturo Espinosa's avatar
Arturo Espinosa committed
122
static char *help_and = {
123
124
	N_("@FUNCTION=AND\n"
	   "@SYNTAX=AND(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
125

126
	   "@DESCRIPTION=Implements the logical AND function: the result is TRUE "
Donnie Barnes's avatar
Donnie Barnes committed
127
	   "if all of the expression evaluates to TRUE, otherwise it returns "
128
	   "FALSE.\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
129

130
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
Arturo Espinosa's avatar
Arturo Espinosa committed
131
	   "If an integer or floating point value is provided zero is considered "
132
	   "FALSE and anything else is TRUE.\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
133
134
135
136
	   
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!' "
	   "is returned. "
137
	   "\n"
138
	   "@SEEALSO=OR, NOT")
Arturo Espinosa's avatar
Arturo Espinosa committed
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
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 */
164
		break;
165
166
167
168
169
	}
	
	return TRUE;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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;
}

193
194
static char *help_asin = {
	N_("@FUNCTION=ASIN\n"
195
	   "@SYNTAX=ASIN(x)\n"
196
197

	   "@DESCRIPTION="
198
199
	   "The ASIN function calculates the arc sine of x; that is "
	   "the value whose sine is x. If x falls outside  the  range "
200
201
202
203
204
205
206
207
	   "-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
208
static Value *
209
gnumeric_asin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
210
211
212
213
214
215
216
217
{
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("asin - domain error");
		return NULL;
	}
218
	return value_float (asin (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
219
220
}

221
222
static char *help_asinh = {
	N_("@FUNCTION=ASINH\n"
223
	   "@SYNTAX=ASINH(x)\n"
224
225

	   "@DESCRIPTION="
226
227
	   "The ASINH  function  calculates  the inverse hyperbolic "
	   " sine of x; that is the value whose hyperbolic sine is x. "
228
229
230
231
232
233
234
235
	   "\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
236
static Value *
237
gnumeric_asinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
238
{
239
240
	return value_float (asinh (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
241

242
243
static char *help_atan = {
	N_("@FUNCTION=ATAN\n"
244
	   "@SYNTAX=ATAN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
245

246
	   "@DESCRIPTION="
247
248
	   "The ATAN function calculates the arc tangent of x; that "
	   " is the value whose tangent is x."
249
250
251
252
253
254
255
256
257
	   "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
258
259

static Value *
260
gnumeric_atan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
261
{
262
	return value_float (atan (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
263
264
}

265
266
static char *help_atanh = {
	N_("@FUNCTION=ATANH\n"
267
	   "@SYNTAX=ATANH(x)\n"
268
269
270

	   "@DESCRIPTION="
	   "The  ATANH  function  calculates  the inverse hyperbolic "
271
272
	   "tangent of x; that is the value whose  hyperbolic  tangent "
	   "is  x.   If  the  absolute value of x is greater than 1.0, "
273
274
275
276
277
278
279
280
281
	   " 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
282
static Value *
283
gnumeric_atanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
284
285
286
287
288
289
290
291
{
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t <= -1.0) || (t >= 1.0)){
		*error_string = _("atanh: domain error");
		return NULL;
	}
292
	return value_float (atanh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
293
294
}

295
static char *help_atan2 = {
296
	N_("@FUNCTION=ATAN2\n"
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
	   "@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
312
static Value *
313
gnumeric_atan2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
314
{
315
316
	return value_float (atan2 (value_get_as_double (argv [0]),
				   value_get_as_double (argv [1])));
Arturo Espinosa's avatar
Arturo Espinosa committed
317
318
}

319
320
static char *help_average = {
	N_("@FUNCTION=AVERAGE\n"
321
	   "@SYNTAX=AVERAGE(value1, value2,...)\n"
322
323
324
325
326
327
328
329
330

	   "@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."
	   "\n"
	   "@SEEALSO=SUM, COUNT")
};

331
Value *
Arturo Espinosa's avatar
Arturo Espinosa committed
332
333
334
335
gnumeric_average (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Value *sum, *count;
336
337
	double c;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
338
339
340
341
342
	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);
343
	if (!count){
Arturo Espinosa's avatar
Arturo Espinosa committed
344
345
346
347
		value_release (sum);
		return NULL;
	}

Arturo Espinosa's avatar
Arturo Espinosa committed
348
	c = value_get_as_double (count);
349
350
351
352
353
354
355
	
	if (c == 0.0){
		*error_string = "Division by zero";
		value_release (sum);
		return NULL;
	}
	
Arturo Espinosa's avatar
Arturo Espinosa committed
356
	result = value_float (value_get_as_double (sum) / c);
357

Arturo Espinosa's avatar
Arturo Espinosa committed
358
359
360
361
362
363
	value_release (count);
	value_release (sum);
	
	return result;
}

364
365
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
366
	   "@SYNTAX=CEIL(x)\n"
367

368
369
	   "@DESCRIPTION=The CEIL function rounds x up to the next nearest "
	   "integer.\n"
370
371
372
373
374
375
376

	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   
	   "@SEEALSO=ABS, FLOOR, INT")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
377
static Value *
378
gnumeric_ceil (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
379
{
380
381
382
	return value_float (ceil (value_get_as_double (argv [0])));
}

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
429
430
431
432
433
434
435
436
437
438
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"
	   
	   "@SEEALSO=CEIL")
};

static Value *
gnumeric_ceiling (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
        float_t k=1;
	float_t div, mod, ceiled;
        float_t x, significance;
	int     n, sign=1;

	if (!VALUE_IS_NUMBER(argv[0]) ||
	    !VALUE_IS_NUMBER(argv[1])) {
                *error_string = _("#VALUE!") ;
                return NULL;
	}
	x = value_get_as_double(argv[0]);
	significance = value_get_as_double(argv[1]);
	if ((x < 0.0 && significance > 0.0) || 
	    (x > 0.0 && significance < 0.0)) {
                *error_string = _("#NUM!") ;
                return NULL;
	}
	if (significance < 0) {
	        sign=-1;
		x = -x;
		significance = -significance;
	}
	/* Find significance level */
	for (n=0; n<12; n++) {
	        ceiled = ceilf(significance * k);
		if (fabs(ceiled - (significance * k)) < significance/2)
		        break;
		k *= 10;
	}
	ceiled *= 10;

	div = ceilf((x * k * 10) / ceiled);
	mod = ((x * k * 10) / ceiled) - div;

	return value_float (sign * ceiled * div / (k*10) -
			    sign * significance * (mod > 0));
}

439
440
static char *help_cos = {
	N_("@FUNCTION=COS\n"
441
	   "@SYNTAX=COS(x)\n"
442
443

	   "@DESCRIPTION="
444
	   "The  COS  function  returns  the cosine of x, where x is "
445
446
447
448
449
450
451
452
           "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
453
static Value *
454
gnumeric_cos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
455
{
456
457
	return value_float (cos (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
458

459
460
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
461
	   "@SYNTAX=COSH(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
462

463
	   "@DESCRIPTION="
464
465
466
	   "The COSH  function  returns the hyperbolic cosine of x, "
	   " which is defined mathematically as (exp(x) + exp(-x)) / 2.   "
	   " x is in radians. "
467
468
469
470
471
472
	   "\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
473
474

static Value *
475
gnumeric_cosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
476
{
477
	return value_float (cosh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
}

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;
}

500
501
502
503
504
505
506
507
508
509
510
511
static char *help_count = {
	N_("@FUNCTION=COUNT\n"
	   "@SYNTAX=COUNT(b1, b2, ...)\n"

	   "@DESCRIPTION="
	   "Returns the total number of arguments passed."
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=AVERAGE")
};

512
Value *
Arturo Espinosa's avatar
Arturo Espinosa committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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;
}

528
529
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
530
	   "@SYNTAX=DEGREES(x)\n"
531
532

	   "@DESCRIPTION="
533
534
	   "Computes the number of degrees equivalent to "
	   " x radians."
535
536
537
538
539
540
541
542
	   "\n"

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

Arturo Espinosa's avatar
Arturo Espinosa committed
543
static Value *
544
gnumeric_degrees (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
545
{
546
547
	return value_float ((value_get_as_double (argv [0]) * 180.0) / M_PI);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
548

549
550
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
551
	   "@SYNTAX=EXP(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
552

553
554
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
555
	   "to the power of x. "
556
557
558
559
560
	   "\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
561
562

static Value *
563
gnumeric_exp (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
564
{
565
	return value_float (exp (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
566
567
}

568
float_t
569
570
571
572
573
574
575
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

576
577
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
578
	   "@SYNTAX=FACT(x)\n"
579
580

	   "@DESCRIPTION="
581
	   "Computes the factorial of x. ie, x!"
582
583
584
585
586
587
588
	   "\n"
	   "Performing this function on a string or empty cell returns an error"
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

589
static Value *
590
gnumeric_fact (struct FunctionDefinition *id, Value *argv [], char **error_string)
591
592
{
	Value *res;
593
	float i;
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

	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);
613
614
615
616
617
618
	if (i > 12){
		res->type = VALUE_FLOAT;
		res->v.v_float = exp (lgamma (i + 1));
	} else {
		res->type = VALUE_INTEGER;
		res->v.v_int = fact ((int)i);
619
620
621
622
	}
	return res;
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
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
659
660
661
662
663
664
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)
{
	return fact(n) / (fact(k) * fact(n-k));
}

static Value *
gnumeric_combin (struct FunctionDefinition *id, Value *argv [], char **error_string)
{
	Value *res;
	float_t n, k;

	if (argv [0]->type == VALUE_INTEGER &&
	    argv [1]->type == VALUE_INTEGER &&
	    argv[0]->v.v_int >= argv[1]->v.v_int){
		n = argv [0]->v.v_int;
		k = argv [1]->v.v_int;
	} else {
		*error_string = "#NUM!";
		return NULL;
	}
	
	res = g_new (Value, 1);
	res->type = VALUE_INTEGER;
	res->v.v_int = combin ((int)n, (int)k);
	return res;
}

665
666
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
667
	   "@SYNTAX=FLOOR(x)\n"
668

669
	   "@DESCRIPTION=The FLOOR function rounds x down to the next nearest "
670
671
672
673
674
675
676
677
	   "integer."
	   "\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
678
static Value *
679
gnumeric_floor (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
680
{
681
682
	return value_float (floor (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
683

684
685
686
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
687

688
689
690
	   "@DESCRIPTION="
	   "The INT function round b1 now to the nearest int. "
	   "Where 'nearest' implies being closer to zero. "
691
	   "Equivalent to FLOOR(b1) for b1 >0, amd CEIL(b1) "
692
693
694
695
696
697
698
	   "for b1 < 0. " 
	   "\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
699
700

static Value *
701
gnumeric_int (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
702
703
704
705
706
{
	float_t t;

	t = value_get_as_double (argv [0]);
	
707
	return value_float (t > 0.0 ? floor (t) : ceil (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
708
709
}

710
711
static char *help_log = {
	N_("@FUNCTION=LOG\n"
712
	   "@SYNTAX=LOG(x)\n"
713
714

	   "@DESCRIPTION="
715
	   "Computes the natural logarithm  of x. "
716
717
718
719
720
721
	   "\n"
	   "Performing this function on a string or empty cell returns an error. "
	   "\n"
	   "@SEEALSO=EXP, LOG2, LOG10")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
722
static Value *
723
gnumeric_log (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
724
725
726
727
{
	float_t t;

	t = value_get_as_double (argv [0]);
728
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
729
730
731
		*error_string = _("log: domain error");
		return NULL;
	}
732
	return value_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
733
734
}

735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
static char *help_power = {
	N_("@FUNCTION=POWER\n"
	   "@SYNTAX=POWER(x,y)\n"

	   "@DESCRIPTION="
	   "Returns the value of x raised to the power y"
	   "\n"
	   "Performing this function on a string or empty cell returns an error. "
	   "\n"
	   "@SEEALSO=EXP")
};

static Value *
gnumeric_power (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	return value_float (pow(value_get_as_double (argv [0]),
				value_get_as_double (argv [1]))) ;
}

754
static char *help_log2 = {
755
	N_("@FUNCTION=LOG2\n"
756
	   "@SYNTAX=LOG2(x)\n"
757
758

	   "@DESCRIPTION="
759
	   "Computes the base-2 logarithm  of x. "
760
761
762
763
764
765
	   "\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
766
static Value *
767
gnumeric_log2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
768
769
770
771
{
	float_t t;

	t = value_get_as_double (argv [0]);
772
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
773
774
775
		*error_string = _("log2: domain error");
		return NULL;
	}
776
	return value_float (log (t) / M_LN2);
Arturo Espinosa's avatar
Arturo Espinosa committed
777
778
}

779
static char *help_log10 = {
780
	N_("@FUNCTION=LOG10\n"
781
	   "@SYNTAX=LOG10(x)\n"
782
783

	   "@DESCRIPTION="
784
	   "Computes the base-10 logarithm  of x. "
785
786
787
788
789
790
791
	   "\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
792
static Value *
793
gnumeric_log10 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
794
795
796
797
{
	float_t t;

	t = value_get_as_double (argv [0]);
798
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
799
800
801
		*error_string = _("log10: domain error");
		return NULL;
	}
802
	return value_float (log10 (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
803
804
}

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
static char *help_min = {
	N_("@FUNCTION=MIN\n"
	   "@SYNTAX=MIN(b1, b2, ...)\n"

	   "@DESCRIPTION="
	   "MIN returns the value of the element of the values passed "
	   "that has the smallest value. With negative numbers considered "
	   "smaller than positive numbers."
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=MAX,ABS")
};

static char *help_max = {
	N_("@FUNCTION=MAX\n"
	   "@SYNTAX=MAX(b1, b2, ...)\n"

	   "@DESCRIPTION="
	   "MAX returns the value of the element of the values passed "
	   "that has the largest value. With negative numbers considered "
	   "smaller than positive numbers."
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=MIN,ABS")
};

833
enum {
834
835
	OPER_MIN,
	OPER_MAX
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
};

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){
852
			if (mm->operation == OPER_MIN){
853
854
855
856
857
858
859
860
861
862
863
864
865
866
				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){
867
			if (mm->operation == OPER_MIN){
868
869
870
871
872
873
874
875
876
877
878
879
880
				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 */
881
		break;
882
883
884
885
886
887
888
889
890
891
892
	}
	
	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;

893
	closure.operation = OPER_MIN;
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
	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;

912
	closure.operation = OPER_MAX;
913
914
915
916
917
918
919
920
921
922
923
	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;
}

924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
static char *help_not = {
	N_("@FUNCTION=NOT\n"
	   "@SYNTAX=NOT(number)\n"

	   "@DESCRIPTION="
	   "Implements the logical NOT function: the result is TRUE if the "
	   "number is zero;  othewise the result is FALSE.\n\n"

	   "@SEEALSO=AND, OR")
};

static Value *
gnumeric_not (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	int b;
	
	b = value_get_as_int (argv [0]);
	
	return value_int (!b);
}

945
946
static char *help_or = {
	N_("@FUNCTION=OR\n"
947
	   "@SYNTAX=OR(b1, b2, ...)\n"
948
949

	   "@DESCRIPTION="
950
	   "Implements the logical OR function: the result is TRUE if any of the "
951
	   "values evaluated to TRUE.\n"
952
953
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
	   "If an integer or floating point value is provided zero is considered "
954
955
956
957
958
	   "FALSE and anything else is TRUE.\n"
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!'"
	   "is returned.\n"

959
	   "@SEEALSO=AND, NOT")
960
961
};

Arturo Espinosa's avatar
Arturo Espinosa committed
962
963
964
965
966
967
968
static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
969
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
970
971
972
973
974
975
976
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
977
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
978
979
980
981
982
983
984
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

	default:
		/* ignore strings */
985
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
	}
	
	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;
}

1014
1015
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
1016
	   "@SYNTAX=RADIANS(x)\n"
1017
1018

	   "@DESCRIPTION="
1019
1020
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
1021
1022
1023
1024
1025
1026
1027
	   "\n"

	   "Performing this function on a string or empty cell simply does nothing. "
	   "\n"
	   
	   "@SEEALSO=PI,DEGREES")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
1028
1029

static Value *
1030
gnumeric_radians (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1031
{
1032
1033
	return value_float ((value_get_as_double (argv [0]) * M_PI) / 180);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1034

1035
1036
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
1037
	   "@SYNTAX=SIN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
1038

1039
	   "@DESCRIPTION="
1040
	   "The SIN function returns the sine of x, where x is given "
1041
1042
1043
1044
1045
1046
1047
           " in radians. "
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=COS, COSH, SINH, TAN, TANH, RADIANS, DEGREES")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
1048
1049

static Value *
1050
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1051
{
1052
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1053
1054
}

1055
1056
static char *help_sinh = {
	N_("@FUNCTION=SINH\n"
1057
	   "@SYNTAX=SINH(x)\n"
1058
1059

	   "@DESCRIPTION="
1060
1061
1062
	   "The SINH  function  returns  the  hyperbolic sine of x, "
	   "which is defined mathematically as (exp(x) - exp(-x)) / 2. "
	   " x is in radians. "
1063
1064
1065
1066
1067
1068
1069
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=SIN, COS, COSH, TAN, TANH, DEGREES, RADIANS, EXP")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1070
static Value *
1071
gnumeric_sinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1072
{
1073
	return value_float (sinh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1074
1075
}

1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
static char *help_sqrt = {
	N_("@FUNCTION=SQRT\n"
	   "@SYNTAX=SQRT(x)\n"

	   "@DESCRIPTION="
	   "The SQRT  function  returns  the  square root of x, "
	   "\n"
	   "If x is negative returns #NUM!."
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=POW")
};

static Value *
gnumeric_sqrt (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	float_t x = value_get_as_double (argv[0]) ;
	if (x<0) {
		*error_string = _("#NUM!") ;
		return NULL ;
	}
	return value_float (sqrt(x)) ;
}

1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
static char *help_sum = {
	N_("@FUNCTION=SUM\n"
	   "@SYNTAX=SUM(value1, value2, ...)\n"

	   "@DESCRIPTION="
	   "Computes the sum of all the values and cells referenced in the "
	   "argument list. " 
	   "\n"

	   "@SEEALSO=AVERAGE, COUNT")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1112
1113
1114
1115
1116
1117
1118
static int
callback_function_sum (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = (Value *) closure;

	switch (value->type){
	case VALUE_INTEGER:
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
		if (result->type == VALUE_INTEGER){
			if ((result->v.v_int > 0) && (value->v.v_int > 0)){
				int sum = result->v.v_int + value->v.v_int;

				if (sum < result->v.v_int){
					double n = result->v.v_int + value->v.v_int;
					
					result->type = VALUE_FLOAT;
					result->v.v_float = n;
				} else
					result->v.v_int = sum;
			} else if ((result->v.v_int < 0) && (value->v.v_int < 0)){
				int sum = result->v.v_int + value->v.v_int;

				if (sum > result->v.v_int){
					double n = result->v.v_int + value->v.v_int;
					
					result->type = VALUE_FLOAT;
					result->v.v_float = n;
				} else {
					result->v.v_int = sum;
				}
			} else {
				result->v.v_int += value->v.v_int;
			}
		} else
1145
			result->v.v_float += value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1146
1147
1148
		break;
		
	case VALUE_FLOAT:
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
		if (result->type == VALUE_FLOAT)
			result->v.v_float += value->v.v_float;
		else {
			double v = result->v.v_int;

			/* cast to float */
			
			result->type = VALUE_FLOAT;
			result->v.v_float = v + value->v.v_float;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
1159
		break;
1160
1161
1162

	case VALUE_STRING:
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1163
1164
		
	default:
1165
1166
1167
1168
		g_warning ("Unimplemented value->type in callback_function_sum : %s (%d)",
			   (value->type == VALUE_CELLRANGE) ? "CELLRANGE" :
			   (value->type == VALUE_ARRAY) ? "ARRAY" :
			   "UNKOWN!", value->type);
Arturo Espinosa's avatar
Arturo Espinosa committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
		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);
1181
1182
	result->type = VALUE_INTEGER;
	result->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
1183
1184
1185
1186
1187
1188
1189
	
	function_iterate_argument_values (sheet, callback_function_sum, result, expr_node_list,
					  eval_col, eval_row, error_string);

	return result;
}

1190
1191
static char *help_tan = {
	N_("@FUNCTION=TAN\n"
1192
	   "@SYNTAX=TAN(x)\n"
1193
1194

	   "@DESCRIPTION="
1195
	   "The TAN function  returns the tangent of x, where x is "
1196
1197
1198
1199
1200
1201
1202
1203
	   "given in radians. "
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=TANH, COS, COSH, SIN, SINH, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1204
static Value *
1205
gnumeric_tan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1206
{
1207
	return value_float (tan (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1208
1209
}

1210
1211
static char *help_tanh = {
	N_("@FUNCTION=TANH\n"
1212
	   "@SYNTAX=TANH(x)\n"
1213
1214

	   "@DESCRIPTION="
1215
1216
	   " The TANH function returns the hyperbolic tangent of x, "
	   " which is defined mathematically as sinh(x) / cosh(x). "
1217
1218
1219
1220
1221
1222
1223
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
	   "\n"
	   "@SEEALSO=TAN, SIN, SINH, COS, COSH, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1224
static Value *
1225
gnumeric_tanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1226
{
1227
	return value_float (tanh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1228
1229
}

1230
1231
1232
1233
1234
1235
1236
1237
static char *help_pi = {
	N_("@FUNCTION=PI\n"
	   "@SYNTAX=PI()\n"

	   "@DESCRIPTION=The PI functions returns the value of Pi "
	   "as defined by M_PI."
	   "\n"

1238
	   "This function is called with no arguments."
1239
1240
1241
1242
	   "\n"
	   "@SEEALSO=")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1243
static Value *
1244
gnumeric_pi (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1245
{
1246
	return value_float (M_PI);
Arturo Espinosa's avatar
Arturo Espinosa committed
1247
1248
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
static char *help_trunc = {
	N_("@FUNCTION=TRUNC\n"
	   "@SYNTAX=TRUNC(number[,digits])\n"

	   "@DESCRIPTION=The TRUNC function returns the value of number "
	   "truncated to the number of digits specified.  If digits is omited "
	   "then digits defaults to zero."
	   "\n"

	   "\n"
	   "@SEEALSO=")
};
static Value *
gnumeric_trunc (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *number;
	int args = g_list_length (expr_node_list);
	int decimals = 0;
	double v, integral, fraction;
	
	if (args < 1 || args > 2){
		*error_string = _("Invalid number of arguments");
		return NULL;
	}

	number = eval_expr (tsheet, (ExprTree *) expr_node_list->data, eval_col, eval_row, error_string);
	if (!number)
		return NULL;

	v = number->v.v_float;
	value_release (number);
	
	if (args == 2){
		Value *value;

		value = eval_expr (tsheet, (ExprTree *) expr_node_list->next->data, eval_col, eval_row, error_string);
		if (!value){
			return NULL;
		}
		
		decimals = value_get_as_int (value);
		value_release (value);
	}

	fraction = modf (v, &integral);
	if (decimals){
		double pot = pow (10, decimals);
		
		return value_float (integral + floor (fraction * pot) / pot);
	} else
		return value_float (integral);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1302
FunctionDefinition math_functions [] = {
1303
1304
1305
1306
1307
1308
1309
1310
1311
	{ "abs",     "f",    "number",    &help_abs,   NULL, gnumeric_abs },
	{ "acos",    "f",    "number",    &help_acos,  NULL, gnumeric_acos },
	{ "acosh",   "f",    "number",    &help_acosh, NULL, gnumeric_acosh },
	{ "and",     0,      "",          &help_and,   gnumeric_and, NULL },
	{ "asin",    "f",    "number",    &help_asin,  NULL, gnumeric_asin },
	{ "asinh",   "f",    "number",    &help_asinh, NULL, gnumeric_asinh },
	{ "atan",    "f",    "number",    &help_atan,  NULL, gnumeric_atan },
	{ "atanh",   "f",    "number",    &help_atanh, NULL, gnumeric_atanh },
	{ "atan2",   "ff",   "xnum,ynum", &help_atan2, NULL, gnumeric_atan2 },
Arturo Espinosa's avatar
Arturo Espinosa committed
1312
1313
	/* avedev */
	{ "average", 0,      "",          &help_average, gnumeric_average, NULL },
1314
1315
1316
1317
	{ "cos",     "f",    "number",    &help_cos,     NULL, gnumeric_cos },
	{ "cosh",    "f",    "number",    &help_cosh,    NULL, gnumeric_cosh },
	{ "count",   0,      "",          &help_count,   gnumeric_count, NULL },
	{ "ceil",    "f",    "number",    &help_ceil,    NULL, gnumeric_ceil },
1318
	{ "ceiling", "ff",   "number,significance",    &help_ceiling, NULL, gnumeric_ceiling },
1319
1320
1321
	{ "degrees", "f",    "number",    &help_degrees, NULL, gnumeric_degrees },
	{ "exp",     "f",    "number",    &help_exp,     NULL, gnumeric_exp },
	{ "fact",    "f",    "number",    &help_fact,    NULL, gnumeric_fact },
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1322
	{ "combin",  "ff",   "n,k",       &help_combin,  NULL, gnumeric_combin },
1323
1324
1325
1326
1327
1328
1329
	{ "floor",   "f",    "number",    &help_floor,   NULL, gnumeric_floor },
	{ "int",     "f",    "number",    &help_int,     NULL, gnumeric_int },
	{ "log",     "f",    "number",    &help_log,     NULL, gnumeric_log },
	{ "log2",    "f",    "number",    &help_log2,    NULL, gnumeric_log2 },
	{ "log10",   "f",    "number",    &help_log10,   NULL, gnumeric_log10 },
	{ "min",     0,      "",          &help_min,     gnumeric_min, NULL },
	{ "max",     0,      "",          &help_max,     gnumeric_max, NULL },
1330
	{ "not",     "f",    "number",    &help_not,     NULL, gnumeric_not },
1331
	{ "or",      0,      "",          &help_or,      gnumeric_or, NULL },
1332
	{ "power",   "ff",   "x,y",       &help_power,   NULL, gnumeric_power },
1333
1334
1335
	{ "radians", "f",    "number",    &help_radians, NULL, gnumeric_radians },
	{ "sin",     "f",    "number",    &help_sin,     NULL, gnumeric_sin },
	{ "sinh",    "f",    "number",    &help_sinh,    NULL, gnumeric_sinh },
1336
	{ "sqrt",    "f",    "number",    &help_sqrt,    NULL, gnumeric_sqrt },
1337
1338
1339
	{ "sum",     0,      "number",    &help_sum,     gnumeric_sum, NULL },
	{ "tan",     "f",    "number",    &help_tan,     NULL, gnumeric_tan },
	{ "tanh",    "f",    "number",    &help_tanh,    NULL, gnumeric_tanh },
Arturo Espinosa's avatar
Arturo Espinosa committed
1340
1341
	{ "trunc",   "f",    "number",    &help_trunc,   gnumeric_trunc, NULL },
	{ "pi",      "",     "",          &help_pi,      NULL, gnumeric_pi },
Arturo Espinosa's avatar
Arturo Espinosa committed
1342
1343
1344
	{ NULL, NULL },
};