functions.c 39.3 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
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++) {
425
426
	        ceiled = ceil (significance * k);
		if (fabs (ceiled - (significance * k)) < significance/2)
427
428
429
430
431
		        break;
		k *= 10;
	}
	ceiled *= 10;

432
	div = ceil ((x * k * 10) / ceiled);
433
434
435
436
437
438
	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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
static char *help_mod = {
	N_("@FUNCTION=MOD\n"
	   "@SYNTAX=MOD(number,divisor)\n"

	   "@DESCRIPTION="
	   "Implements modulo arithmetic."
	   "Returns the remainder when divisor is divided into abs(number)."
	   "\n"
	   "Returns #DIV/0! if divisor is zero."
	   "@SEEALSO=INT,FLOOR,CEIL")
};

static Value *
gnumeric_mod (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	int a,b;
	
	a = value_get_as_int (argv[0]) ;
	b = value_get_as_int (argv[1]) ;
	/* Obscure handling of C's mod function */
	if (a<0) a = -a ;
	if (a<0) { /* -0 */
		*error_string = _("#NUM!") ;
		return NULL ;
	}
	if (b<0) {
		a = -a ;
		b = -b ;
	}
	if (b<0) { /* -0 */
		*error_string = _("#NUM!") ;
		return NULL ;
	}
	if (b==0) {
		*error_string = _("#DIV/0!") ;
		return NULL ;
	}
	
	return value_int(a%b) ;
}

965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
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);
}

986
987
static char *help_or = {
	N_("@FUNCTION=OR\n"
988
	   "@SYNTAX=OR(b1, b2, ...)\n"
989
990

	   "@DESCRIPTION="
991
	   "Implements the logical OR function: the result is TRUE if any of the "
992
	   "values evaluated to TRUE.\n"
993
994
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
	   "If an integer or floating point value is provided zero is considered "
995
996
997
998
999
	   "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"

1000
	   "@SEEALSO=AND, NOT")
1001
1002
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1003
1004
1005
1006
1007
1008
1009
static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
1010
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
1011
1012
1013
1014
1015
1016
1017
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
1018
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
1019
1020
1021
1022
1023
1024
1025
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

	default:
		/* ignore strings */
1026
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
	}
	
	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;
}

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

	   "@DESCRIPTION="
1060
1061
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
1062
1063
1064
1065
1066
1067
1068
	   "\n"

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

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

1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
static char *help_rand = {
	N_("@FUNCTION=RAND\n"
	   "@SYNTAX=RAND()\n"

	   "@DESCRIPTION="
	   "Returns a random number greater than or equal to 0 and less than 1."
	   "\n"
	   "\n"
	   
	   "@SEEALSO=")
};

static Value *
gnumeric_rand (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	return value_float (rand()/(RAND_MAX + 1.0)) ;
}

1094
1095
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
1096
	   "@SYNTAX=SIN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
1097

1098
	   "@DESCRIPTION="
1099
	   "The SIN function returns the sine of x, where x is given "
1100
1101
1102
1103
1104
1105
1106
           " 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
1107
1108

static Value *
1109
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1110
{
1111
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1112
1113
}

1114
1115
static char *help_sinh = {
	N_("@FUNCTION=SINH\n"
1116
	   "@SYNTAX=SINH(x)\n"
1117
1118

	   "@DESCRIPTION="
1119
1120
1121
	   "The SINH  function  returns  the  hyperbolic sine of x, "
	   "which is defined mathematically as (exp(x) - exp(-x)) / 2. "
	   " x is in radians. "
1122
1123
1124
1125
1126
1127
1128
	   "\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
1129
static Value *
1130
gnumeric_sinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1131
{
1132
	return value_float (sinh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1133
1134
}

1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
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)) ;
}

1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
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
1171
1172
1173
1174
1175
1176
1177
static int
callback_function_sum (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = (Value *) closure;

	switch (value->type){
	case VALUE_INTEGER:
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
		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
1204
			result->v.v_float += value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1205
1206
1207
		break;
		
	case VALUE_FLOAT:
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
		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
1218
		break;
1219
1220
1221

	case VALUE_STRING:
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1222
1223
		
	default:
1224
1225
1226
1227
		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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
		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);
1240
1241
	result->type = VALUE_INTEGER;
	result->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
1242
1243
1244
1245
1246
1247
1248
	
	function_iterate_argument_values (sheet, callback_function_sum, result, expr_node_list,
					  eval_col, eval_row, error_string);

	return result;
}

1249
1250
static char *help_tan = {
	N_("@FUNCTION=TAN\n"
1251
	   "@SYNTAX=TAN(x)\n"
1252
1253

	   "@DESCRIPTION="
1254
	   "The TAN function  returns the tangent of x, where x is "
1255
1256
1257
1258
1259
1260
1261
1262
	   "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
1263
static Value *
1264
gnumeric_tan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1265
{
1266
	return value_float (tan (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1267
1268
}

1269
1270
static char *help_tanh = {
	N_("@FUNCTION=TANH\n"
1271
	   "@SYNTAX=TANH(x)\n"
1272
1273

	   "@DESCRIPTION="
1274
1275
	   " The TANH function returns the hyperbolic tangent of x, "
	   " which is defined mathematically as sinh(x) / cosh(x). "
1276
1277
1278
1279
1280
1281
1282
	   "\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
1283
static Value *
1284
gnumeric_tanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1285
{
1286
	return value_float (tanh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1287
1288
}

1289
1290
1291
1292
1293
1294
1295
1296
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"

1297
	   "This function is called with no arguments."
1298
1299
1300
1301
	   "\n"
	   "@SEEALSO=")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1302
static Value *
1303
gnumeric_pi (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1304
{
1305
	return value_float (M_PI);
Arturo Espinosa's avatar
Arturo Espinosa committed
1306
1307
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
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);
}

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
static char *help_even = {
	N_("@FUNCTION=EVEN\n"
	   "@SYNTAX=EVEN(number)\n"

	   "@DESCRIPTION=EVEN function returns the number rounded up to the "