functions.c 48.8 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
 */
#include <config.h>
#include <gnome.h>
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"


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

23
	   "@DESCRIPTION"
Donnie Barnes's avatar
Donnie Barnes committed
24
	   ""
25
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
26
27
28

	   ""
	   ""
29
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
30
31
32
33
	   
	   ""
	   ""
	   ""
34
35
	   ""
	   "@SEEALSO=")
Donnie Barnes's avatar
Donnie Barnes committed
36
37
38
39
};

#endif

40
41
42
static char *help_abs = {
	N_("@FUNCTION=ABS\n"
	   "@SYNTAX=ABS(b1)\n"
43

44
45
46
	   "@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."
47
	   "\n"
48
	   "Performing this function on a string or empty cell simply does nothing."
49
	   "\n"
50
	   "@SEEALSO=CEIL, FLOOR")
51
52
};

53
static Value *
54
55
gnumeric_abs (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
56
57
58
{
	return value_float (fabs (value_get_as_double (argv [0])));
}
59
60

static char *help_acos = {
61
	N_("@FUNCTION=ACOS\n"
62
	   "@SYNTAX=ACOS(x)\n"
63

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

76
static Value *
77
78
gnumeric_acos (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
79
80
{
	float_t t;
Donnie Barnes's avatar
Donnie Barnes committed
81

82
83
84
85
86
87
88
89
90
91
	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"
92
	   "@SYNTAX=ACOSH(x)\n"
93
94
95

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

106
static Value *
107
108
gnumeric_acosh (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
109
110
111
112
113
114
115
116
117
118
119
{
	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
120
static char *help_and = {
121
122
	N_("@FUNCTION=AND\n"
	   "@SYNTAX=AND(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
123

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

128
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
Arturo Espinosa's avatar
Arturo Espinosa committed
129
	   "If an integer or floating point value is provided zero is considered "
130
	   "FALSE and anything else is TRUE.\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
131
132
133
134
	   
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!' "
	   "is returned. "
135
	   "\n"
136
	   "@SEEALSO=OR, NOT")
Arturo Espinosa's avatar
Arturo Espinosa committed
137
138
};

139
static int
140
141
callback_function_and (Sheet *sheet, Value *value,
		       char **error_string, void *closure)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
	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 */
163
		break;
164
165
166
167
168
	}
	
	return TRUE;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
169
static Value *
170
171
gnumeric_and (void *tsheet, GList *expr_node_list,
	      int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
	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
210
gnumeric_asin (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
211
212
213
214
215
216
217
218
{
	float_t t;

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

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

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

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

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

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

268
269
static char *help_atanh = {
	N_("@FUNCTION=ATANH\n"
270
	   "@SYNTAX=ATANH(x)\n"
271
272
273

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

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

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

324
325
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
326
	   "@SYNTAX=CEIL(x)\n"
327

328
329
	   "@DESCRIPTION=The CEIL function rounds x up to the next nearest "
	   "integer.\n"
330
331
332
333
334
335
336

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

Arturo Espinosa's avatar
Arturo Espinosa committed
337
static Value *
338
339
gnumeric_ceil (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
340
{
341
342
343
	return value_float (ceil (value_get_as_double (argv [0])));
}

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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 *
360
361
gnumeric_ceiling (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
{
        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++) {
387
388
	        ceiled = ceil (significance * k);
		if (fabs (ceiled - (significance * k)) < significance/2)
389
390
391
392
393
		        break;
		k *= 10;
	}
	ceiled *= 10;

394
	div = ceil ((x * k * 10) / ceiled);
395
396
397
398
399
400
	mod = ((x * k * 10) / ceiled) - div;

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

401
402
static char *help_cos = {
	N_("@FUNCTION=COS\n"
403
	   "@SYNTAX=COS(x)\n"
404
405

	   "@DESCRIPTION="
406
	   "The  COS  function  returns  the cosine of x, where x is "
407
408
409
410
411
412
413
414
           "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
415
static Value *
416
417
gnumeric_cos (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
418
{
419
420
	return value_float (cos (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
421

422
423
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
424
	   "@SYNTAX=COSH(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
425

426
	   "@DESCRIPTION="
427
428
429
	   "The COSH  function  returns the hyperbolic cosine of x, "
	   " which is defined mathematically as (exp(x) + exp(-x)) / 2.   "
	   " x is in radians. "
430
431
432
433
434
435
	   "\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
436
437

static Value *
438
439
gnumeric_cosh (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
440
{
441
	return value_float (cosh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
442
443
}

444
445
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
446
	   "@SYNTAX=DEGREES(x)\n"
447
448

	   "@DESCRIPTION="
449
450
	   "Computes the number of degrees equivalent to "
	   " x radians."
451
452
453
454
455
456
457
458
	   "\n"

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

Arturo Espinosa's avatar
Arturo Espinosa committed
459
static Value *
460
461
gnumeric_degrees (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
462
{
463
464
	return value_float ((value_get_as_double (argv [0]) * 180.0) / M_PI);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
465

466
467
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
468
	   "@SYNTAX=EXP(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
469

470
471
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
472
	   "to the power of x. "
473
474
475
476
477
	   "\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
478
479

static Value *
480
481
gnumeric_exp (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
482
{
483
	return value_float (exp (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
484
485
}

486
float_t
487
488
489
490
491
492
493
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

494
495
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
496
	   "@SYNTAX=FACT(x)\n"
497
498

	   "@DESCRIPTION="
499
	   "Computes the factorial of x. ie, x!"
500
501
502
503
504
505
506
	   "\n"
	   "Performing this function on a string or empty cell returns an error"
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

507
static Value *
508
509
gnumeric_fact (struct FunctionDefinition *id,
	       Value *argv [], char **error_string)
510
511
{
	Value *res;
512
	float i;
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

	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);
532
533
534
535
536
537
	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);
538
539
540
541
	}
	return res;
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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 *
563
564
gnumeric_combin (struct FunctionDefinition *id,
		 Value *argv [], char **error_string)
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
{
	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;
}

585
586
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
587
	   "@SYNTAX=FLOOR(x)\n"
588

589
	   "@DESCRIPTION=The FLOOR function rounds x down to the next nearest "
590
591
592
593
594
595
596
597
	   "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
598
static Value *
599
600
gnumeric_floor (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
601
{
602
603
	return value_float (floor (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
604

605
606
607
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
608

609
610
611
	   "@DESCRIPTION="
	   "The INT function round b1 now to the nearest int. "
	   "Where 'nearest' implies being closer to zero. "
612
	   "Equivalent to FLOOR(b1) for b1 >0, amd CEIL(b1) "
613
614
615
616
617
618
619
	   "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
620
621

static Value *
622
623
gnumeric_int (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
624
625
626
627
628
{
	float_t t;

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

632
633
static char *help_log = {
	N_("@FUNCTION=LOG\n"
634
	   "@SYNTAX=LOG(x)\n"
635
636

	   "@DESCRIPTION="
637
	   "Computes the natural logarithm  of x. "
638
639
640
641
642
643
	   "\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
644
static Value *
645
646
gnumeric_log (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
647
648
649
650
{
	float_t t;

	t = value_get_as_double (argv [0]);
651
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
652
653
654
		*error_string = _("log: domain error");
		return NULL;
	}
655
	return value_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
656
657
}

658
659
660
661
662
663
664
665
666
667
668
669
670
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 *
671
672
gnumeric_power (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
673
674
675
676
677
{
	return value_float (pow(value_get_as_double (argv [0]),
				value_get_as_double (argv [1]))) ;
}

678
static char *help_log2 = {
679
	N_("@FUNCTION=LOG2\n"
680
	   "@SYNTAX=LOG2(x)\n"
681
682

	   "@DESCRIPTION="
683
	   "Computes the base-2 logarithm  of x. "
684
685
686
687
688
689
	   "\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
690
static Value *
691
692
gnumeric_log2 (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
693
694
695
696
{
	float_t t;

	t = value_get_as_double (argv [0]);
697
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
698
699
700
		*error_string = _("log2: domain error");
		return NULL;
	}
701
	return value_float (log (t) / M_LN2);
Arturo Espinosa's avatar
Arturo Espinosa committed
702
703
}

704
static char *help_log10 = {
705
	N_("@FUNCTION=LOG10\n"
706
	   "@SYNTAX=LOG10(x)\n"
707
708

	   "@DESCRIPTION="
709
	   "Computes the base-10 logarithm  of x. "
710
711
712
713
714
715
716
	   "\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
717
static Value *
718
719
gnumeric_log10 (struct FunctionDefinition *i,
		Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
720
721
722
723
{
	float_t t;

	t = value_get_as_double (argv [0]);
724
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
725
726
727
		*error_string = _("log10: domain error");
		return NULL;
	}
728
	return value_float (log10 (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
729
730
}

731
732
733
734
735
736
737
738
739
740
741
742
743
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 *
744
745
gnumeric_mod (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
{
	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) ;
}

773
774
775
776
777
778
779
780
781
782
783
784
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 *
785
786
gnumeric_not (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
787
788
789
790
791
792
793
794
{
	int b;
	
	b = value_get_as_int (argv [0]);
	
	return value_int (!b);
}

795
796
static char *help_or = {
	N_("@FUNCTION=OR\n"
797
	   "@SYNTAX=OR(b1, b2, ...)\n"
798
799

	   "@DESCRIPTION="
800
	   "Implements the logical OR function: the result is TRUE if any of the "
801
	   "values evaluated to TRUE.\n"
802
803
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
	   "If an integer or floating point value is provided zero is considered "
804
805
806
807
808
	   "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"

809
	   "@SEEALSO=AND, NOT")
810
811
};

Arturo Espinosa's avatar
Arturo Espinosa committed
812
static int
813
814
callback_function_or (Sheet *sheet, Value *value,
		      char **error_string, void *closure)
Arturo Espinosa's avatar
Arturo Espinosa committed
815
816
817
818
819
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
820
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
821
822
823
824
825
826
827
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
828
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
829
830
831
832
833
834
835
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

	default:
		/* ignore strings */
836
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
837
838
839
840
841
842
	}
	
	return TRUE;
}

static Value *
843
844
gnumeric_or (void *tsheet, GList *expr_node_list,
	     int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
{
	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;
}

866
867
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
868
	   "@SYNTAX=RADIANS(x)\n"
869
870

	   "@DESCRIPTION="
871
872
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
873
874
875
876
877
878
879
	   "\n"

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

static Value *
882
883
gnumeric_radians (struct FunctionDefinition *i,
		  Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
884
{
885
886
	return value_float ((value_get_as_double (argv [0]) * M_PI) / 180);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
887

888
889
890
891
892
893
894
895
896
897
898
899
900
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 *
901
902
gnumeric_rand (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
903
904
905
906
{
	return value_float (rand()/(RAND_MAX + 1.0)) ;
}

907
908
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
909
	   "@SYNTAX=SIN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
910

911
	   "@DESCRIPTION="
912
	   "The SIN function returns the sine of x, where x is given "
913
914
915
916
917
918
919
           " 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
920
921

static Value *
922
923
gnumeric_sin (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
924
{
925
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
926
927
}

928
929
static char *help_sinh = {
	N_("@FUNCTION=SINH\n"
930
	   "@SYNTAX=SINH(x)\n"
931
932

	   "@DESCRIPTION="
933
934
935
	   "The SINH  function  returns  the  hyperbolic sine of x, "
	   "which is defined mathematically as (exp(x) - exp(-x)) / 2. "
	   " x is in radians. "
936
937
938
939
940
941
942
	   "\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
943
static Value *
944
945
gnumeric_sinh (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
946
{
947
	return value_float (sinh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
948
949
}

950
951
952
953
954
955
956
957
958
959
960
961
962
963
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 *
964
965
gnumeric_sqrt (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
966
967
968
969
970
971
972
973
974
{
	float_t x = value_get_as_double (argv[0]) ;
	if (x<0) {
		*error_string = _("#NUM!") ;
		return NULL ;
	}
	return value_float (sqrt(x)) ;
}

975
976
977
978
979
980
981
982
983
984
985
986
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
987
static int
988
989
callback_function_sum (Sheet *sheet, Value *value,
		       char **error_string, void *closure)
Arturo Espinosa's avatar
Arturo Espinosa committed
990
991
992
993
994
{
	Value *result = (Value *) closure;

	switch (value->type){
	case VALUE_INTEGER:
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
		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
1021
			result->v.v_float += value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1022
1023
1024
		break;
		
	case VALUE_FLOAT:
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
		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
1035
		break;
1036
1037
1038

	case VALUE_STRING:
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1039
1040
		
	default:
1041
1042
1043
1044
		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
1045
1046
1047
1048
1049
		break;
	}		
	return TRUE;
}

1050
Value *
1051
1052
gnumeric_sum (void *tsheet, GList *expr_node_list,
	      int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1053
1054
1055
1056
1057
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

	result = g_new (Value, 1);
1058
1059
	result->type = VALUE_INTEGER;
	result->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
1060
1061
1062
1063
1064
1065
1066
	
	function_iterate_argument_values (sheet, callback_function_sum, result, expr_node_list,
					  eval_col, eval_row, error_string);

	return result;
}

1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
static char *help_sumsq = {
	N_("@FUNCTION=SUMSQ\n"
	   "@SYNTAX=SUMSQ(value1, value2, ...)\n"

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

	   "@SEEALSO=SUM, COUNT")
};

typedef struct {
	guint32 num;
	float_t sum;
} math_sumsq_t;

static int
1085
1086
callback_function_sumsq (Sheet *sheet, Value *value,
			 char **error_string, void *closure)
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
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
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
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
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
{
	math_sumsq_t *mm = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		mm->num++;
		mm->sum += value->v.v_int * value->v.v_int;
		break;
	case VALUE_FLOAT:
		mm->num++;
		mm->sum += value->v.v_float * value->v.v_float;
		break;
	default:
		/* ignore strings */
		break;
	}
	return TRUE;
}

static Value *
gnumeric_sumsq (void *sheet, GList *expr_node_list, int eval_col,
		int eval_row, char **error_string)
{
        math_sumsq_t p;

	p.num = 0;
	p.sum = 0;

	function_iterate_argument_values (sheet, callback_function_sumsq,
					  &p, expr_node_list,
					  eval_col, eval_row, error_string);

	return value_float (p.sum);
}

static char *help_multinomial = {
	N_("@FUNCTION=MULTINOMIAL\n"
	   "@SYNTAX=MULTINOMIAL(value1, value2, ...)\n"

	   "@DESCRIPTION="
	   "MULTINOMIAL returns the ratio of the factorial of a sum of "
	   "values to the product of factorials. "
	   "\n"

	   "@SEEALSO=SUM")
};

typedef struct {
	guint32 num;
	int     sum;
        int     product;
} math_multinomial_t;

static int
callback_function_multinomial (Sheet *sheet, Value *value,
			       char **error_string, void *closure)
{
	math_multinomial_t *mm = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
	        mm->product *= fact(value->v.v_int);
		mm->sum += value->v.v_int;
		mm->num++;
		break;
	default:
		return FALSE;
	}
	return TRUE;
}

static Value *
gnumeric_multinomial (void *sheet, GList *expr_node_list, int eval_col,
		      int eval_row, char **error_string)
{
        math_multinomial_t p;

	p.num = 0;
	p.sum = 0;
	p.product = 1;

	if (function_iterate_argument_values (sheet,
					      callback_function_multinomial,
					      &p, expr_node_list,
					      eval_col, eval_row,
					      error_string) == FALSE) {
	        *error_string = _("#VALUE!");
		return NULL;
	}

	return value_float (fact(p.sum) / p.product);
}

static char *help_product = {
	N_("@FUNCTION=PRODUCT\n"
	   "@SYNTAX=PRODUCT(value1, value2, ...)\n"

	   "@DESCRIPTION="
	   "PRODUCT returns the product of all the values and cells "
	   "referenced in the argument list. " 
	   "\n"

	   "@SEEALSO=SUM, COUNT")
};

typedef struct {
	guint32 num;
	float_t product;
} math_product_t;

static int
callback_function_product (Sheet *sheet, Value *value,
			   char **error_string, void *closure)
{
	math_product_t *mm = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		mm->num++;
		mm->product *= value->v.v_int;
		break;
	case VALUE_FLOAT:
		mm->num++;
		mm->product *= value->v.v_float;
		break;
	default:
		/* ignore strings */
		break;
	}
	return TRUE;
}

static Value *
gnumeric_product (void *sheet, GList *expr_node_list, int eval_col,
		  int eval_row, char **error_string)
{
        math_product_t p;

	p.num = 0;
	p.product = 1;

	function_iterate_argument_values (sheet, callback_function_product,
					  &p, expr_node_list,
					  eval_col, eval_row, error_string);

	return value_float (p.product);
}

1235
1236
static char *help_tan = {
	N_("@FUNCTION=TAN\n"
1237
	   "@SYNTAX=TAN(x)\n"
1238
1239

	   "@DESCRIPTION="
1240
	   "The TAN function  returns the tangent of x, where x is "
1241
1242
1243
1244
1245
1246
1247
1248
	   "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
1249
static Value *
1250
1251
gnumeric_tan (struct FunctionDefinition *i,
	      Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1252
{
1253
	return value_float (tan (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1254
1255
}

1256
1257
static char *help_tanh = {
	N_("@FUNCTION=TANH\n"
1258
	   "@SYNTAX=TANH(x)\n"
1259
1260

	   "@DESCRIPTION="
1261
1262
	   " The TANH function returns the hyperbolic tangent of x, "
	   " which is defined mathematically as sinh(x) / cosh(x). "
1263
1264
1265
1266
1267
1268
1269
	   "\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
1270
static Value *
1271
1272
gnumeric_tanh (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1273
{
1274
	return value_float (tanh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
1275
1276
}

1277
1278
1279
1280
1281
1282
1283
1284
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"

1285
	   "This function is called with no arguments."
1286
1287
1288
1289
	   "\n"
	   "@SEEALSO=")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
1290
static Value *
1291
1292
gnumeric_pi (struct FunctionDefinition *i,
	     Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1293
{
1294
	return value_float (M_PI);
Arturo Espinosa's avatar
Arturo Espinosa committed
1295
1296
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
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 *
1310
1311
gnumeric_trunc (void *tsheet, GList *expr_node_list, 
		int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
{
	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;
	}

1323
1324
	number = eval_expr (tsheet, (ExprTree *) expr_node_list->data,
			    eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1325
1326
1327
1328
1329
1330
1331
1332
1333
	if (!number)
		return NULL;

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

1334
1335
1336
		value = eval_expr (tsheet, 
				   (ExprTree *) expr_node_list->next->data,
				   eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
		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);
}

1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
static char *help_even = {
	N_("@FUNCTION=EVEN\n"
	   "@SYNTAX=EVEN(number)\n"

	   "@DESCRIPTION=EVEN function returns the number rounded up to the "
	   "nearest even integer. "
	   "\n"
	   "@SEEALSO=ODD")
};

static Value *
1365
1366
gnumeric_even (struct FunctionDefinition *i,
	       Value *argv [], char **error_string)
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
{
        float_t number, ceiled;
	int     sign = 1;

	number = value_get_as_double (argv[0]);
	if (number < 0) {
	        sign = -1;
		number = -number;
	}
	ceiled = ceil(number);
	if (fmod(ceiled, 2) == 0)
	        if (number > ceiled)
		        return value_int ((int) (sign * (ceiled + 2)));
		else
		        return value_int ((int) (sign * ceiled));
	else
	        return value_int ((int) (sign * (ceiled + 1)));
}

static char *help_odd = {
	N_("@FUNCTION=ODD\n"
	   "@SYNTAX=ODD(number)\n"

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