aboutsummaryrefslogtreecommitdiffstats
path: root/numbers.c
diff options
context:
space:
mode:
authornop <nop>1997-03-03 04:18:21 +0000
committernop <nop>1997-03-03 04:18:21 +0000
commit9cf5366eb0435a19fbc233d53765bb9aeae85be2 (patch)
tree2604ca1ea3ddf09fec07c7c26cea22637c23b5fa /numbers.c
parenta515162931c35db517995e3427cb41cee2a63a0a (diff)
downloadmoo-cvs-9cf5366eb0435a19fbc233d53765bb9aeae85be2.tar.gz
moo-cvs-9cf5366eb0435a19fbc233d53765bb9aeae85be2.tar.xz
moo-cvs-9cf5366eb0435a19fbc233d53765bb9aeae85be2.zip
GNU Indent normalization
Diffstat (limited to 'numbers.c')
-rw-r--r--numbers.c298
1 files changed, 148 insertions, 150 deletions
diff --git a/numbers.c b/numbers.c
index 9eb8317..a3b3edf 100644
--- a/numbers.c
+++ b/numbers.c
@@ -34,10 +34,10 @@
static int
parse_number(const char *str, int *result, int try_floating_point)
{
- char *p;
- int negative = 0;
-
- while (*str && *str == ' ')
+ char *p;
+ int negative = 0;
+
+ while (*str && *str == ' ')
str++;
if (*str == '-') {
str++;
@@ -59,11 +59,11 @@ parse_number(const char *str, int *result, int try_floating_point)
}
static int
-parse_object(const char *str, Objid *result)
+parse_object(const char *str, Objid * result)
{
- int number;
-
- while (*str && *str == ' ')
+ int number;
+
+ while (*str && *str == ' ')
str++;
if (*str == '#')
str++;
@@ -77,10 +77,10 @@ parse_object(const char *str, Objid *result)
static int
parse_float(const char *str, double *result)
{
- char *p;
- int negative = 0;
-
- while (*str && *str == ' ')
+ char *p;
+ int negative = 0;
+
+ while (*str && *str == ' ')
str++;
if (*str == '-') {
str++;
@@ -101,60 +101,60 @@ parse_float(const char *str, double *result)
enum error
become_integer(Var in, int *ret, int called_from_tonum)
-{
+{
switch (in.type) {
- case TYPE_INT:
+ case TYPE_INT:
*ret = in.v.num;
break;
- case TYPE_STR:
+ case TYPE_STR:
if (!(called_from_tonum
? parse_number(in.v.str, ret, 1)
: parse_object(in.v.str, ret)))
*ret = 0;
break;
- case TYPE_OBJ:
+ case TYPE_OBJ:
*ret = in.v.obj;
break;
- case TYPE_ERR:
+ case TYPE_ERR:
*ret = in.v.err;
break;
- case TYPE_FLOAT:
+ case TYPE_FLOAT:
if (*in.v.fnum < (double) INT_MIN || *in.v.fnum > (double) INT_MAX)
return E_FLOAT;
*ret = (int) *in.v.fnum;
break;
- case TYPE_LIST:
+ case TYPE_LIST:
return E_TYPE;
- default:
- errlog("BECOME_INTEGER: Impossible var type: %d\n", (int)in.type);
+ default:
+ errlog("BECOME_INTEGER: Impossible var type: %d\n", (int) in.type);
}
return E_NONE;
}
static enum error
become_float(Var in, double *ret)
-{
+{
switch (in.type) {
- case TYPE_INT:
- *ret = (double) in.v.num;
- break;
- case TYPE_STR:
- if (!parse_float(in.v.str, ret) || !IS_REAL(*ret))
- return E_INVARG;
- break;
- case TYPE_OBJ:
- *ret = (double) in.v.obj;
- break;
- case TYPE_ERR:
- *ret = (double) in.v.err;
- break;
- case TYPE_FLOAT:
- *ret = *in.v.fnum;
- break;
- case TYPE_LIST:
- return E_TYPE;
- default:
- errlog("BECOME_FLOAT: Impossible var type: %d\n", (int)in.type);
+ case TYPE_INT:
+ *ret = (double) in.v.num;
+ break;
+ case TYPE_STR:
+ if (!parse_float(in.v.str, ret) || !IS_REAL(*ret))
+ return E_INVARG;
+ break;
+ case TYPE_OBJ:
+ *ret = (double) in.v.obj;
+ break;
+ case TYPE_ERR:
+ *ret = (double) in.v.err;
+ break;
+ case TYPE_FLOAT:
+ *ret = *in.v.fnum;
+ break;
+ case TYPE_LIST:
+ return E_TYPE;
+ default:
+ errlog("BECOME_FLOAT: Impossible var type: %d\n", (int) in.type);
}
return E_NONE;
}
@@ -162,7 +162,7 @@ become_float(Var in, double *ret)
Var
new_float(double d)
{
- Var v;
+ Var v;
v.type = TYPE_FLOAT;
v.v.fnum = mymalloc(sizeof(double), M_FLOAT);
@@ -176,13 +176,13 @@ static int
to_float(Var v, double *dp)
{
switch (v.type) {
- case TYPE_INT:
+ case TYPE_INT:
*dp = (double) v.v.num;
break;
- case TYPE_FLOAT:
+ case TYPE_FLOAT:
*dp = *v.v.fnum;
break;
- default:
+ default:
return 0;
}
@@ -195,26 +195,25 @@ to_float(Var v, double *dp)
int
matherr(struct exception *x)
{
- switch (x->type)
- {
- case DOMAIN:
- case SING:
- errno = EDOM;
- /* fall thru to... */
- case OVERFLOW:
- x->retval = HUGE_VAL;
- return 1;
- case UNDERFLOW:
- x->retval = 0.0;
- return 1;
- default:
- return 0; /* Take default action */
- }
+ switch (x->type) {
+ case DOMAIN:
+ case SING:
+ errno = EDOM;
+ /* fall thru to... */
+ case OVERFLOW:
+ x->retval = HUGE_VAL;
+ return 1;
+ case UNDERFLOW:
+ x->retval = 0.0;
+ return 1;
+ default:
+ return 0; /* Take default action */
+ }
}
#endif
+
-
- /**** opcode implementations ****/
+/**** opcode implementations ****/
/*
* All of the following implementations are strict, not performing any
@@ -223,7 +222,7 @@ matherr(struct exception *x)
int
do_equals(Var lhs, Var rhs)
-{ /* LHS == RHS */
+{ /* LHS == RHS */
/* At least one of LHS and RHS is TYPE_FLOAT */
if (lhs.type != rhs.type)
@@ -246,7 +245,7 @@ compare_integers(int a, int b)
Var
compare_numbers(Var a, Var b)
{
- Var ans;
+ Var ans;
if (a.type != b.type) {
ans.type = TYPE_ERR;
@@ -255,8 +254,8 @@ compare_numbers(Var a, Var b)
ans.type = TYPE_INT;
ans.v.num = compare_integers(a.v.num, b.v.num);
} else {
- double aa = *a.v.fnum, bb = *b.v.fnum;
-
+ double aa = *a.v.fnum, bb = *b.v.fnum;
+
ans.type = TYPE_INT;
if (aa < bb)
ans.v.num = -1;
@@ -297,7 +296,6 @@ compare_numbers(Var a, Var b)
SIMPLE_BINARY(add, +)
SIMPLE_BINARY(subtract, -)
SIMPLE_BINARY(multiply, *)
-
#define DIVISION_OP(name, iop, fexpr) \
Var \
do_ ## name(Var a, Var b) \
@@ -330,14 +328,13 @@ SIMPLE_BINARY(multiply, *)
DIVISION_OP(divide, /, *a.v.fnum / *b.v.fnum)
DIVISION_OP(modulus, %, fmod(*a.v.fnum, *b.v.fnum))
-
Var
do_power(Var lhs, Var rhs)
-{ /* LHS ^ RHS */
- Var ans;
+{ /* LHS ^ RHS */
+ Var ans;
- if (lhs.type == TYPE_INT) { /* integer exponentiation */
- int a = lhs.v.num, b, r;
+ if (lhs.type == TYPE_INT) { /* integer exponentiation */
+ int a = lhs.v.num, b, r;
if (rhs.type != TYPE_INT)
goto type_error;
@@ -346,40 +343,44 @@ do_power(Var lhs, Var rhs)
ans.type = TYPE_INT;
if (b < 0)
switch (a) {
- case -1:
+ case -1:
ans.v.num = (b % 2 == 0 ? 1 : -1);
break;
- case 0:
+ case 0:
ans.type = TYPE_ERR;
ans.v.err = E_DIV;
break;
- case 1:
+ case 1:
ans.v.num = 1;
break;
- default:
+ default:
ans.v.num = 0;
+ } else {
+ r = 1;
+ while (b != 0) {
+ if (b % 2 != 0)
+ r *= a;
+ a *= a;
+ b >>= 1;
}
- else {
- r = 1;
- while (b != 0) {
- if (b % 2 != 0)
- r *= a;
- a *= a;
- b >>= 1;
- }
- ans.v.num = r;
+ ans.v.num = r;
}
- } else if (lhs.type == TYPE_FLOAT) { /* floating-point exponentiation */
+ } else if (lhs.type == TYPE_FLOAT) { /* floating-point exponentiation */
double d;
switch (rhs.type) {
- case TYPE_INT: d = (double) rhs.v.num; break;
- case TYPE_FLOAT: d = *rhs.v.fnum; break;
- default: goto type_error;
+ case TYPE_INT:
+ d = (double) rhs.v.num;
+ break;
+ case TYPE_FLOAT:
+ d = *rhs.v.fnum;
+ break;
+ default:
+ goto type_error;
}
errno = 0;
d = pow(*lhs.v.fnum, d);
- if (errno != 0 || !IS_REAL(d)) {
+ if (errno != 0 || !IS_REAL(d)) {
ans.type = TYPE_ERR;
ans.v.err = E_FLOAT;
} else
@@ -394,34 +395,34 @@ do_power(Var lhs, Var rhs)
ans.v.err = E_TYPE;
return ans;
}
-
- /**** built in functions ****/
+
+/**** built in functions ****/
static package
bf_toint(Var arglist, Byte next, void *vdata, Objid progr)
{
- Var r;
+ Var r;
enum error e;
-
+
r.type = TYPE_INT;
e = become_integer(arglist.v.list[1], &(r.v.num), 1);
-
+
free_var(arglist);
if (e != E_NONE)
return make_error_pack(e);
-
+
return make_var_pack(r);
}
static package
bf_tofloat(Var arglist, Byte next, void *vdata, Objid progr)
{
- Var r;
+ Var r;
enum error e;
-
+
r = new_float(0.0);
e = become_float(arglist.v.list[1], r.v.fnum);
-
+
free_var(arglist);
if (e == E_NONE)
return make_var_pack(r);
@@ -433,12 +434,12 @@ bf_tofloat(Var arglist, Byte next, void *vdata, Objid progr)
static package
bf_min(Var arglist, Byte next, void *vdata, Objid progr)
{
- Var r;
- int i, nargs = arglist.v.list[0].v.num;
- int bad_types = 0;
-
+ Var r;
+ int i, nargs = arglist.v.list[0].v.num;
+ int bad_types = 0;
+
r = arglist.v.list[1];
- if (r.type == TYPE_INT) { /* integers */
+ if (r.type == TYPE_INT) { /* integers */
for (i = 2; i <= nargs; i++)
if (arglist.v.list[i].type != TYPE_INT)
bad_types = 1;
@@ -463,12 +464,12 @@ bf_min(Var arglist, Byte next, void *vdata, Objid progr)
static package
bf_max(Var arglist, Byte next, void *vdata, Objid progr)
{
- Var r;
- int i, nargs = arglist.v.list[0].v.num;
- int bad_types = 0;
-
+ Var r;
+ int i, nargs = arglist.v.list[0].v.num;
+ int bad_types = 0;
+
r = arglist.v.list[1];
- if (r.type == TYPE_INT) { /* integers */
+ if (r.type == TYPE_INT) { /* integers */
for (i = 2; i <= nargs; i++)
if (arglist.v.list[i].type != TYPE_INT)
bad_types = 1;
@@ -481,7 +482,7 @@ bf_max(Var arglist, Byte next, void *vdata, Objid progr)
else if (*arglist.v.list[i].v.fnum > *r.v.fnum)
r = arglist.v.list[i];
}
-
+
r = var_ref(r);
free_var(arglist);
if (bad_types)
@@ -494,7 +495,7 @@ static package
bf_abs(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
-
+
r = var_dup(arglist.v.list[1]);
if (r.type == TYPE_INT) {
if (r.v.num < 0)
@@ -504,7 +505,7 @@ bf_abs(Var arglist, Byte next, void *vdata, Objid progr)
free_var(arglist);
return make_var_pack(r);
-}
+}
#define MATH_FUNC(name) \
static package \
@@ -525,29 +526,23 @@ bf_abs(Var arglist, Byte next, void *vdata, Objid progr)
}
MATH_FUNC(sqrt)
-
MATH_FUNC(sin)
MATH_FUNC(cos)
MATH_FUNC(tan)
-
MATH_FUNC(asin)
MATH_FUNC(acos)
-
MATH_FUNC(sinh)
MATH_FUNC(cosh)
MATH_FUNC(tanh)
-
MATH_FUNC(exp)
MATH_FUNC(log)
MATH_FUNC(log10)
-
MATH_FUNC(ceil)
MATH_FUNC(floor)
-
-static package
-bf_trunc(Var arglist, Byte next, void *vdata, Objid progr)
+ static package
+ bf_trunc(Var arglist, Byte next, void *vdata, Objid progr)
{
- double d;
+ double d;
d = *arglist.v.list[1].v.fnum;
errno = 0;
@@ -558,7 +553,7 @@ bf_trunc(Var arglist, Byte next, void *vdata, Objid progr)
free_var(arglist);
if (errno == EDOM)
return make_error_pack(E_INVARG);
- else if (errno != 0 || !IS_REAL(d))
+ else if (errno != 0 || !IS_REAL(d))
return make_error_pack(E_FLOAT);
else
return make_var_pack(new_float(d));
@@ -567,7 +562,7 @@ bf_trunc(Var arglist, Byte next, void *vdata, Objid progr)
static package
bf_atan(Var arglist, Byte next, void *vdata, Objid progr)
{
- double d, dd;
+ double d, dd;
d = *arglist.v.list[1].v.fnum;
errno = 0;
@@ -579,7 +574,7 @@ bf_atan(Var arglist, Byte next, void *vdata, Objid progr)
free_var(arglist);
if (errno == EDOM)
return make_error_pack(E_INVARG);
- else if (errno != 0 || !IS_REAL(d))
+ else if (errno != 0 || !IS_REAL(d))
return make_error_pack(E_FLOAT);
else
return make_var_pack(new_float(d));
@@ -599,40 +594,40 @@ static package
bf_ctime(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
- time_t c;
- char buffer[50];
-
+ time_t c;
+ char buffer[50];
+
if (arglist.v.list[0].v.num == 1) {
c = arglist.v.list[1].v.num;
} else {
c = time(0);
}
- { /* Format the time, including a timezone name */
+ { /* Format the time, including a timezone name */
#if HAVE_STRFTIME
strftime(buffer, 50, "%a %b %d %H:%M:%S %Y %Z", localtime(&c));
#else
# if HAVE_TM_ZONE
- struct tm *t = localtime(&c);
- char *tzname = t->tm_zone;
+ struct tm *t = localtime(&c);
+ char *tzname = t->tm_zone;
# else
# if !HAVE_TZNAME
- const char *tzname = "XXX";
+ const char *tzname = "XXX";
# endif
-# endif
-
+# endif
+
strcpy(buffer, ctime(&c));
buffer[24] = ' ';
strncpy(buffer + 25, tzname, 3);
buffer[28] = '\0';
#endif
}
-
+
if (buffer[8] == '0')
buffer[8] = ' ';
r.type = TYPE_STR;
r.v.str = str_dup(buffer);
-
+
free_var(arglist);
return make_var_pack(r);
}
@@ -642,13 +637,13 @@ bf_random(Var arglist, Byte next, void *vdata, Objid progr)
{
int nargs = arglist.v.list[0].v.num;
int num = (nargs >= 1 ? arglist.v.list[1].v.num : 1);
-
+
free_var(arglist);
-
- if (num <= 0)
+
+ if (num <= 0)
return make_error_pack(E_INVARG);
else {
- Var r;
+ Var r;
r.type = TYPE_INT;
if (nargs == 0)
@@ -661,13 +656,13 @@ bf_random(Var arglist, Byte next, void *vdata, Objid progr)
static package
bf_floatstr(Var arglist, Byte next, void *vdata, Objid progr)
-{ /* (float, precision [, sci-notation]) */
- double d = *arglist.v.list[1].v.fnum;
- int prec = arglist.v.list[2].v.num;
- int use_sci = (arglist.v.list[0].v.num >= 3
- && is_true(arglist.v.list[3]));
- char fmt[10], output[500]; /* enough for IEEE double */
- Var r;
+{ /* (float, precision [, sci-notation]) */
+ double d = *arglist.v.list[1].v.fnum;
+ int prec = arglist.v.list[2].v.num;
+ int use_sci = (arglist.v.list[0].v.num >= 3
+ && is_true(arglist.v.list[3]));
+ char fmt[10], output[500]; /* enough for IEEE double */
+ Var r;
free_var(arglist);
if (prec > DBL_DIG + 4)
@@ -687,7 +682,7 @@ Var zero; /* useful constant */
void
register_numbers(void)
-{
+{
zero.type = TYPE_INT;
zero.v.num = 0;
register_function("toint", 1, 1, bf_toint, TYPE_ANY);
@@ -723,9 +718,12 @@ register_numbers(void)
char rcsid_numbers[] = "$Id$";
/* $Log$
-/* Revision 1.1 1997/03/03 03:45:00 nop
-/* Initial revision
+/* Revision 1.2 1997/03/03 04:19:11 nop
+/* GNU Indent normalization
/*
+ * Revision 1.1.1.1 1997/03/03 03:45:00 nop
+ * LambdaMOO 1.8.0p5
+ *
* Revision 2.5 1996/03/19 07:15:27 pavel
* Fixed floatstr() to allow DBL_DIG + 4 digits. Release 1.8.0p2.
*