From 8aeb526a2a9b732ebec0c36ea0056701c7d28348 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 27 Oct 2015 12:51:40 +0800 Subject: [PATCH] Clean up abs usage versus implementation. Clarify ernd() behaviour. --- estruct.h | 12 ------------ eval.c | 32 +++++++++++++------------------- eval.h | 1 - 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/estruct.h b/estruct.h index 35dba87..caa95d2 100644 --- a/estruct.h +++ b/estruct.h @@ -183,12 +183,6 @@ #define poke(a,b,c,d) movedata(FP_SEG(c),FP_OFF(c),a,b,d) #endif -#if VMS -#define atoi xatoi -#define abs xabs -#define getname xgetname -#endif - #if MSDOS & MSC #include #include @@ -215,12 +209,6 @@ #define ENVFUNC 0 #endif -/* Internal defined functions */ - -#ifdef abs -#undef abs -#endif - /* DIFCASE represents the integer difference between upper and lower case letters. It is an xor-able value, which is fortunate, since the relative positions of upper to lower diff --git a/eval.c b/eval.c index ee77424..814a031 100644 --- a/eval.c +++ b/eval.c @@ -288,7 +288,7 @@ static int putctext( char *iline) static char *result ; /* string result */ static int ressize = 0 ; /* mark result as uninitialized */ -static int ernd( void) ; +static int ernd( int i) ; static int sindex( char *source, char *pattern) ; static char *xlat( char *source, char *lookup, char *trans) ; @@ -537,13 +537,7 @@ static char *gtfun( char *fname) { retstr = result ; break ; case UFRND | MONAMIC: - sz = atoi( arg1) ; - if( sz == 0) - sz = ernd() ; - else - sz = ernd() % abs( sz) + 1 ; - - retstr = i_to_a( sz) ; + retstr = i_to_a( ernd( atoi( arg1))) ; break ; case UFABS | MONAMIC: retstr = i_to_a( abs( atoi( arg1))) ; @@ -1311,21 +1305,21 @@ char *mklower(char *str) return str; } -/* - * take the absolute value of an integer - */ -int abs(int x) -{ - return x < 0 ? -x : x; -} - /* * returns a random integer + * ernd( 0) [ 0 .. 2147483647] + * ernd( i) [ 1 .. abs( i)] + * ernd( 1) [ 1] + * ernd( -2147483648) [ 1 .. 2147483647] actually 2147482413 */ -static int ernd( void) { +static int ernd( int i) { seed = seed * 1721 + 10007 ; - seed &= ~(1 << 31) ; /* abs() introduces 176719 periodicity */ - return seed ; + seed &= ~(1 << 31) ; /* avoid abs() which introduces 176719 periodicity */ + if( i == 0) + return seed ; + + i = i < 0 ? -i : i ; /* abs( i) */ + return seed % i + 1 ; } /* diff --git a/eval.h b/eval.h index e5943d5..701c2b8 100644 --- a/eval.h +++ b/eval.h @@ -22,7 +22,6 @@ int setvar( int f, int n) ; char *getval( char *token) ; int stol( char *val) ; char *mklower( char *str) ; -int abs( int x) ; int clrmes( int f, int n) ; int writemsg( int f, int n) ;