functions.c 28 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1
/*
2
 * fn-math.c:  Built in mathematical functions and functions registration
Arturo Espinosa's avatar
Arturo Espinosa committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 */
#include <config.h>
#include <gnome.h>
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"

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

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

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

30
	   "@DESCRIPTION"
Donnie Barnes's avatar
Donnie Barnes committed
31
	   ""
32
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
33
34
35

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

#endif

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

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

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

static char *help_acos = {
67
68
	N_("@FUNCTION=ACOS\n"
	   "@SYNTAX=ACOS(b1)\n"
69

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

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

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	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"
	   "@SYNTAX=ACOSH(b1)\n"

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

111
112
113
114
115
116
117
118
119
120
121
122
123
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
124
static char *help_and = {
125
126
	N_("@FUNCTION=AND\n"
	   "@SYNTAX=AND(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
127

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

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

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
static int
callback_function_and (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
		if (value->v.v_int == 0){
			result->v.v_int = 0;
			return FALSE;
		} else
			result->v.v_int = 1;
		break;

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

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

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

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
static char *help_asin = {
	N_("@FUNCTION=ASIN\n"
	   "@SYNTAX=ASIN(b1)\n"

	   "@DESCRIPTION="
	   "The ASIN function calculates the arc sine of b1; that is "
	   "the value whose sine is b1.  If b1 falls outside  the  range "
	   "-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
209
static Value *
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
224
225
226
227
228
229
230
231
232
233
234
235
236
static char *help_asinh = {
	N_("@FUNCTION=ASINH\n"
	   "@SYNTAX=ASINH(b1)\n"

	   "@DESCRIPTION="
	   "The ASIN  function  calculates  the inverse hyperbolic "
	   " sine of b1; that is the value whose hyperbolic sine is b1. "
	   "\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
gnumeric_asinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
239
{
240
241
	return value_float (asinh (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
242

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

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

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

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static char *help_atanh = {
	N_("@FUNCTION=ATANH\n"
	   "@SYNTAX=ATANH(b1)\n"

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

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

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
static char *help_atan2 = {
	N_("@FUNCTION=ATAN2"
	   "@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. "
	   "This function only takes one argument."  
	   "\n"
	   "@SEEALSO=ATAN, ATANH, COS, SIN, DEGREES, RADIANS")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
314
static Value *
315
gnumeric_atan2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
316
{
317
318
	return value_float (atan2 (value_get_as_double (argv [0]),
				   value_get_as_double (argv [1])));
Arturo Espinosa's avatar
Arturo Espinosa committed
319
320
}

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

	   "@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")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
333
334
335
336
337
338
339
340
341
342
343
static Value *
gnumeric_average (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Value *sum, *count;

	sum = gnumeric_sum (tsheet, expr_node_list, eval_col, eval_row, error_string);
	if (!sum)
		return NULL;
	
	count = gnumeric_count (tsheet, expr_node_list, eval_col, eval_row, error_string);
344
	if (!count || (count && count->v.v_int == 0)){
Arturo Espinosa's avatar
Arturo Espinosa committed
345
346
347
348
		value_release (sum);
		return NULL;
	}

349
350
	result = value_float (sum->v.v_float / count->v.v_int);

Arturo Espinosa's avatar
Arturo Espinosa committed
351
352
353
354
355
356
	value_release (count);
	value_release (sum);
	
	return result;
}

357
358
359
360
361
362
363
364
365
366
367
368
369
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
	   "@SYNTAX=CEIL(b1)\n"

	   "@DESCRIPTION=The CEIL function rounds b1 up to the next nearest"
	   "integer."

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

Arturo Espinosa's avatar
Arturo Espinosa committed
370
static Value *
371
gnumeric_ceil (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
372
{
373
374
375
376
377
378
	return value_float (ceil (value_get_as_double (argv [0])));
}

static char *help_bin2dec = {
	N_("@FUNCTION=BIN2DEC("
	   "@SYNTAX=BIN2DEC(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
379

380
381
382
383
	   "@DESCRIPTION="
	   "The BIN2DEC function coverts a binary number "
	   "in string or number to its decimal equilivent."
	   "\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
384

385
386
387
388
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=DEC2BIN")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
389
390

static Value *
391
gnumeric_bin2dec (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
	int  result, v, n, bit;
	char *p;

	result = 0;
	switch (argv [0]->type){
	case VALUE_INTEGER:
		v = argv [0]->v.v_int;
		n = 0;
		for (n = 0; v; n++){
			bit = v % 10;
			v   = v / 10;
			result |= bit << n;
		}
		break;
		
	case VALUE_STRING:
		p = argv [0]->v.str->str;
410
		for (;*p; p++){
Arturo Espinosa's avatar
Arturo Espinosa committed
411
412
413
414
			if (!(*p == '0' || *p == '1')){
				*error_string = "#NUM!";
				return NULL;
			}
415
			result = result << 1 | (*p - '0');
Arturo Espinosa's avatar
Arturo Espinosa committed
416
417
418
419
420
421
422
		}
		break;
		
	default:
		*error_string = "#NUM!";
		return NULL;
	}
423
	return value_int (result);
Arturo Espinosa's avatar
Arturo Espinosa committed
424
425
}

426
427
428
429
430
431
432
433
434
435
436
437
438
439
static char *help_cos = {
	N_("@FUNCTION=COS\n"
	   "@SYNTAX=COS(b1)\n"

	   "@DESCRIPTION="
	   "The  COS  function  returns  the cosine of b1, where b1 is "
           "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
440
static Value *
441
gnumeric_cos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
442
{
443
444
	return value_float (cos (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
445

446
447
448
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
	   "@SYNTAX=COSH(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
449

450
451
452
453
454
455
456
457
458
459
	   "@DESCRIPTION="
	   "The COSH  function  returns the hyperbolic cosine of b1, "
	   " which is defined mathematically as (exp(b1) + exp(-b1)) / 2.   "
	   " b1 is in radians. "
	   "\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
460
461

static Value *
462
gnumeric_cosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
463
{
464
	return value_float (cosh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
}

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

487
488
489
490
491
492
493
494
495
496
497
498
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")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
static Value *
gnumeric_count (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *result;
	Sheet *sheet = (Sheet *) tsheet;

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

	return result;
}

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
	   "@SYNTAX=DEGREES(b1)\n"

	   "@DESCRIPTION="
	   "Computes the number of degrees equilivent to "
	   " b1 radians."
	   "\n"

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

Arturo Espinosa's avatar
Arturo Espinosa committed
530
static Value *
531
gnumeric_degrees (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
532
{
533
534
	return value_float ((value_get_as_double (argv [0]) * 180.0) / M_PI);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
535

536
537
538
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
	   "@SYNTAX=EXP(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
539

540
541
542
543
544
545
546
547
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
	   "to the power of b1. "
	   "\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
548
549

static Value *
550
gnumeric_exp (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
551
{
552
	return value_float (exp (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
553
554
}

555
556
557
558
559
560
561
562
static float_t
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

563
564
565
566
567
568
569
570
571
572
573
574
575
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
	   "@SYNTAX=FACT(b1)\n"

	   "@DESCRIPTION="
	   "Computes the factorial of b1. ie, b1!"
	   "\n"
	   "Performing this function on a string or empty cell returns an error"
	   "\n"
	   "\n"
	   "@SEEALSO=")
};

576
static Value *
577
gnumeric_fact (struct FunctionDefinition *id, Value *argv [], char **error_string)
578
579
{
	Value *res;
580
	float i;
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

	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);
600
601
602
603
604
605
	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);
606
607
608
609
	}
	return res;
}

610
611
612
613
614
615
616
617
618
619
620
621
622
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
	   "@SYNTAX=FLOOR(b1)\n"

	   "@DESCRIPTION=The FLOOR function rounds b1 down to the next nearest"
	   "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
623
static Value *
624
gnumeric_floor (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
625
{
626
627
	return value_float (floor (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
628

629
630
631
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
632

633
634
635
636
637
638
639
640
641
642
643
	   "@DESCRIPTION="
	   "The INT function round b1 now to the nearest int. "
	   "Where 'nearest' implies being closer to zero. "
	   "Eqilivent to FLOOR(b1) for b1 >0, amd CEIL(b1) "
	   "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
644
645

static Value *
646
gnumeric_int (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
647
648
649
650
651
{
	float_t t;

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

655
656
657
658
659
660
661
662
663
664
665
666
static char *help_log = {
	N_("@FUNCTION=LOG\n"
	   "@SYNTAX=LOG(b1)\n"

	   "@DESCRIPTION="
	   "Computes the natural logarithm  of b1. "
	   "\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
667
static Value *
668
gnumeric_log (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
669
670
671
672
{
	float_t t;

	t = value_get_as_double (argv [0]);
673
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
674
675
676
		*error_string = _("log: domain error");
		return NULL;
	}
677
	return value_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
678
679
}

680
681
682
683
684
685
686
687
688
689
690
691
static char *help_log2 = {
	N_("@FUNCTION=LOG2"
	   "@SYNTAX=LOG2(b1)\n"

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

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

705
706
707
708
709
710
711
712
713
714
715
716
717
static char *help_log10 = {
	N_("@FUNCTION=LOG10"
	   "@SYNTAX=LOG10(b1)\n"

	   "@DESCRIPTION="
	   "Computes the base-10 logarithm  of b1. "
	   "\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
718
static Value *
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
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")
};

759
enum {
760
761
	OPER_MIN,
	OPER_MAX
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
};

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){
778
			if (mm->operation == OPER_MIN){
779
780
781
782
783
784
785
786
787
788
789
790
791
792
				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){
793
			if (mm->operation == OPER_MIN){
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
				if (value->v.v_float < mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_float;
			} else {
				if (value->v.v_float > mm->result->v.v_float)
					mm->result->v.v_float = value->v.v_float;
			}
		} else {
			mm->found = 1;
			mm->result->v.v_float = value->v.v_float;
		}

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

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

818
	closure.operation = OPER_MIN;
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
	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;

837
	closure.operation = OPER_MAX;
838
839
840
841
842
843
844
845
846
847
848
	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;
}

849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
static char *help_or = {
	N_("@FUNCTION=OR\n"
	   "@SYNTAX=OR(b1, b2, ...)"

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

	   "@SEEALSO=AND")
};

Arturo Espinosa's avatar
Arturo Espinosa committed
866
867
868
869
870
871
872
static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
873
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
874
875
876
877
878
879
880
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
881
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

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

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

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

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

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

917
918
919
920
921
922
923
924
925
926
927
928
929
930
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
	   "@SYNTAX=RADIANS(b1)\n"

	   "@DESCRIPTION="
	   "Computes the number of radians equilivent to  "
	   "b1 degrees. "
	   "\n"

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

static Value *
933
gnumeric_radians (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
934
{
935
936
	return value_float ((value_get_as_double (argv [0]) * M_PI) / 180);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
937

938
939
940
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
	   "@SYNTAX=SIN(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
941

942
943
944
945
946
947
948
949
950
	   "@DESCRIPTION="
	   "The SIN function returns the sine of b1, where b1 is given "
           " 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
951
952

static Value *
953
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
954
{
955
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
956
957
}

958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
static char *help_sinh = {
	N_("@FUNCTION=SINH\n"
	   "@SYNTAX=SINH(b1)\n"

	   "@DESCRIPTION="
	   "The SINH  function  returns  the  hyperbolic sine of b1, "
	   "which is defined mathematically as (exp(b1) - exp(-b1)) / 2. "
	   " b1 is in radians. "
	   "\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
973
static Value *
974
gnumeric_sinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
975
{
976
	return value_float (sinh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
977
978
}

979
980
981
982
983
984
985
986
987
988
989
990
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
991
992
993
994
995
996
997
998
999
1000
static int
callback_function_sum (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = (Value *) closure;

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