functions.c 30.2 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1
/*
2
 * fn-math.c:  Built in mathematical functions and functions registration
Arturo Espinosa's avatar
Arturo Espinosa committed
3
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
	N_("@FUNCTION=ACOS\n"
68
	   "@SYNTAX=ACOS(x)\n"
69

70
	   "@DESCRIPTION="
71
72
	   "The ACOS function calculates the arc cosine of x; that "
	   " is the value whose cosine is x.  If x  falls  outside  the "
73
74
	   " 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
	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"
97
	   "@SYNTAX=ACOSH(x)\n"
98
99
100

	   "@DESCRIPTION="
	   "The ACOSH  function  calculates  the inverse hyperbolic "
101
102
	   "cosine of x; that is the value whose hyperbolic cosine is "
	   "x. If x is less than 1.0, acosh() returns the error "
103
	   " '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
	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE. "
Arturo Espinosa's avatar
Arturo Espinosa committed
133
	   "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, NOT")
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
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 */
166
		break;
167
168
169
170
171
	}
	
	return TRUE;
}

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

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

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

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

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

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

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

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

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

297
static char *help_atan2 = {
298
	N_("@FUNCTION=ATAN2\n"
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	   "@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
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
static char *help_average = {
	N_("@FUNCTION=AVERAGE\n"
323
	   "@SYNTAX=AVERAGE(value1, value2,...)\n"
324
325
326
327
328
329
330
331
332

	   "@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
static char *help_ceil = {
	N_("@FUNCTION=CEIL\n"
368
	   "@SYNTAX=CEIL(x)\n"
369

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

	   "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
	return value_float (ceil (value_get_as_double (argv [0])));
}

static char *help_bin2dec = {
386
387
	N_("@FUNCTION=BIN2DEC\n"
	   "@SYNTAX=BIN2DEC(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
388

389
	   "@DESCRIPTION="
390
391
	   "The BIN2DEC function converts a binary number "
	   "in string or number to its decimal equivalent."
392
	   "\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
static char *help_cos = {
	N_("@FUNCTION=COS\n"
437
	   "@SYNTAX=COS(x)\n"
438
439

	   "@DESCRIPTION="
440
	   "The  COS  function  returns  the cosine of x, where x is "
441
442
443
444
445
446
447
448
           "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
static char *help_cosh = {
	N_("@FUNCTION=COSH\n"
457
	   "@SYNTAX=COSH(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
458

459
	   "@DESCRIPTION="
460
461
462
	   "The COSH  function  returns the hyperbolic cosine of x, "
	   " which is defined mathematically as (exp(x) + exp(-x)) / 2.   "
	   " x is in radians. "
463
464
465
466
467
468
	   "\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
static char *help_degrees = {
	N_("@FUNCTION=DEGREES\n"
526
	   "@SYNTAX=DEGREES(x)\n"
527
528

	   "@DESCRIPTION="
529
530
	   "Computes the number of degrees equivalent to "
	   " x radians."
531
532
533
534
535
536
537
538
	   "\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
static char *help_exp = {
	N_("@FUNCTION=EXP\n"
547
	   "@SYNTAX=EXP(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
548

549
550
	   "@DESCRIPTION="
	   "Computes the value of e(the base of natural logarithmns) raised "
551
	   "to the power of x. "
552
553
554
555
556
	   "\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
static char *help_fact = {
	N_("@FUNCTION=FACT\n"
574
	   "@SYNTAX=FACT(x)\n"
575
576

	   "@DESCRIPTION="
577
	   "Computes the factorial of x. ie, x!"
578
579
580
581
582
583
584
	   "\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
static char *help_floor = {
	N_("@FUNCTION=FLOOR\n"
621
	   "@SYNTAX=FLOOR(x)\n"
622

623
	   "@DESCRIPTION=The FLOOR function rounds x down to the next nearest "
624
625
626
627
628
629
630
631
	   "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
	   "@DESCRIPTION="
	   "The INT function round b1 now to the nearest int. "
	   "Where 'nearest' implies being closer to zero. "
645
	   "Equivalent to FLOOR(b1) for b1 >0, amd CEIL(b1) "
646
647
648
649
650
651
652
	   "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
static char *help_log = {
	N_("@FUNCTION=LOG\n"
666
	   "@SYNTAX=LOG(x)\n"
667
668

	   "@DESCRIPTION="
669
	   "Computes the natural logarithm  of x. "
670
671
672
673
674
675
	   "\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
static char *help_log2 = {
690
	N_("@FUNCTION=LOG2\n"
691
	   "@SYNTAX=LOG2(x)\n"
692
693

	   "@DESCRIPTION="
694
	   "Computes the base-2 logarithm  of x. "
695
696
697
698
699
700
	   "\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
static char *help_log10 = {
715
	N_("@FUNCTION=LOG10\n"
716
	   "@SYNTAX=LOG10(x)\n"
717
718

	   "@DESCRIPTION="
719
	   "Computes the base-10 logarithm  of x. "
720
721
722
723
724
725
726
	   "\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
				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 */
816
		break;
817
818
819
820
821
822
823
824
825
826
827
	}
	
	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;

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

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

859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
static char *help_not = {
	N_("@FUNCTION=NOT\n"
	   "@SYNTAX=NOT(number)\n"

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

	   "@SEEALSO=AND, OR")
};

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

880
881
static char *help_or = {
	N_("@FUNCTION=OR\n"
882
	   "@SYNTAX=OR(b1, b2, ...)\n"
883
884

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

894
	   "@SEEALSO=AND, NOT")
895
896
};

Arturo Espinosa's avatar
Arturo Espinosa committed
897
898
899
900
901
902
903
static int
callback_function_or (Sheet *sheet, Value *value, char **error_string, void *closure)
{
	Value *result = closure;
	
	switch (value->type){
	case VALUE_INTEGER:
904
		if (value->v.v_int != 0){
Arturo Espinosa's avatar
Arturo Espinosa committed
905
906
907
908
909
910
911
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;
		break;

	case VALUE_FLOAT:
912
		if (value->v.v_float != 0.0){
Arturo Espinosa's avatar
Arturo Espinosa committed
913
914
915
916
917
918
919
			result->v.v_int = 1;
			return FALSE;
		} else
			result->v.v_int = 0;

	default:
		/* ignore strings */
920
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
	}
	
	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;
}

949
950
static char *help_radians = {
	N_("@FUNCTION=RADIANS\n"
951
	   "@SYNTAX=RADIANS(x)\n"
952
953

	   "@DESCRIPTION="
954
955
	   "Computes the number of radians equivalent to  "
	   "x degrees. "
956
957
958
959
960
961
962
	   "\n"

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

static Value *
965
gnumeric_radians (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
966
{
967
968
	return value_float ((value_get_as_double (argv [0]) * M_PI) / 180);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
969

970
971
static char *help_sin = {
	N_("@FUNCTION=SIN\n"
972
	   "@SYNTAX=SIN(x)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
973

974
	   "@DESCRIPTION="
975
	   "The SIN function returns the sine of x, where x is given "
976
977
978
979
980
981
982
           " 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
983
984

static Value *
985
gnumeric_sin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
986
{
987
	return value_float (sin (value_get_as_double (argv [0])));
Arturo Espinosa's avatar
Arturo Espinosa committed
988
989
}

990
991
static char *help_sinh = {
	N_("@FUNCTION=SINH\n"
992
	   "@SYNTAX=SINH(x)\n"
993
994

	   "@DESCRIPTION="
995
996
997
	   "The SINH  function  returns  the  hyperbolic sine of x, "
	   "which is defined mathematically as (exp(x) - exp(-x)) / 2. "
	   " x is in radians. "
998
999
1000
	   "\n"
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."