Commit 9dc8509d authored by Morten Welinder's avatar Morten Welinder

Fix GCD.

parent c875894a
1999-06-25 Morten Welinder <terra@diku.dk>
* src/fn-math.c (gnumeric_gcd): Cleanup and fix.
1999-06-24 Michael Meeks <michael@edenproject.org>
* src/expr.c (value_area_get_at_x_y): Return value_zero instead
......
1999-06-25 Morten Welinder <terra@diku.dk>
* src/fn-math.c (gnumeric_gcd): Cleanup and fix.
1999-06-24 Michael Meeks <michael@edenproject.org>
* src/expr.c (value_area_get_at_x_y): Return value_zero instead
......
1999-06-25 Morten Welinder <terra@diku.dk>
* src/fn-math.c (gnumeric_gcd): Cleanup and fix.
1999-06-24 Michael Meeks <michael@edenproject.org>
* src/expr.c (value_area_get_at_x_y): Return value_zero instead
......
1999-06-25 Morten Welinder <terra@diku.dk>
* src/fn-math.c (gnumeric_gcd): Cleanup and fix.
1999-06-24 Michael Meeks <michael@edenproject.org>
* src/expr.c (value_area_get_at_x_y): Return value_zero instead
......
......@@ -111,33 +111,6 @@ callback_function_sumxy (Sheet *sheet, int col, int row,
return TRUE;
}
static int
callback_function_makeslist (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_sums_t *mm = closure;
float_t x;
gpointer p;
switch (value->type) {
case VALUE_INTEGER:
x = value->v.v_int;
break;
case VALUE_FLOAT:
x = value->v.v_float;
break;
default:
return TRUE;
}
p = g_new(float_t, 1);
*((float_t *) p) = x;
mm->list = g_slist_append(mm->list, p);
mm->num++;
return TRUE;
}
typedef struct {
GSList *list;
criteria_test_fun_t fun;
......@@ -191,52 +164,32 @@ static char *help_gcd = {
"@SEEALSO=LCM")
};
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *expr_node_list)
static int
range_gcd (const float_t *xs, int n, float_t *res)
{
math_sums_t p;
GSList *current;
int a, b, old_gcd, new_gcd;
p.num = 0;
p.list = NULL;
if (function_iterate_argument_values (&ei->pos,
callback_function_makeslist,
&p, expr_node_list,
ei->error, TRUE) == FALSE) {
if (error_message_is_set (ei->error))
return function_error (ei, gnumeric_err_NUM);
}
if (p.list == NULL || p.list->next == NULL)
return function_error (ei, gnumeric_err_NUM);
if (n > 0) {
int i;
int gcd_so_far = 0;
try_again:
a = *((float_t *) p.list->data);
current=p.list->next;
b = *((float_t *) current->data);
old_gcd = gcd(a, b);
for (current=current->next; current!=NULL; current=current->next) {
b = *((float_t *) current->data);
new_gcd = gcd(a, b);
if (old_gcd != new_gcd) {
GSList *tmp;
for (tmp=p.list; tmp!=NULL; tmp=tmp->next) {
b = *((float_t *) current->data);
if (b % old_gcd == 0)
*((float_t *) current->data) =
b / old_gcd;
}
goto try_again;
for (i = 0; i < n; i++) {
if (xs[i] <= 0)
return 1;
else
gcd_so_far = gcd ((int)(floor (xs[i])), gcd_so_far);
}
}
for (current=p.list; current!=NULL; current=current->next)
g_free(current->data);
g_slist_free(p.list);
*res = gcd_so_far;
return 0;
} else
return 1;
}
return value_new_int (old_gcd);
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *nodes)
{
return float_range_function (nodes, ei,
range_gcd,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_NUM, ei->error);
}
static char *help_lcm = {
......
......@@ -536,11 +536,6 @@ gnumeric_negbinomdist (FunctionEvalInfo *ei, Value **argv)
int x, r;
float_t p;
if (!VALUE_IS_NUMBER(argv[0]) ||
!VALUE_IS_NUMBER(argv[1]) ||
!VALUE_IS_NUMBER(argv[2]))
return function_error (ei, gnumeric_err_VALUE);
x = value_get_as_int (argv [0]);
r = value_get_as_int (argv [1]);
p = value_get_as_float (argv[2]);
......@@ -557,9 +552,9 @@ static char *help_normsdist = {
"@DESCRIPTION="
"The NORMSDIST function returns the standard normal cumulative "
"distribution. @x is the value for which you want the distribution. "
"distribution. @x is the value for which you want the distribution."
"\n"
"Performing this function on a string or empty cell simply does nothing. "
"Performing this function on a string or empty cell simply does nothing."
"\n"
"@SEEALSO=NOMRDIST")
};
......@@ -685,9 +680,6 @@ gnumeric_fisherinv (FunctionEvalInfo *ei, Value **argv)
{
float_t y;
if (!VALUE_IS_NUMBER(argv [0]))
return function_error (ei, gnumeric_err_VALUE);
y = value_get_as_float (argv [0]);
return value_new_float ((exp (2*y)-1.0) / (exp (2*y)+1.0));
}
......@@ -1325,7 +1317,7 @@ static char *help_betadist = {
"@DESCRIPTION="
"BETADIST function returns the cumulative beta distribution. @a "
"is the optional lower bound of @x and @b is the optinal upper "
"is the optional lower bound of @x and @b is the optional upper "
"bound of @x. If @a is not given, BETADIST uses 0. If @b is "
"not given, BETADIST uses 1."
"\n"
......
......@@ -111,33 +111,6 @@ callback_function_sumxy (Sheet *sheet, int col, int row,
return TRUE;
}
static int
callback_function_makeslist (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_sums_t *mm = closure;
float_t x;
gpointer p;
switch (value->type) {
case VALUE_INTEGER:
x = value->v.v_int;
break;
case VALUE_FLOAT:
x = value->v.v_float;
break;
default:
return TRUE;
}
p = g_new(float_t, 1);
*((float_t *) p) = x;
mm->list = g_slist_append(mm->list, p);
mm->num++;
return TRUE;
}
typedef struct {
GSList *list;
criteria_test_fun_t fun;
......@@ -191,52 +164,32 @@ static char *help_gcd = {
"@SEEALSO=LCM")
};
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *expr_node_list)
static int
range_gcd (const float_t *xs, int n, float_t *res)
{
math_sums_t p;
GSList *current;
int a, b, old_gcd, new_gcd;
p.num = 0;
p.list = NULL;
if (function_iterate_argument_values (&ei->pos,
callback_function_makeslist,
&p, expr_node_list,
ei->error, TRUE) == FALSE) {
if (error_message_is_set (ei->error))
return function_error (ei, gnumeric_err_NUM);
}
if (p.list == NULL || p.list->next == NULL)
return function_error (ei, gnumeric_err_NUM);
if (n > 0) {
int i;
int gcd_so_far = 0;
try_again:
a = *((float_t *) p.list->data);
current=p.list->next;
b = *((float_t *) current->data);
old_gcd = gcd(a, b);
for (current=current->next; current!=NULL; current=current->next) {
b = *((float_t *) current->data);
new_gcd = gcd(a, b);
if (old_gcd != new_gcd) {
GSList *tmp;
for (tmp=p.list; tmp!=NULL; tmp=tmp->next) {
b = *((float_t *) current->data);
if (b % old_gcd == 0)
*((float_t *) current->data) =
b / old_gcd;
}
goto try_again;
for (i = 0; i < n; i++) {
if (xs[i] <= 0)
return 1;
else
gcd_so_far = gcd ((int)(floor (xs[i])), gcd_so_far);
}
}
for (current=p.list; current!=NULL; current=current->next)
g_free(current->data);
g_slist_free(p.list);
*res = gcd_so_far;
return 0;
} else
return 1;
}
return value_new_int (old_gcd);
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *nodes)
{
return float_range_function (nodes, ei,
range_gcd,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_NUM, ei->error);
}
static char *help_lcm = {
......
......@@ -536,11 +536,6 @@ gnumeric_negbinomdist (FunctionEvalInfo *ei, Value **argv)
int x, r;
float_t p;
if (!VALUE_IS_NUMBER(argv[0]) ||
!VALUE_IS_NUMBER(argv[1]) ||
!VALUE_IS_NUMBER(argv[2]))
return function_error (ei, gnumeric_err_VALUE);
x = value_get_as_int (argv [0]);
r = value_get_as_int (argv [1]);
p = value_get_as_float (argv[2]);
......@@ -557,9 +552,9 @@ static char *help_normsdist = {
"@DESCRIPTION="
"The NORMSDIST function returns the standard normal cumulative "
"distribution. @x is the value for which you want the distribution. "
"distribution. @x is the value for which you want the distribution."
"\n"
"Performing this function on a string or empty cell simply does nothing. "
"Performing this function on a string or empty cell simply does nothing."
"\n"
"@SEEALSO=NOMRDIST")
};
......@@ -685,9 +680,6 @@ gnumeric_fisherinv (FunctionEvalInfo *ei, Value **argv)
{
float_t y;
if (!VALUE_IS_NUMBER(argv [0]))
return function_error (ei, gnumeric_err_VALUE);
y = value_get_as_float (argv [0]);
return value_new_float ((exp (2*y)-1.0) / (exp (2*y)+1.0));
}
......@@ -1325,7 +1317,7 @@ static char *help_betadist = {
"@DESCRIPTION="
"BETADIST function returns the cumulative beta distribution. @a "
"is the optional lower bound of @x and @b is the optinal upper "
"is the optional lower bound of @x and @b is the optional upper "
"bound of @x. If @a is not given, BETADIST uses 0. If @b is "
"not given, BETADIST uses 1."
"\n"
......
......@@ -111,33 +111,6 @@ callback_function_sumxy (Sheet *sheet, int col, int row,
return TRUE;
}
static int
callback_function_makeslist (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_sums_t *mm = closure;
float_t x;
gpointer p;
switch (value->type) {
case VALUE_INTEGER:
x = value->v.v_int;
break;
case VALUE_FLOAT:
x = value->v.v_float;
break;
default:
return TRUE;
}
p = g_new(float_t, 1);
*((float_t *) p) = x;
mm->list = g_slist_append(mm->list, p);
mm->num++;
return TRUE;
}
typedef struct {
GSList *list;
criteria_test_fun_t fun;
......@@ -191,52 +164,32 @@ static char *help_gcd = {
"@SEEALSO=LCM")
};
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *expr_node_list)
static int
range_gcd (const float_t *xs, int n, float_t *res)
{
math_sums_t p;
GSList *current;
int a, b, old_gcd, new_gcd;
p.num = 0;
p.list = NULL;
if (function_iterate_argument_values (&ei->pos,
callback_function_makeslist,
&p, expr_node_list,
ei->error, TRUE) == FALSE) {
if (error_message_is_set (ei->error))
return function_error (ei, gnumeric_err_NUM);
}
if (p.list == NULL || p.list->next == NULL)
return function_error (ei, gnumeric_err_NUM);
if (n > 0) {
int i;
int gcd_so_far = 0;
try_again:
a = *((float_t *) p.list->data);
current=p.list->next;
b = *((float_t *) current->data);
old_gcd = gcd(a, b);
for (current=current->next; current!=NULL; current=current->next) {
b = *((float_t *) current->data);
new_gcd = gcd(a, b);
if (old_gcd != new_gcd) {
GSList *tmp;
for (tmp=p.list; tmp!=NULL; tmp=tmp->next) {
b = *((float_t *) current->data);
if (b % old_gcd == 0)
*((float_t *) current->data) =
b / old_gcd;
}
goto try_again;
for (i = 0; i < n; i++) {
if (xs[i] <= 0)
return 1;
else
gcd_so_far = gcd ((int)(floor (xs[i])), gcd_so_far);
}
}
for (current=p.list; current!=NULL; current=current->next)
g_free(current->data);
g_slist_free(p.list);
*res = gcd_so_far;
return 0;
} else
return 1;
}
return value_new_int (old_gcd);
static Value *
gnumeric_gcd (FunctionEvalInfo *ei, GList *nodes)
{
return float_range_function (nodes, ei,
range_gcd,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_NUM, ei->error);
}
static char *help_lcm = {
......
......@@ -536,11 +536,6 @@ gnumeric_negbinomdist (FunctionEvalInfo *ei, Value **argv)
int x, r;
float_t p;
if (!VALUE_IS_NUMBER(argv[0]) ||
!VALUE_IS_NUMBER(argv[1]) ||
!VALUE_IS_NUMBER(argv[2]))
return function_error (ei, gnumeric_err_VALUE);
x = value_get_as_int (argv [0]);
r = value_get_as_int (argv [1]);
p = value_get_as_float (argv[2]);
......@@ -557,9 +552,9 @@ static char *help_normsdist = {
"@DESCRIPTION="
"The NORMSDIST function returns the standard normal cumulative "
"distribution. @x is the value for which you want the distribution. "
"distribution. @x is the value for which you want the distribution."
"\n"
"Performing this function on a string or empty cell simply does nothing. "
"Performing this function on a string or empty cell simply does nothing."
"\n"
"@SEEALSO=NOMRDIST")
};
......@@ -685,9 +680,6 @@ gnumeric_fisherinv (FunctionEvalInfo *ei, Value **argv)
{
float_t y;
if (!VALUE_IS_NUMBER(argv [0]))
return function_error (ei, gnumeric_err_VALUE);
y = value_get_as_float (argv [0]);
return value_new_float ((exp (2*y)-1.0) / (exp (2*y)+1.0));
}
......@@ -1325,7 +1317,7 @@ static char *help_betadist = {
"@DESCRIPTION="
"BETADIST function returns the cumulative beta distribution. @a "
"is the optional lower bound of @x and @b is the optinal upper "
"is the optional lower bound of @x and @b is the optional upper "
"bound of @x. If @a is not given, BETADIST uses 0. If @b is "
"not given, BETADIST uses 1."
"\n"
......
......@@ -229,6 +229,10 @@ range_stddev_est (const float_t *xs, int n, float_t *res)
}
}
/*
* The skew of a population. Note, that this might is a special Excel
* meaning of "skew". Caveat emptor.
*/
int
range_skew (const float_t *xs, int n, float_t *res)
{
......
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