Commit a19304a8 authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

New function.

2002-09-20  Morten Welinder  <terra@diku.dk>

	* functions.c (gnumeric_fib): New function.
parent eba52b03
......@@ -28,6 +28,7 @@ Morten:
* Leak plugging in support libraries.
* Speeding up a few string value constructions.
* Really make autocorrect utf8 safe.
* Add FIB function.
--------------------------------------------------------------------------
Gnumeric 1.1.8
......
2002-09-20 Morten Welinder <terra@diku.dk>
* functions.c (gnumeric_fib): New function.
2002-08-25 Jody Goldberg <jody@gnome.org>
* Release 1.1.8
......
......@@ -1662,6 +1662,54 @@ gnumeric_factdouble (FunctionEvalInfo *ei, Value **argv)
/***************************************************************************/
static const char *help_fib = {
N_("@FUNCTION=FIB\n"
"@SYNTAX=FIB(number)\n"
"@DESCRIPTION="
"FIB function computes Fibonacci numbers.\n"
"\n"
"* If @number is not an integer, it is truncated.\n"
"* If @number is negative or zero FIB returns #NUM! error.\n"
"\n"
"@EXAMPLES=\n"
"FIB(12) equals 144.\n"
"\n"
"@SEEALSO=")
};
static Value *
gnumeric_fib (FunctionEvalInfo *ei, Value **argv)
{
static int fibs[47];
static int fib_count = sizeof (fibs) / sizeof (fibs[0]);
static gboolean inited = FALSE;
int n = value_get_as_int (argv[0]);
if (n <= 0)
return value_new_error (ei->pos, gnumeric_err_NUM );
if (n < fib_count) {
if (!inited) {
int i;
fibs[1] = fibs[2] = 1;
for (i = 3; i < fib_count; i++)
fibs[i] = fibs[i - 1] + fibs[i - 2];
inited = TRUE;
}
return value_new_int (fibs[n]);
} else {
gnum_float s5 = sqrtgnum (5.0);
gnum_float r1 = (1 + s5) / 2;
gnum_float r2 = (1 - s5) / 2;
// Use the Binet form.
return value_new_float ((powgnum (r1, n) - powgnum (r2, n)) / s5);
}
}
/***************************************************************************/
static const char *help_quotient = {
N_("@FUNCTION=QUOTIENT\n"
"@SYNTAX=QUOTIENT(numerator,denumerator)\n"
......@@ -3075,6 +3123,8 @@ const ModulePluginFunctionInfo math_functions[] = {
gnumeric_fact, NULL, NULL, NULL },
{ "factdouble", "f", N_("number"), &help_factdouble,
gnumeric_factdouble, NULL, NULL, NULL },
{ "fib", "f", N_("number"), &help_fib,
gnumeric_fib, NULL, NULL, NULL },
{ "combin", "ff", N_("n,k"), &help_combin,
gnumeric_combin, NULL, NULL, NULL },
{ "floor", "f|f", N_("number"), &help_floor,
......
......@@ -29,6 +29,7 @@
<function name="exp"/>
<function name="fact"/>
<function name="factdouble"/>
<function name="fib"/>
<function name="combin"/>
<function name="floor"/>
<function name="gcd"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment