Clean up abs usage versus implementation.

Clarify ernd() behaviour.
This commit is contained in:
Renaud 2015-10-27 12:51:40 +08:00
parent 83b4028c95
commit 8aeb526a2a
3 changed files with 13 additions and 32 deletions

View File

@ -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 <dos.h>
#include <memory.h>
@ -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

32
eval.c
View File

@ -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 ;
}
/*

1
eval.h
View File

@ -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) ;