functions.c 29.6 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
24
 *
 * 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);

static Value *
25
gnumeric_abs (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
26
27
28
29
30
31
32
33
34
35
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = fabs (value_get_as_double (argv [0]));

	return v;
}

static Value *
36
gnumeric_acos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("acos - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = acos (t);

	return v;
}

static Value *
54
gnumeric_acosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 1.0){
		*error_string = _("acosh - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = acosh (t);

	return v;
}

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

Donnie Barnes's avatar
Donnie Barnes committed
99
100
101
#if 0
/* help template */
static char *help_ = {
102
103
	N_("@FUNCTION=NAME\n"
	   "@SYNTAX=(b1, b2, ...)\n"
Donnie Barnes's avatar
Donnie Barnes committed
104

105
	   "@DESCRIPTION"
Donnie Barnes's avatar
Donnie Barnes committed
106
	   ""
107
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
108
109
110

	   ""
	   ""
111
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
112
113
114
115
	   
	   ""
	   ""
	   ""
116
117
	   ""
	   "@SEEALSO=")
Donnie Barnes's avatar
Donnie Barnes committed
118
119
120
121
};

#endif

122
static char *help_atan2 = {
123
	N_("@FUNCTION=ATAN2"
124
	   "@SYNTAX=ATAN2(b1,b2)\n"
125

126
	   "@DESCRIPTION="
127
128
129
130
131
	   "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."
132
	   "\n"
133
134
135

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."  
136
137
	   "\n"
	   "@SEEALSO=ATAN, ATANH, COS, SIN, DEGREES, RADIANS")
138
139
140
141
};


static char *help_atanh = {
142
143
	N_("@FUNCTION=ATANH\n"
	   "@SYNTAX=ATANH(b1)\n"
144

145
	   "@DESCRIPTION="
146
147
148
149
	   "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'      "
150
	   "\n"
151
152
153

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
154
155
	   "\n"
	   "@SEEALSO=ATAN, TAN, SIN, COS, DEGREES, RADIANS")
156
157
158
};

static char *help_atan = {
159
160
	N_("@FUNCTION=ATAN\n"
	   "@SYNTAX=ATAN(b1)\n"
161

162
	   "@DESCRIPTION="
163
164
165
	   "The ATAN function calculates the arc tangent of b1; that "
	   " is the value whose tangent is b1."
	   "Return value is in radians."
166
	   "\n"
167
168
169

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
170
	   "\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
171

172
	   "@SEEALSO=TAN, COS, SIN, DEGREES, RADIANS")
173
174
175
};

static char *help_asinh = {
176
177
	N_("@FUNCTION=ASINH\n"
	   "@SYNTAX=ASINH(b1)\n"
178

179
	   "@DESCRIPTION="
180
181
	   "The ASIN  function  calculates  the inverse hyperbolic "
	   " sine of b1; that is the value whose hyperbolic sine is b1. "
182
	   "\n"
183
184
185

	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
186
187
	   "\n"
	   "@SEEALSO=ASIN, SIN, COS, DEGREES, RADIANS")
188
189
190
191
};


static char *help_asin = {
192
193
	N_("@FUNCTION=ASIN\n"
	   "@SYNTAX=ASIN(b1)\n"
194

195
	   "@DESCRIPTION="
196
197
198
	   "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'   "
199
	   "\n"
200
201
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
202
203
	   "\n"
	   "@SEEALSO=SIN, COS, ASINH, DEGREES, RADIANS")
204
205
206
207
};


static char *help_acosh = {
208
209
	N_("@FUNCTION=ACOSH\n"
	   "@SYNTAX=ACOSH(b1)\n"
210

211
	   "@DESCRIPTION="
212
213
214
215
	   "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'"
216
	   "\n"
217
218
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
219
220
	   "\n"
	   "@SEEALSO=ACOS, DEGREES, RADIANS ")
221
222
223
};

static char *help_acos = {
224
225
	N_("@FUNCTION=ACOS\n"
	   "@SYNTAX=ACOS(b1)\n"
226

227
	   "@DESCRIPTION="
228
229
230
231
	   "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. "
232
	   "\n"
233
234
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
235
236
	   "\n"
	   "@SEEALSO=COS, SIN, DEGREES, RADIANS")
237
238
};

Arturo Espinosa's avatar
Arturo Espinosa committed
239
static char *help_average = {
240
	N_("@FUNCTION=AVERAGE\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
241
242
243
244
245
246
	   "@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."
247
	   "\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
248
249
	   "@SEEALSO=SUM, COUNT")
};
250
251

static char *help_tanh = {
252
253
	N_("@FUNCTION=TANH\n"
	   "@SYNTAX=TANH(b1)\n"
254

255
	   "@DESCRIPTION="
256
257
	   " The TANH function returns the hyperbolic tangent of b1, "
	   " which is defined mathematically as sinh(b1) / cosh(b1). "
258
	   "\n"
259
260
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
261
262
	   "\n"
	   "@SEEALSO=TAN, SIN, SINH, COS, COSH, DEGREES, RADIANS")
263
264
265
};

static char *help_tan = {
266
267
	N_("@FUNCTION=TAN\n"
	   "@SYNTAX=TAN(b1)\n"
268

269
	   "@DESCRIPTION="
270
271
	   "The TAN function  returns the tangent of b1, where b1 is "
	   "given in radians. "
272
	   "\n"
273
274
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
275
276
	   "\n"
	   "@SEEALSO=TANH, COS, COSH, SIN, SINH, DEGREES, RADIANS")
277
278
279
280
};


static char *help_sinh = {
281
282
	N_("@FUNCTION=SINH\n"
	   "@SYNTAX=SINH(b1)\n"
283

284
	   "@DESCRIPTION="
285
286
287
	   "The SINH  function  returns  the  hyperbolic sine of b1, "
	   "which is defined mathematically as (exp(b1) - exp(-b1)) / 2. "
	   " b1 is in radians. "
288
	   "\n"
289
290
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
291
292
	   "\n"
	   "@SEEALSO=SIN, COS, COSH, TAN, TANH, DEGREES, RADIANS, EXP")
293
294
295
};

static char *help_cosh = {
296
297
	N_("@FUNCTION=COSH\n"
	   "@SYNTAX=COSH(b1)\n"
298

299
	   "@DESCRIPTION="
300
301
302
	   "The COSH  function  returns the hyperbolic cosine of b1, "
	   " which is defined mathematically as (exp(b1) + exp(-b1)) / 2.   "
	   " b1 is in radians. "
303
	   "\n"
304
305
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
306
307
	   "\n"
	   "@SEEALSO=COS, SIN, SINH, TAN, TANH, RADIANS, DEGREES, EXP")
308
309
310
311
};


static char *help_sin = {
312
313
	N_("@FUNCTION=SIN\n"
	   "@SYNTAX=SIN(b1)\n"
314

315
	   "@DESCRIPTION="
316
317
	   "The SIN function returns the sine of b1, where b1 is given "
           " in radians. "
318
	   "\n"
319
320
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
321
322
	   "\n"
	   "@SEEALSO=COS, COSH, SINH, TAN, TANH, RADIANS, DEGREES")
323
324
325
};

static char *help_cos = {
326
327
	N_("@FUNCTION=COS\n"
	   "@SYNTAX=COS(b1)\n"
328

329
	   "@DESCRIPTION="
330
331
	   "The  COS  function  returns  the cosine of b1, where b1 is "
           "given in radians.  "
332
	   "\n"
333
334
	   "Performing this function on a string or empty cell simply does nothing. "
	   "This function only takes one argument."
335
336
	   "\n"
	   "@SEEALSO=COSH, SIN, SINH, TAN, TANH, RADIANS, DEGREES")
337
338
339
340
341
};



static char *help_int = {
342
343
	N_("@FUNCTION=INT\n"
	   "@SYNTAX=INT(b1, b2, ...)\n"
344

345
	   "@DESCRIPTION="
346
347
348
349
	   "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. " 
350
	   "\n"
351
352
	   "Performing this function on a string or empty cell simply does nothing."
	   ""
353
354
	   "\n"
	   "@SEEALSO=FLOOR, CEIL, ABS")
355
356
357
};

static char *help_count = {
358
359
	N_("@FUNCTION=COUNT\n"
	   "@SYNTAX=COUNT(b1, b2, ...)\n"
360

361
	   "@DESCRIPTION="
362
	   "Returns the total number of arguments passed."
363
	   "\n"
364
	   "Performing this function on a string or empty cell simply does nothing."
365
366
	   "\n"
	   "@SEEALSO=AVERAGE")
367
368
369
370
};


static char *help_fact = {
371
372
	N_("@FUNCTION=FACT\n"
	   "@SYNTAX=FACT(b1)\n"
373

374
	   "@DESCRIPTION="
375
	   "Computes the factorial of b1. ie, b1!"
376
	   "\n"
377
	   "Performing this function on a string or empty cell returns an error"
378
	   "\n"
379
	   "\n"
380
	   "@SEEALSO=")
381
382
383
384
};


static char *help_exp = {
385
386
	N_("@FUNCTION=EXP\n"
	   "@SYNTAX=EXP(b1)\n"
387

388
	   "@DESCRIPTION="
389
390
	   "Computes the value of e(the base of natural logarithmns) raised "
	   "to the power of b1. "
391
	   "\n"
392
	   "Performing this function on a string or empty cell returns an error."
393
394
	   "\n"
	   "@SEEALSO=LOG, LOG2, LOG10")
395
396
397
};

static char *help_log = {
398
399
	N_("@FUNCTION=LOG\n"
	   "@SYNTAX=LOG(b1)\n"
400

401
	   "@DESCRIPTION="
402
	   "Computes the natural logarithm  of b1. "
403
	   "\n"
404
	   "Performing this function on a string or empty cell returns an error. "
405
406
	   "\n"
	   "@SEEALSO=EXP, LOG2, LOG10")
407
408
409
};

static char *help_log10 = {
410
	N_("@FUNCTION=LOG10"
411
	   "@SYNTAX=LOG10(b1)\n"
412

413
	   "@DESCRIPTION="
414
	   "Computes the base-10 logarithm  of b1. "
415
	   "\n"
416
417

	   "Performing this function on a string or empty cell returns an error. "
418
419
	   "\n"
	   "@SEEALSO=EXP, LOG2, LOG")
420
421
422
};

static char *help_log2 = {
423
	N_("@FUNCTION=LOG2"
424
	   "@SYNTAX=LOG2(b1)\n"
425

426
	   "@DESCRIPTION="
427
	   "Computes the base-2 logarithm  of b1. "
428
	   "\n"
429
	   "Performing this function on a string or empty cell returns an error. "
430
431
	   "\n"
	   "@SEEALSO=EXP, LOG10, LOG")
432
433
434
435
};


static char *help_min = {
436
437
	N_("@FUNCTION=MIN\n"
	   "@SYNTAX=MIN(b1, b2, ...)\n"
438

439
	   "@DESCRIPTION="
440
441
442
	   "MIN returns the value of the element of the values passed "
	   "that has the smallest value. With negative numbers considered "
	   "smaller than positive numbers."
443
	   "\n"
444
	   "Performing this function on a string or empty cell simply does nothing."
445
446
	   "\n"
	   "@SEEALSO=MAX,ABS")
447
448
449
};

static char *help_max = {
450
451
	N_("@FUNCTION=MAX\n"
	   "@SYNTAX=MAX(b1, b2, ...)\n"
452

453
	   "@DESCRIPTION="
454
455
456
	   "MAX returns the value of the element of the values passed "
	   "that has the largest value. With negative numbers considered "
	   "smaller than positive numbers."
457
	   "\n"
458
	   "Performing this function on a string or empty cell simply does nothing."
459
460
	   "\n"
	   "@SEEALSO=MIN,ABS")
461
462
463
};


Arturo Espinosa's avatar
Arturo Espinosa committed
464
static char *help_or = {
465
	N_("@FUNCTION=OR\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
	   "@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")
};


482
static char *help_radians = {
483
484
	N_("@FUNCTION=RADIANS\n"
	   "@SYNTAX=RADIANS(b1)\n"
485

486
	   "@DESCRIPTION="
487
488
	   "Computes the number of radians equilivent to  "
	   "b1 degrees. "
489
	   "\n"
490
491

	   "Performing this function on a string or empty cell simply does nothing. "
492
	   "\n"
493
	   
494
	   "@SEEALSO=PI,DEGREES")
495
496
497
};

static char *help_degrees = {
498
499
	N_("@FUNCTION=DEGREES\n"
	   "@SYNTAX=DEGREES(b1)\n"
500

501
	   "@DESCRIPTION="
502
503
	   "Computes the number of degrees equilivent to "
	   " b1 radians."
504
	   "\n"
505
506

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

static char *help_sum = {
513
514
	N_("@FUNCTION=SUM\n"
	   "@SYNTAX=SUM(value1, value2, ...)\n"
515

516
	   "@DESCRIPTION="
517
518
	   "Computes the sum of all the values and cells referenced in the "
	   "argument list. " 
519
	   "\n"
520

521
	   "@SEEALSO=AVERAGE, COUNT")
522
523
524
525
};


static char *help_bin2dec = {
526
	N_("@FUNCTION=BIN2DEC("
527
	   "@SYNTAX=BIN2DEC(b1)\n"
528

529
	   "@DESCRIPTION="
530
531
	   "The BIN2DEC function coverts a binary number "
	   "in string or number to its decimal equilivent."
532
	   "\n"
533
534

	   "Performing this function on a string or empty cell simply does nothing."
535
	   "\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
536
	   "@SEEALSO=DEC2BIN")
537
538
539
540
};


static char *help_pi = {
541
542
	N_("@FUNCTION=PI\n"
	   "@SYNTAX=PI()\n"
543

544
	   "@DESCRIPTION=The PI functions returns the value of Pi "
545
	   "as defined by M_PI."
546
	   "\n"
547
548
549

	   "Performing this function on a string or empty cell"
	   "returns an error. This function is called with no arguments."
550
551
	   "\n"
	   "@SEEALSO=")
552
553
554
};


Donnie Barnes's avatar
Donnie Barnes committed
555
static char *help_floor = {
556
557
	N_("@FUNCTION=FLOOR\n"
	   "@SYNTAX=FLOOR(b1)\n"
Donnie Barnes's avatar
Donnie Barnes committed
558

559
	   "@DESCRIPTION=The FLOOR function rounds b1 down to the next nearest"
Donnie Barnes's avatar
Donnie Barnes committed
560
	   "integer."
561
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
562
563

	   "Performing this function on a string or empty cell simply does nothing."
564
565
	   "\n"
	   "@SEEALSO=CEIL, ABS, INT")
Donnie Barnes's avatar
Donnie Barnes committed
566
567
568
};

static char *help_ceil = {
569
570
	N_("@FUNCTION=CEIL\n"
	   "@SYNTAX=CEIL(b1)\n"
Donnie Barnes's avatar
Donnie Barnes committed
571

572
	   "@DESCRIPTION=The CEIL function rounds b1 up to the next nearest"
Donnie Barnes's avatar
Donnie Barnes committed
573
	   "integer."
574

Donnie Barnes's avatar
Donnie Barnes committed
575
	   "Performing this function on a string or empty cell simply does nothing."
576
	   "\n"
577
	   
578
	   "@SEEALSO=ABS, FLOOR, INT")
Donnie Barnes's avatar
Donnie Barnes committed
579
580
581
};

static char *help_abs = {
582
583
	N_("@FUNCTION=ABS\n"
	   "@SYNTAX=ABS(b1)\n"
Donnie Barnes's avatar
Donnie Barnes committed
584

585
	   "@DESCRIPTION=Implements the Absolute Value function:  the result is "
Donnie Barnes's avatar
Donnie Barnes committed
586
	   "to drop the negative sign (if present).  This can be done for "
Arturo Espinosa's avatar
Arturo Espinosa committed
587
588
	   "integers and floating point numbers."
	   "\n"
Donnie Barnes's avatar
Donnie Barnes committed
589
	   "Performing this function on a string or empty cell simply does nothing."
590
591
	   "\n"
	   "@SEEALSO=CEIL, FLOOR")
Donnie Barnes's avatar
Donnie Barnes committed
592
593
};

Arturo Espinosa's avatar
Arturo Espinosa committed
594
static char *help_and = {
595
596
	N_("@FUNCTION=AND\n"
	   "@SYNTAX=AND(b1, b2, ...)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
597

598
	   "@DESCRIPTION=Implements the logical AND function: the result is TRUE "
Donnie Barnes's avatar
Donnie Barnes committed
599
	   "if all of the expression evaluates to TRUE, otherwise it returns "
600
	   "FALSE.\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
601
602
603

	   "b1, trough bN are expressions that should evaluate to TRUE or FALSE."
	   "If an integer or floating point value is provided zero is considered "
604
	   "FALSE and anything else is TRUE.\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
605
606
607
608
	   
	   "If the values contain strings or empty cells those values are "
	   "ignored.  If no logical values are provided, then the error '#VALUE!' "
	   "is returned. "
609
	   "\n"
610
	   "@SEEALSO=OR")
Arturo Espinosa's avatar
Arturo Espinosa committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
};

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

static Value *
637
gnumeric_asin (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t < -1.0) || (t > 1.0)){
		*error_string = _("asin - domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = asin (t);

	return v;
}

static Value *
655
gnumeric_asinh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
656
657
658
659
660
661
662
663
664
665
666
{
	Value *v;

	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = asinh (value_get_as_double (argv [0]));

	return v;
}

static Value *
667
gnumeric_atan (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
668
669
670
671
672
673
674
675
676
677
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = atan (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
678
gnumeric_atanh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
{
	Value *v = g_new (Value, 1);
	float_t t;

	t = value_get_as_double (argv [0]);
	if ((t <= -1.0) || (t >= 1.0)){
		*error_string = _("atanh: domain error");
		return NULL;
	}
	v->type = VALUE_FLOAT;
	v->v.v_float = atanh (value_get_as_double (argv [0]));
	
	return v;
}

static Value *
695
gnumeric_atan2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = atan2 (value_get_as_double (argv [0]),
			      value_get_as_double (argv [1]));
	
	return v;
}

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);
	if (!count){
		value_release (sum);
		return NULL;
	}

	result = g_new (Value, 1);
	result->type = VALUE_FLOAT;
	result->v.v_float = sum->v.v_float / count->v.v_int;
	
	value_release (count);
	value_release (sum);
	
	return result;
}

static Value *
733
gnumeric_ceil (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
734
735
736
737
738
739
740
741
742
743
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = ceil (value_get_as_double (argv [0]));

	return v;
}

static Value *
744
gnumeric_bin2dec (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
{
	Value *value;
	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;
764
		for (;*p; p++){
Arturo Espinosa's avatar
Arturo Espinosa committed
765
766
767
768
			if (!(*p == '0' || *p == '1')){
				*error_string = "#NUM!";
				return NULL;
			}
769
			result = result << 1 | (*p - '0');
Arturo Espinosa's avatar
Arturo Espinosa committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
		}
		break;
		
	default:
		*error_string = "#NUM!";
		return NULL;
	}
	value = g_new (Value, 1);
	value->type = VALUE_INTEGER;
	value->v.v_int = result;
	
	return value;
}

static Value *
785
gnumeric_cos (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
786
787
788
789
790
791
792
793
794
795
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = cos (value_get_as_double (argv [0]));

	return v;
}

static Value *
796
gnumeric_cosh (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = cos (value_get_as_double (argv [0]));

	return v;
}

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

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

static Value *
843
gnumeric_degrees (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
844
845
846
847
848
849
850
851
852
853
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = (value_get_as_double (argv [0]) * 180.0) / M_PI;

	return v;
}

static Value *
854
gnumeric_exp (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
855
856
857
858
859
860
861
862
863
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = exp (value_get_as_double (argv [0]));

	return v;
}

864
865
866
867
868
869
870
871
872
static float_t
fact (int n)
{
	if (n == 0)
		return 1;
	return (n * fact (n - 1));
}

static Value *
873
gnumeric_fact (struct FunctionDefinition *id, Value *argv [], char **error_string)
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
{
	Value *res;
	int i;

	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);
	if (i > 69){
		i = 69;
	}
	res->type = VALUE_FLOAT;
	res->v.v_float = fact (i);
	return res;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
904
static Value *
905
gnumeric_floor (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
906
907
908
909
910
911
912
913
914
915
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = floor (value_get_as_double (argv [0]));

	return v;
}

static Value *
916
gnumeric_int (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
917
918
919
920
921
922
923
924
925
926
927
928
929
{
	Value *v = g_new (Value, 1);
	float_t t;

	t = value_get_as_double (argv [0]);
	
	v->type = VALUE_FLOAT;
	v->v.v_float = t > 0.0 ? floor (t) : ceil (t);

	return v;
}

static Value *
930
gnumeric_log (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log (t);

	return v;
}

static Value *
948
gnumeric_log2 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log2: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log (t) / M_LN2;

	return v;
}

static Value *
966
gnumeric_log10 (struct FunctionDefinition *i, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
{
	Value *v;
	float_t t;

	t = value_get_as_double (argv [0]);
	if (t < 0.0){
		*error_string = _("log10: domain error");
		return NULL;
	}
	v = g_new (Value, 1);
	v->type = VALUE_FLOAT;
	v->v.v_float = log10 (t);

	return v;
}

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
enum {
	OP_MIN,
	OP_MAX
};

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: