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

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

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

366
367
368
369
370
371
372
373
374
375
376
377
378
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
379
static Value *
380
gnumeric_ceil (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
381
{
382
383
384
385
386
387
	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
388

389
390
391
392
	   "@DESCRIPTION="
	   "The BIN2DEC function coverts a binary number "
	   "in string or number to its decimal equilivent."
	   "\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
393

394
395
396
397
	   "Performing this function on a string or empty cell simply does nothing."
	   "\n"
	   "@SEEALSO=DEC2BIN")
};
Arturo Espinosa's avatar
Arturo Espinosa committed
398
399

static Value *
400
gnumeric_bin2dec (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{
	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;
419
		for (;*p; p++){
Arturo Espinosa's avatar
Arturo Espinosa committed
420
421
422
423
			if (!(*p == '0' || *p == '1')){
				*error_string = "#NUM!";
				return NULL;
			}
424
			result = result << 1 | (*p - '0');
Arturo Espinosa's avatar
Arturo Espinosa committed
425
426
427
428
429
430
431
		}
		break;
		
	default:
		*error_string = "#NUM!";
		return NULL;
	}
432
	return value_int (result);
Arturo Espinosa's avatar
Arturo Espinosa committed
433
434
}

435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
449
static Value *
450
gnumeric_cos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
451
{
452
453
	return value_float (cos (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
454

455
456
457
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
	   "@SYNTAX=COSH(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
458

459
460
461
462
463
464
465
466
467
468
	   "@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
469
470

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

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

496
497
498
499
500
501
502
503
504
505
506
507
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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;
}

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
539
static Value *
540
gnumeric_degrees (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
541
{
542
543
	return value_float ((value_get_as_double (argv [0]) * 180.0) / M_PI);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
544

545
546
547
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
	   "@SYNTAX=EXP(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
548

549
550
551
552
553
554
555
556
	   "@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
557
558

static Value *
559
gnumeric_exp (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
560
{
561
	return value_float (exp (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
562
563
}

564
565
566
567
568
569
570
571
static float_t
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

572
573
574
575
576
577
578
579
580
581
582
583
584
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=")
};

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

	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);
609
610
611
612
613
614
	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);
615
616
617
618
	}
	return res;
}

619
620
621
622
623
624
625
626
627
628
629
630
631
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
632
static Value *
633
gnumeric_floor (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
634
{
635
636
	return value_float (floor (value_get_as_double (argv [0])));
}
Arturo Espinosa's avatar
Arturo Espinosa committed
637

638
639
640
static char *help_int = {
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
641

642
643
644
645
646
647
648
649
650
651
652
	   "@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
653
654

static Value *
655
gnumeric_int (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
656
657
658
659
660
{
	float_t t;

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

664
665
666
667
668
669
670
671
672
673
674
675
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
676
static Value *
677
gnumeric_log (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
678
679
680
681
{
	float_t t;

	t = value_get_as_double (argv [0]);
682
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
683
684
685
		*error_string = _("log: domain error");
		return NULL;
	}
686
	return value_float (log (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
687
688
}

689
690
691
692
693
694
695
696
697
698
699
700
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
701
static Value *
702
gnumeric_log2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
703
704
705
706
{
	float_t t;

	t = value_get_as_double (argv [0]);
707
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
708
709
710
		*error_string = _("log2: domain error");
		return NULL;
	}
711
	return value_float (log (t) / M_LN2);
Arturo Espinosa's avatar
Arturo Espinosa committed
712
713
}

714
715
716
717
718
719
720
721
722
723
724
725
726
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
727
static Value *
728
gnumeric_log10 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
729
730
731
732
{
	float_t t;

	t = value_get_as_double (argv [0]);
733
	if (t <= 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
734
735
736
		*error_string = _("log10: domain error");
		return NULL;
	}
737
	return value_float (log10 (t));
Arturo Espinosa's avatar
Arturo Espinosa committed
738
739
}

740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
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")
};

768
enum {
769
770
	OPER_MIN,
	OPER_MAX
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
};

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){
787
			if (mm->operation == OPER_MIN){
788
789
790
791
792
793
794
795
796
797
798
799
800
801
				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){
802
			if (mm->operation == OPER_MIN){
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
				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;

827
	closure.operation = OPER_MIN;
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
	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;

846
	closure.operation = OPER_MAX;
847
848
849
850
851
852
853
854
855
856
857
	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;
}

858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
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
875
876
877
878
879
880
881
static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
882
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
883
884
885
886
887
888
889
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
890
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
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
917
918
919
920
921
922
923
924
925
			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;
}

926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
940
941

static Value *
942
gnumeric_radians (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
943
{
944
945
	return value_float ((value_get_as_double (argv [0]) * M_PI) / 180);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
946

947
948
949
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
	   "@SYNTAX=SIN(b1)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
950

951
952
953
954
955
956
957
958
959
	   "@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
960
961

static Value *
962
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
963
{
964
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
965
966
}

967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
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
982
static Value *
983
gnumeric_sinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
984
{
985
	return value_float (sinh (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
986
987
}

988
989
990
991
992
993
994
995
996
997
998
999
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
1000
static int