Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax errors and queries 1

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
0
0
PL
Hello all,

I've been adapting some code from the GNU Scientific Library (GSL) for my own evil purposes. The following is a random number generator "ran1" (same algorithm as found also in Numerical Recipes for C):

Code:
#define N_SHUFFLE 32
#define N_DIV (1 + 2147483646/N_SHUFFLE)

static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
static const double z_max = 1 - 1.2e-7f; 

static unsigned long int x;
static unsigned long int n;
static unsigned long int shuffle[N_SHUFFLE];


static inline unsigned long int ran1_get (void) {
	const long int h = x / q;
	const long int t = a * (x - h * q) - h * r;

	if(t<0)
		x = t + m;
	else
		x = t;

	{
		unsigned long int j = n / N_DIV;
		n = shuffle[j];
		shuffle[j] = x;
	}

	return(n);
}


static double ran1_get_double (void) {
	double z = ran1_get() / 2147483647.0f ;
	if(z > z_max)
		return(z_max);
	return(z);
}


static void ran1_set (unsigned long int s) {
	int i;

	if(s==0)
		s = 1;
	
	for(i=0;i<8;++i) {
		long int h = s / q;
		long int t = a * (s - h * q) - h * r;
		if(t < 0)
			t += m;
		s = t;
	}

	for(i=N_SHUFFLE-1;i>=0;--i) {
		long int h = s/q;
		long int t = a * (s - h * q) - h * r;
		if(t < 0)
			t += m;
		s = t;
		shuffle[i] = s;
	}

	x = s;
	n = s;

	return;
}

Anyway, the main problem is that the compiler returns an error for the "static inline" bit before the function ran1_get(void). Any ideas why?

Second, what are the benefits of declaring functions as static and/or inline as is done here? :)

Third, why in the loops in ran1_set are the long int variables h and t redeclared each time round in the loop?

Many thanks,

-- Joe
 
Well the bit you have in {} is not part of the if/else code.

And inline isn't valid C.

You need either C++, C99 or some compiler specific extension to get an inline function.

--
 
inline is a C++ thing. Just remove it and it should be OK in C.

static means no other module can see it - opposite of public.

declarations: no reason whatsoever. Some people have this thing about getting rid of stuff when you no longer have a use for it and only declaring stuff when you need it. Others will declare everything up front leave it hanging around until the end of the routine. It is a personal thing.
 
Well the bit you have in {} is not part of the if/else code.
That bit I know, but it's perfectly valid ANSI C as far as I know. (gcc with all the ansi options lets it go, anyway!)

Inline was the problem, and also the fact that static was being inappropriately used---in the GSL this module contained other things which got round those issues.
 
The {} are just to make the declaration legal. Unlike C++, you can't declare stuff all over the place but you can if it follows a {.
 
The {} are just to make the declaration legal. Unlike C++, you can't declare stuff all over the place but you can if it follows a {.
Yep. For what it's worth, here's the updated, seemingly correct version of the program, which is an implementation of the "ran1" algorithm found, among other places, in Numerical Recipes in C. Unlike that code, though, the implementation in the GSL is released under GPL.

So, this code is also GPL. I've taken the algorithm and made it a standalone module that can be used all by itself and does not require the whole GSL to be installed.

Hope it's worthwhile! If anyone has any comments or suggestions for improvements, they'd be welcome.

I called it gsl-_ran1.c because it's taken from the GSL (hence gsl-) and it implements ran1. ;-)

Many thanks for advice, & best wishes,

-- Joe


Code:
/* gsl-_ran1.c
 *
 * This progam is a modified version of the rng/ran1.c code in the GNU
 * Scientific Library (GSL), version 1.7.  It will stand alone as a
 * module and does not need any code other than is contained herein!
 *                                      -- Joseph Rushton Wakeling, 2005
 * 
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
 * Copyright (C) 2005 Joseph Rushton Wakeling
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#define N_SHUFFLE 32
#define N_DIV (1 + 2147483646/N_SHUFFLE)

static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
static const double z_max = 1 - 1.2e-7f; 

static unsigned long int x;
static unsigned long int n;
static unsigned long int shuffle[N_SHUFFLE];


static unsigned long int ran1_get (void) {
	const long int h = x / q;
	const long int t = a * (x - h * q) - h * r;

	if(t<0)
		x = t + m;
	else
		x = t;

	{
		unsigned long int j = n / N_DIV;
		n = shuffle[j];
		shuffle[j] = x;
	}

	return(n);
}


double ran1_get_double (void) {
	double z = ran1_get() / 2147483647.0f ;
	if(z > z_max)
		return(z_max);
	return(z);
}


void ran1_set (unsigned long int s) {
	int i;
	long int h, t;

	if(s==0)
		s = 1;
	
	for(i=0;i<8;++i) {
		h = s / q;
		t = a * (s - h * q) - h * r;
		if(t < 0)
			t += m;
		s = t;
	}

	for(i=N_SHUFFLE-1;i>=0;--i) {
		h = s/q;
		t = a * (s - h * q) - h * r;
		if(t < 0)
			t += m;
		s = t;
		shuffle[i] = s;
	}

	x = s;
	n = s;

	return;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top