Commit 419a71d7 authored by Morten Welinder's avatar Morten Welinder
Browse files

Post Michael-cleanup.

parent 0039c920
......@@ -10,6 +10,8 @@
#include "gnumeric.h"
#include "utils.h"
#include "func.h"
#include "mathfunc.h"
#include "collect.h"
#if 0
/* help template */
......@@ -1184,86 +1186,13 @@ static char *help_sum = {
"@SEEALSO=AVERAGE, COUNT")
};
static int
callback_function_sum (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
Value *result = (Value *) closure;
switch (value->type){
case VALUE_INTEGER:
if (result->type == VALUE_INTEGER){
if ((result->v.v_int > 0) && (value->v.v_int > 0)){
int sum = result->v.v_int + value->v.v_int;
if (sum < result->v.v_int){
double n = result->v.v_int + value->v.v_int;
result->type = VALUE_FLOAT;
result->v.v_float = n;
} else
result->v.v_int = sum;
} else if ((result->v.v_int < 0) && (value->v.v_int < 0)){
int sum = result->v.v_int + value->v.v_int;
if (sum > result->v.v_int){
double n = result->v.v_int + value->v.v_int;
result->type = VALUE_FLOAT;
result->v.v_float = n;
} else {
result->v.v_int = sum;
}
} else {
result->v.v_int += value->v.v_int;
}
} else
result->v.v_float += value->v.v_int;
break;
case VALUE_FLOAT:
if (result->type == VALUE_FLOAT)
result->v.v_float += value->v.v_float;
else {
double v = result->v.v_int;
/* cast to float */
result->type = VALUE_FLOAT;
result->v.v_float = v + value->v.v_float;
}
break;
case VALUE_STRING:
break;
default:
g_warning ("Unimplemented value->type in callback_function_sum : %s (%d)",
(value->type == VALUE_CELLRANGE) ? "CELLRANGE" :
(value->type == VALUE_ARRAY) ? "ARRAY" :
"UNKOWN!", value->type);
break;
}
return TRUE;
}
Value *
gnumeric_sum (FunctionEvalInfo *ei, GList *nodes)
{
Value *result;
result = g_new (Value, 1);
result->type = VALUE_INTEGER;
result->v.v_int = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sum,
result, nodes,
ei->error, TRUE)) {
value_release (result);
return NULL;
}
return result;
return float_range_function (nodes, ei,
range_sum,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_suma = {
......@@ -1285,20 +1214,10 @@ static char *help_suma = {
Value *
gnumeric_suma (FunctionEvalInfo *ei, GList *nodes)
{
Value *result;
result = g_new (Value, 1);
result->type = VALUE_INTEGER;
result->v.v_int = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sum,
result, nodes,
ei->error, TRUE)) {
value_release (result);
return NULL;
}
return result;
return float_range_function (nodes, ei,
range_sum,
COLLECT_ZERO_STRINGS | COLLECT_ZEROONE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_sumsq = {
......@@ -1313,47 +1232,14 @@ static char *help_sumsq = {
"@SEEALSO=SUM, COUNT")
};
typedef struct {
guint32 num;
float_t sum;
} math_sumsq_t;
static int
callback_function_sumsq (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_sumsq_t *mm = closure;
switch (value->type){
case VALUE_INTEGER:
mm->num++;
mm->sum += value->v.v_int * value->v.v_int;
break;
case VALUE_FLOAT:
mm->num++;
mm->sum += value->v.v_float * value->v.v_float;
break;
default:
/* ignore strings */
break;
}
return TRUE;
}
static Value *
gnumeric_sumsq (FunctionEvalInfo *ei, GList *nodes)
{
math_sumsq_t p;
p.num = 0;
p.sum = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sumsq,
&p, nodes,
ei->error, TRUE))
return NULL;
return value_new_float (p.sum);
return float_range_function (nodes, ei,
range_sumsq,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_multinomial = {
......@@ -1422,47 +1308,13 @@ static char *help_product = {
"@SEEALSO=SUM, COUNT")
};
typedef struct {
guint32 num;
float_t product;
} math_product_t;
static int
callback_function_product (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_product_t *mm = closure;
switch (value->type){
case VALUE_INTEGER:
mm->num++;
mm->product *= value->v.v_int;
break;
case VALUE_FLOAT:
mm->num++;
mm->product *= value->v.v_float;
break;
default:
/* ignore strings */
break;
}
return TRUE;
}
static Value *
gnumeric_product (FunctionEvalInfo *ei, GList *nodes)
{
math_product_t p;
p.num = 0;
p.product = 1;
if (!function_iterate_argument_values (&ei->pos, callback_function_product,
&p, nodes,
ei->error, TRUE))
return NULL;
return value_new_float (p.product);
return float_range_function (nodes, ei,
range_product,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_tan = {
......@@ -1733,7 +1585,7 @@ static Value *
gnumeric_randbetween (FunctionEvalInfo *ei, Value **argv)
{
int bottom, top;
double range, r;
double r;
bottom = value_get_as_int (argv[0]);
top = value_get_as_int (argv[1]);
......@@ -1741,10 +1593,7 @@ gnumeric_randbetween (FunctionEvalInfo *ei, Value **argv)
return function_error (ei, gnumeric_err_NUM );
r = bottom + floor ((top + 1.0 - bottom) * random_01 ());
if (fabs (r) < INT_MAX)
return value_new_int ((int)r);
else
return value_new_float (r);
return value_new_int ((int)r);
}
static char *help_rounddown = {
......@@ -2374,8 +2223,10 @@ gnumeric_seriessum (FunctionEvalInfo *ei, GList *nodes)
val = eval_expr (ei, tree);
if (!val) return NULL;
if (! VALUE_IS_NUMBER(val))
if (! VALUE_IS_NUMBER(val)) {
value_release (val);
return function_error (ei, gnumeric_err_VALUE);
}
x = value_get_as_float (val);
value_release (val);
......@@ -2409,7 +2260,7 @@ gnumeric_seriessum (FunctionEvalInfo *ei, GList *nodes)
return function_error (ei, gnumeric_err_VALUE);
}
m = value_get_as_int(val);
m = value_get_as_float(val);
nodes = nodes->next;
p.n = n;
......
......@@ -906,27 +906,11 @@ static char *help_average = {
Value *
gnumeric_average (FunctionEvalInfo *ei, GList *expr_node_list)
{
Value *vtmp;
float_t sum, count;
vtmp = gnumeric_sum (ei, expr_node_list);
if (!vtmp)
return NULL;
sum = value_get_as_float (vtmp);
value_release (vtmp);
vtmp = gnumeric_count (ei, expr_node_list);
if (!vtmp)
return NULL;
count = value_get_as_float (vtmp);
value_release (vtmp);
if (count == 0)
return function_error (ei, gnumeric_err_DIV0);
return value_new_float (sum / count);
return float_range_function (expr_node_list,
ei,
range_average,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_min = {
......@@ -1759,11 +1743,6 @@ gnumeric_standardize (FunctionEvalInfo *ei, Value **argv)
{
float_t x, mean, stddev;
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_float (argv [0]);
mean = value_get_as_float (argv [1]);
stddev = value_get_as_float (argv [2]);
......@@ -2530,7 +2509,7 @@ gnumeric_prob (FunctionEvalInfo *ei, Value **argv)
static char *help_steyx = {
N_("@FUNCTION=STEYX\n"
"@SYNTAX=STDYX(known_y's,known_x's)\n"
"@SYNTAX=STEYX(known_y's,known_x's)\n"
"@DESCRIPTION="
"STEYX function returns the standard error of the predicted "
......
......@@ -10,6 +10,8 @@
#include "gnumeric.h"
#include "utils.h"
#include "func.h"
#include "mathfunc.h"
#include "collect.h"
#if 0
/* help template */
......@@ -1184,86 +1186,13 @@ static char *help_sum = {
"@SEEALSO=AVERAGE, COUNT")
};
static int
callback_function_sum (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
Value *result = (Value *) closure;
switch (value->type){
case VALUE_INTEGER:
if (result->type == VALUE_INTEGER){
if ((result->v.v_int > 0) && (value->v.v_int > 0)){
int sum = result->v.v_int + value->v.v_int;
if (sum < result->v.v_int){
double n = result->v.v_int + value->v.v_int;
result->type = VALUE_FLOAT;
result->v.v_float = n;
} else
result->v.v_int = sum;
} else if ((result->v.v_int < 0) && (value->v.v_int < 0)){
int sum = result->v.v_int + value->v.v_int;
if (sum > result->v.v_int){
double n = result->v.v_int + value->v.v_int;
result->type = VALUE_FLOAT;
result->v.v_float = n;
} else {
result->v.v_int = sum;
}
} else {
result->v.v_int += value->v.v_int;
}
} else
result->v.v_float += value->v.v_int;
break;
case VALUE_FLOAT:
if (result->type == VALUE_FLOAT)
result->v.v_float += value->v.v_float;
else {
double v = result->v.v_int;
/* cast to float */
result->type = VALUE_FLOAT;
result->v.v_float = v + value->v.v_float;
}
break;
case VALUE_STRING:
break;
default:
g_warning ("Unimplemented value->type in callback_function_sum : %s (%d)",
(value->type == VALUE_CELLRANGE) ? "CELLRANGE" :
(value->type == VALUE_ARRAY) ? "ARRAY" :
"UNKOWN!", value->type);
break;
}
return TRUE;
}
Value *
gnumeric_sum (FunctionEvalInfo *ei, GList *nodes)
{
Value *result;
result = g_new (Value, 1);
result->type = VALUE_INTEGER;
result->v.v_int = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sum,
result, nodes,
ei->error, TRUE)) {
value_release (result);
return NULL;
}
return result;
return float_range_function (nodes, ei,
range_sum,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_suma = {
......@@ -1285,20 +1214,10 @@ static char *help_suma = {
Value *
gnumeric_suma (FunctionEvalInfo *ei, GList *nodes)
{
Value *result;
result = g_new (Value, 1);
result->type = VALUE_INTEGER;
result->v.v_int = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sum,
result, nodes,
ei->error, TRUE)) {
value_release (result);
return NULL;
}
return result;
return float_range_function (nodes, ei,
range_sum,
COLLECT_ZERO_STRINGS | COLLECT_ZEROONE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_sumsq = {
......@@ -1313,47 +1232,14 @@ static char *help_sumsq = {
"@SEEALSO=SUM, COUNT")
};
typedef struct {
guint32 num;
float_t sum;
} math_sumsq_t;
static int
callback_function_sumsq (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_sumsq_t *mm = closure;
switch (value->type){
case VALUE_INTEGER:
mm->num++;
mm->sum += value->v.v_int * value->v.v_int;
break;
case VALUE_FLOAT:
mm->num++;
mm->sum += value->v.v_float * value->v.v_float;
break;
default:
/* ignore strings */
break;
}
return TRUE;
}
static Value *
gnumeric_sumsq (FunctionEvalInfo *ei, GList *nodes)
{
math_sumsq_t p;
p.num = 0;
p.sum = 0;
if (!function_iterate_argument_values (&ei->pos, callback_function_sumsq,
&p, nodes,
ei->error, TRUE))
return NULL;
return value_new_float (p.sum);
return float_range_function (nodes, ei,
range_sumsq,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_multinomial = {
......@@ -1422,47 +1308,13 @@ static char *help_product = {
"@SEEALSO=SUM, COUNT")
};
typedef struct {
guint32 num;
float_t product;
} math_product_t;
static int
callback_function_product (const EvalPosition *ep, Value *value,
ErrorMessage *error, void *closure)
{
math_product_t *mm = closure;
switch (value->type){
case VALUE_INTEGER:
mm->num++;
mm->product *= value->v.v_int;
break;
case VALUE_FLOAT:
mm->num++;
mm->product *= value->v.v_float;
break;
default:
/* ignore strings */
break;
}
return TRUE;
}
static Value *
gnumeric_product (FunctionEvalInfo *ei, GList *nodes)
{
math_product_t p;
p.num = 0;
p.product = 1;
if (!function_iterate_argument_values (&ei->pos, callback_function_product,
&p, nodes,
ei->error, TRUE))
return NULL;
return value_new_float (p.product);
return float_range_function (nodes, ei,
range_product,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_tan = {
......@@ -1733,7 +1585,7 @@ static Value *
gnumeric_randbetween (FunctionEvalInfo *ei, Value **argv)
{
int bottom, top;
double range, r;
double r;
bottom = value_get_as_int (argv[0]);
top = value_get_as_int (argv[1]);
......@@ -1741,10 +1593,7 @@ gnumeric_randbetween (FunctionEvalInfo *ei, Value **argv)
return function_error (ei, gnumeric_err_NUM );
r = bottom + floor ((top + 1.0 - bottom) * random_01 ());
if (fabs (r) < INT_MAX)
return value_new_int ((int)r);
else
return value_new_float (r);
return value_new_int ((int)r);
}
static char *help_rounddown = {
......@@ -2374,8 +2223,10 @@ gnumeric_seriessum (FunctionEvalInfo *ei, GList *nodes)
val = eval_expr (ei, tree);
if (!val) return NULL;
if (! VALUE_IS_NUMBER(val))
if (! VALUE_IS_NUMBER(val)) {
value_release (val);
return function_error (ei, gnumeric_err_VALUE);
}
x = value_get_as_float (val);
value_release (val);
......@@ -2409,7 +2260,7 @@ gnumeric_seriessum (FunctionEvalInfo *ei, GList *nodes)
return function_error (ei, gnumeric_err_VALUE);
}
m = value_get_as_int(val);
m = value_get_as_float(val);
nodes = nodes->next;
p.n = n;
......
......@@ -906,27 +906,11 @@ static char *help_average = {
Value *
gnumeric_average (FunctionEvalInfo *ei, GList *expr_node_list)
{
Value *vtmp;
float_t sum, count;
vtmp = gnumeric_sum (ei, expr_node_list);
if (!vtmp)
return NULL;
sum = value_get_as_float (vtmp);
value_release (vtmp);
vtmp = gnumeric_count (ei, expr_node_list);
if (!vtmp)
return NULL;
count = value_get_as_float (vtmp);
value_release (vtmp);
if (count == 0)
return function_error (ei, gnumeric_err_DIV0);
return value_new_float (sum / count);
return float_range_function (expr_node_list,
ei,
range_average,
COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS,
gnumeric_err_VALUE, ei->error);
}
static char *help_min = {
......@@ -1759,11 +1743,6 @@ gnumeric_standardize (FunctionEvalInfo *ei, Value **argv)
{
float_t x, mean, stddev;
if (!VALUE_IS_NUMBER(argv[0]) ||
!VALUE_IS_NUMBER(argv[1]) ||