C function call Numerical Recipes Chapter 10.3 - c

I have this function. I'm kinda green in C and I don't know how to call this function to get a result:
float dbrent(float ax, float bx, float cx, float (*f)(float),float (*df)(float), float tol, float* xmin)
{
int iter, ok1, ok2;
float a, b, d, d1, d2, du, dv, dw, dx, e = 0.0;
float fu, fv, fw, fx, olde, tol1, tol2, u, u1, u2, v, w, x, xm;
a = (ax < cx ? ax : cx);
b = (ax > cx ? ax : cx);
x = w = v = bx;
fw = fv = fx = (*f)(x);
dw = dv = dx = (*df)(x);
for (iter = 1; iter <= ITMAX; iter++)
{
xm = 0.5 * (a + b);
tol1 = tol * fabs(x) + ZEPS;
tol2 = 2.0 * tol1;
if (fabs(x - xm) <= (tol2 - 0.5 * (b - a))) {
*xmin = x;
return fx;
}
if (fabs(e) > tol1) {
d1 = 2.0 * (b - a);
d2 = d1;
if (dw != dx) d1 = (w - x) * dx / (dx - dw);
if (dv != dx) d2 = (v - x) * dx / (dx - dv);
u1 = x + d1;
u2 = x + d2;
ok1 = (a - u1) * (u1 - b) > 0.0 && dx * d1 <= 0.0;
ok2 = (a - u2) * (u2 - b) > 0.0 && dx * d2 <= 0.0;
olde = e;
e = d;
if (ok1 || ok2) {
if (ok1 && ok2)
d = (fabs(d1) < fabs(d2) ? d1 : d2);
else if (ok1)
d = d1;
else
d = d2;
if (fabs(d) <= fabs(0.5 * olde)) {
u = x + d;
if (u - a < tol2 || b - u < tol2)
d = SIGN(tol1, xm - x);
}
else {
d = 0.5 * (e = (dx >= 0.0 ? a - x : b - x));
}
}
else {
d = 0.5 * (e = (dx >= 0.0 ? a - x : b - x));
}
}
else {
d = 0.5 * (e = (dx >= 0.0 ? a - x : b - x));
}
if (fabs(d) >= tol1) {
u = x + d;
fu = (*f)(u);
}
else {
u = x + SIGN(tol1, d);
fu = (*f)(u);
if (fu > fx) {
*xmin = x;
return fx;
}
}
du = (*df)(u);
if (fu <= fx) {
if (u >= x) a = x; else b = x;
MOV3(v, fv, dv, w, fw, dw)
MOV3(w, fw, dw, x, fx, dx)
MOV3(x, fx, dx, u, fu, du)
}
else {
if (u < x) a = u; else b = u;
if (fu <= fw || w == x) {
MOV3(v, fv, dv, w, fw, dw)
MOV3(w, fw, dw, u, fu, du)
}
else if (fu < fv || v == x || v == w) {
MOV3(v, fv, dv, u, fu, du)
}
}
}
cout<<"Too many iterations in routine dbrent";
return 0.0;
}
This code is from Numerical Recipes Chapter 10.3. I need to call this function and get the result but I don't know how to call it, especially when there is a syntax like float (*)(float).
Thanks in advance.

Well, let's suppose you have the following functions:
float df_(float yourFloat) {
...
return yourFloat;
}
and
float f_(float yourFloat) {
...
return yourFloat;
}
Then you could call your function as follows:
float t = dbrent(ax, bx, cx, &f_, &df_, tol, &xmin);
Where &xmin hands over the address of some float variable xmin.
So, in essence, you supply the address of your functions f_ and df_ and your float variable xmin.

We can call dbrent in several ways, the below shown is one of the ways.
Your function prototype
float dbrent ( float ax, float bx, float cx,
float (*f)(float),
float (*df)(float),
float tol, float* xmin
)
what you need is a func_f (you can use any name for function) that takes a float value as an argument and returns a float value as shown below.
float func_f(float num)
{
/* code */
}
you need one more function with same type as mentioned above
float func_df(float num)
{
/* code */
}
Following is the setup you need to make before calling dbrent
some_funtion()
{
// setup to call dbrent
// guess you know you need some values in it.
float ax, bx, cx, tol, xmin;
float (*f)(float);
float (*df)(float);
f = func_f;
df = func_df;
//one way of calling dbrent
dbrent(ax, bx, cx, f, df,tol, xmin);
//you can even directly call using the function names instead of using pointers
dbrent(ax, bx, cx, func_f, func_df, tol, xmin);
}
NOTE: variables have not been initialized for simplicity, make sure you pass valid variables when calling the function.

Related

Perlin Noise function - values way out of range

So, I've been trying to use Perlin Noise in my project, and bizarrely the noise function outputs mostly values way out of the [-1, 1] bounds. I've implemented it in C through Perlin's code for his improved noise in Java, and another useful implementation in C++ by sol. I've ran through it at least 20 times and made sure it was exactly the same, but couldn't find any causes for the issue.
Here is how it looks:
// DEFAULT_PERMUTATION is defined through the preprocessor.
// It is basically an initializer of the same array used in Perlin's
// implementation, except it has been duplicated into a 512 element array
double simpleNoise(double x, double y, double z){
double res;
int p[512] = DEFAULT_PERMUTATION;
int X = (int) floor(x) & 255;
int Y = (int) floor(y) & 255;
int Z = (int) floor(z) & 255;
double u = fade(x);
double v = fade(y);
double w = fade(z);
int A = p[X] + Y;
int AA = p[A] + Z;
int AB = p[A + 1] + Z;
int B = p[X + 1] + Y;
int BA = p[B] + Z;
int BB = p[B + 1] + Z;
x -= floor(x);
y -= floor(y);
z -= floor(z);
res = lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
grad(p[BA+1], x-1, y , z-1 )),
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 ))));
return res;
}
The other private functions are pretty straightfoward too:
double fade(double t){
return t * t * t * (t * (t * 6 - 15) + 10);
}
double lerp(double t, double a, double b){
return a + t * (b - a);
}
double grad(int hash, double x, double y, double z){
int h = hash & 15;
double u = h<8 ? x : y,
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
In my project, the noise looks like this, due to the fact that the color only cares about values between 0 and 1, but tests with some positions give absurd values like -15, 32, some bigger than thousands. Feeding the values (1,-4,1), (2,5,1), (2,0,0) are interesting to say the least.
What could the problem be?
EDIT: For investigation purposes, this program lists inconsistencies found:
#define DEFAULT_PERMUTATION { \
151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142, \
8,99,37,240,21,10,23,190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117, \
35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71, \
134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41, \
55,46,245,40,244,102,143,54, 65,25,63,161,1,216,80,73,209,76,132,187,208, 89, \
18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226, \
250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182, \
189,28,42,223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, \
43,172,9,129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246, \
97,228,251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239, \
107,49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, \
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,\
151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142, \
8,99,37,240,21,10,23,190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117, \
35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71, \
134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41, \
55,46,245,40,244,102,143,54, 65,25,63,161,1,216,80,73,209,76,132,187,208, 89, \
18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226, \
250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182, \
189,28,42,223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, \
43,172,9,129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246, \
97,228,251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239, \
107,49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, \
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 }
#define SIZE 10
double fade(double t){
return t * t * t * (t * (t * 6 - 15) + 10);
}
double lerp(double t, double a, double b){
return a + t * (b - a);
}
double grad(int hash, double x, double y, double z){
int h = hash & 15;
double u = h<8 ? x : y,
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
double simpleNoise(double x, double y, double z){
double res;
int p[512] = DEFAULT_PERMUTATION;
int X = (int) floor(x) & 255;
int Y = (int) floor(y) & 255;
int Z = (int) floor(z) & 255;
double u = fade(x);
double v = fade(y);
double w = fade(z);
int A = p[X] + Y;
int AA = p[A] + Z;
int AB = p[A + 1] + Z;
int B = p[X + 1] + Y;
int BA = p[B] + Z;
int BB = p[B + 1] + Z;
x -= floor(x);
y -= floor(y);
z -= floor(z);
res = lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
grad(p[BA+1], x-1, y , z-1 )),
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 ))));
return res;
}
main(){
int i, j, k, n=0;
double e;
for(i=-SIZE/2; i<SIZE/2; i++){
for(j=-SIZE/2; j<SIZE/2; j++){
for(k=-SIZE/2; k<SIZE/2; k++){
e = simpleNoise(i, j, k);
if(fabs(e) > 1){
printf("P(%d,%d,%d): %f\n", i, j, k, e);
n++;
}
}
}
}
printf("%d inconsistencies found", n);
}
The reference implementation here does
x -= floor(x);
y -= floor(y);
z -= floor(z);
before such steps as
double u = fade(x);
double v = fade(y);
double w = fade(z);
Your implementation moved that step much further down the code. Why?
If you move it back to its original location, in your "investigation" code the noise function will always expectedly return zero, since Perlin noise function is supposed to return zero at integer points.

Perlin Noise Attempt

I am trying my attempt at Perlin Noise (3-dimensional) as outlined in this document: http://lodev.org/cgtutor/randomnoise.html
However, this is what I'm getting.
It looks like the smoothing isn't working. You can see blocks the size of the 'size' parameter. Can someone point out what I'm doing wrong?
Here's my code:
%ffp
ctl(1):standard,"Size",range=(1,256), pos=(300,20), size=(120,*),val=64,track, action=preview
onFilterStart:
{
allocArray(9,64,64,64,4); // Array for noise depth
for(int z = 0; z < 64; z++)
for(int y = 0; y < 64; y++)
for(int x = 0; x < 64; x++) {
fputArray(9,x,y,z,(float)(rand() % 32768) / 32768.0);
}
return false;
}
forEveryTile:
{
double fractX,fractY,fractZ,xx,yy,zz;
int x1,y1,z1,x2,y2,z2,col;
double value = 0.0, value2 = 0.0, size, isize=(float)ctl(1);
// int X=screen Width, int Y=screen Height
for(int y = 0; y < Y; y++) {
for(int x = 0; x < X; x++) {
//for(int z = 0; z < 64; z++) {
value2 = 0.0;
size = isize;
while (size >=1.0) {
xx=(float)x/size;
yy=(float)y/size;
zz=(float)clock()/size;
fractX = xx - (int)(xx);
fractY = yy - (int)(yy);
fractZ = zz - (int)(zz);
x1 = ((int)(xx) + 64) % 64;
y1 = ((int)(yy) + 64) % 64;
z1 = ((int)(zz) + 64) % 64;
x2 = (x1 + 64- 1) % 64;
y2 = (y1 + 64- 1) % 64;
z2 = (z1 + 64- 1) % 64;
value=0.0;
value += fractX * fractY * fractZ * fgetArray(9,z1,y1,x1);
value += fractX * (1 - fractY) * fractZ * fgetArray(9,z1,y2,x1);
value += (1 - fractX) * fractY * fractZ * fgetArray(9,z1,y1,x2);
value += (1 - fractX) * (1 - fractY) * fractZ * fgetArray(9,z1,y2,x2);
value += fractX * fractY * (1 - fractZ) * fgetArray(9,z2,y1,x1);
value += fractX * (1 - fractY) * (1 - fractZ) * fgetArray(9,z2,y2,x1);
value += (1 - fractX) * fractY * (1 - fractZ) * fgetArray(9,z2,y1,x2);
value += (1 - fractX) * (1 - fractY) * (1 - fractZ) * fgetArray(9,z2,y2,x2);
value2 += value*size;
size /= 2.0;
}
col=(int)((float)(128.0 * value2 / isize));
col=max(min(col,255),0);
psetp(x,y,RGB(col,col,col));
//} //z
} //x
} //y
return true;
}
Your code is kind of hard to read as written.
For Perlin noise start out with a integer noise function, that behaves like a hash.
float noise(int x, int y, int z) { return hash(x+y*5+z*7); }
or
float noise(int x, int y, int z) { return array[x%w+y%h*w+z%d*w*h]; }
Those are just examples. The important part is that noise(x,y,z) = noise(x,y,z). The noise function has to return the same value for the same parameters every time.
There is a problem though: The noise function only takes integer parameters! But we would like to sample it at float values.
float noisesample (float x, float y, float z) { ... }
The easiest way to to that is using linear filtering. Any positive float value is between (int)pos and ((int)pos)+1. At sub-position pos-(int)pos. This gets us:
float Lerp(float a, float b, float f) { return a+(b-a)*f; }
Where f is the sub-position in the [0..1] range and a,b are the values to the left and right. If f is 0, Lerp returns a, if it is 1, it returns b. In between it does linear interpolation.
So use this for a simple 1D noisesample function:
float noisesample(float x) { return Lerp(noise((int)x), noise((int)x+1), fract(x) }
with
float fract(float x) { return x-(int)x; }
I am using (int)x liberally here, it is the same as floor(x) if x is positive.
To go from a single parameter noisesample to x,y is easy: Do the Lerp twice for x at y and y+1, and Lerp between those:
float noisesample(float x, float y) {
float y0 = Lerp(noise((int)x,(int)y), noise((int)x+1,(int)y), fract(x) }
float y1 = Lerp(noise((int)x,(int)y+1), noise((int)x+1,(int)y+1), fract(x) }
return Lerp ( y0, y1, fract(y) );
}
First interpolate x, twice, then interpolate between the results in y. In total we sample noise() 4 times. I leave it as an exercise how to write noisesample ( float x, float y, float z). It will sample noise() eight times and call Lerp 7 times.
All that got us is that we can sample noise (somewhat smooth - there are smoother ways!) at float coordinates. And that is what we need to make perlin noise!
float perlin(float x, float y, float z, int oc=4) {
// maybe: x = x*2^oc, y, z...
float r = 0;
float s = 1;
for ( int i=0; i<oc; i++ ) {
r += noisesample(x,y,z) * s;
s/=2.0f; // to taste
x/=2.0f;
y/=2.0f;
z/=2.0f;
}
return r;
}
The key idea is to understand sampling. It's just a combination of sampling a simple integer noise function.

Generate a random point in a specific plane in C

I have a 2D plane in three dimensions: x+y+z=1, and I want to generate random points(x,y,z) on the plane. How can I choose these points so that they are distributed uniformly?
The Problem
As mentioned in the comments, the question was under specified. Despite that it's a interesting question. Because no distribution was given I just picked one. Here is the more precise(?)/general(?) question I will answer:
Suppose I have a plane P in R^3 defined by ax + by + cz = d.
Let c be in the point on P closest to the origin.
How can I uniformly choose a point on P within some radius r of c?
The Algorithm
Let n = (a,b,c). n is the vector normal to P.
direction
Generate any non-zero vector on the plane ax + by + cz = d, call it w. You can do this by taking the cross product of n with any non-zero vector not parallel to n.
Rotate w around n by a random angle in [0,2pi). You can do this using http://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula.
so , now you got direction by normalizing it
direction = direction / direction.magnitude
origin of the ray
If d is 0, we're done. Otherwise:
Calculate c = distance of plane from Vector3(0 , 0 , 0)
according to http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_plane.
Translate Origin of ray
origin of the ray = vector3.zero + c * ( n )
scale = random.range(min , max)
So the point is
origin_of_the_ray + scale * (direction_)
The Code
Here is my C implementation of the algorithm. I wrote all the vector machinery from scratch so it's a little messy. I have not tested this throughly.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
typedef struct {
double x, y, z;
} vec3;
vec3 vec(double x, double y, double z);
vec3 crossp(vec3 u, vec3 v);
vec3 add(vec3 u, vec3 v);
double dotp(vec3 u, vec3 v);
double norm2(vec3 u);
double norm(vec3 u);
vec3 scale(vec3 u, double s);
vec3 normalize(vec3 u);
void print_vec3(vec3 u);
// generates a random point on the plane ax + by + cz = d
vec3 random_on_plane(double r, double a, double b, double c, double d) {
// The normal vector for the plane
vec3 n = vec(a, b, c);
// create a normal vector on the plane ax + by + cz = 0
// we take any vector not parallel to n
// and find the cross product
vec3 w;
if (n.x == 0)
w = crossp(n, vec(1,0,0));
else
w = crossp(n, vec(0,0,1));
// rotate the vector around n by a random angle
// using Rodrigues' rotation formula
// http://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
double theta = ((double)rand() / RAND_MAX) * M_PI;
vec3 k = normalize(n);
w = add(scale(w, cos(theta)),
scale(crossp(k, w), sin(theta)));
// Scale the vector fill our disk.
// If the radius is zero, generate unit vectors
if (r == 0) {
w = scale(w, r/norm(w));
} else {
double rand_r = ((double)rand() / RAND_MAX) * r;
w = scale(w, rand_r/norm(w));
}
// now translate the vector from ax + by + cz = 0
// to the plane ax + by + cz = d
// http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_plane
if (d != 0) {
vec3 t = scale(n, d / norm2(n));
w = add(w, t);
}
return w;
}
int main(void) {
int i;
srand(time(NULL));
for (i = 0; i < 100; i++) {
vec3 r = random_on_plane(10, 1, 1, 1, 1);
printf("random v = ");
print_vec3(r);
printf("sum = %f, norm = %f\n", r.x + r.y + r.z, norm(r));
}
}
vec3 vec(double x, double y, double z) {
vec3 u;
u.x = x;
u.y = y;
u.z = z;
return u;
}
vec3 crossp(vec3 u, vec3 v) {
vec3 w;
w.x = (u.y * v.z) - (u.z * v.y);
w.y = (u.z * v.x) - (u.x * v.z);
w.z = (u.x * v.y) - (u.y * v.x);
return w;
}
double dotp(vec3 u, vec3 v) {
return (u.x * v.x) + (u.y * v.y) + (u.z * v.z);
}
double norm2(vec3 u) {
return dotp(u, u);
}
double norm(vec3 u) {
return sqrt(norm2(u));
}
vec3 scale(vec3 u, double s) {
u.x *= s;
u.y *= s;
u.z *= s;
return u;
}
vec3 add(vec3 u, vec3 v) {
u.x += v.x;
u.y += v.y;
u.z += v.z;
return u;
}
vec3 normalize(vec3 u) {
return scale(u, 1/norm(u));
}
void print_vec3(vec3 u) {
printf("%f %f %f\n", u.x, u.y, u.z);
}
Eugene had it almost right: generate two random numbers on the interval [0,1), call them A, B. Then x = min(A,B), y = max(A,B) - x, z = 1 - (x + y). Basically, you pick two points on the line [0,1) and your three coordinates are the three intervals defined by those two points.
I'll first give you a simple algorithm
x = rand()
y = rand()
z = 1 - x - y
Now lets see an implementation of that algorithm
This code will produce any sort of numbers ( +ve or -ve )
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int x= ( rand() - rand() ) ;
int y= ( rand() - rand() ) ;
int z=1-x-y;
printf("x=%d y=%d z=%d",x,y,z);
}
just use srand() to seed the random number generator, and use rand() to assign a random number.
If you need to create random numbers with a range, then use rand() % ( maxnumber + 1 ) where maxnumber is the maximum value you want.
If you want all of your numbers to be positive, then try this
int main()
{
srand(time(NULL));
int x, y , z = -1;
while ( z < 0 )
{
x = rand() ;
y = rand() ;
z = 1 - (x + y );
}
printf("x=%d y=%d z=%d",x,y,z);
}
WARNING
the above code might take some time to execute, so don't expect an instant result

Calculating exponents in C without pow()

int main ()
{
int n = 0;
int base = 0;
while(n < 10)
{
int x = 2;
int answer = power(x, n);
float neganswer = negpower(x, n);
printf("%d %d %f\n", base, answer, neganswer);
base++;
n++;
}
return EXIT_SUCCESS;
}
int power(int base, int power)
{
int result, i;
result = 1;
for (i=0; i < power; i++)
{
result *= base;
}
return result;
}
int negpower(int base, int power)
{
float result, i;
result = 1.0;
for (i=0; i < power; i++)
{
result = result / base;
}
return result;
}
So I'm trying to call upon this function that i've made, and I think its calculating it correctly, however it is only outputting 1.0000000 followed directly by 0.0000000. I think I've got problems with carrying the float value, can anyone chime in?
Thanks
This is because you are returning a float from negpower() which has return type of int and assigning it to a float neganswer.
Change
int negpower(int base, int power)
to
float negpower(int base, int power)
Output:
Side note:
Always add required header files.
A prototype should be declared if a function definition appears after the main().
The answer is much simpler. Your negpower function returns an int, when you actually return a float from it. Change the prototype and it should work alright.
This is optimized library if you are interested:
#ifdef DOCUMENTATION
title pow x raised to power y
index x raised to power y
usage
.s
double x, y, f, pow();
.br
f = pow(x, y);
.s
description
.s
Returns value of x raised to power y
.s
diagnostics
.s
There are three error possible error messages from this function.
.s
If the x argument is negative the message 'pow arg negative',
followed by the value of x, is written to stderr. The value
of pow for |x| is returned.
.s
If x = 0.0 and y <= 0.0 or if result overflows the message 'pow
overflow', followed by the value of y, is written to stderr.
The value of HUGE is returned.
.s
If the result underflows and if warnings are enabled (normally not),
the message 'pow underflow', followed by the value of y, is written
to stderr. The value of 0 is returned.
.s
The suggestion of Cody and Waite, that the domain be reduced to
simplify the overflow test, has been adopted, consequently overflow
is reported if the result would exceed HUGE * 2**(-1/16).
2**(-1/16) is approximately 0.9576.
.s
internal
.s
Algorithm from Cody and Waite pp. 84-124. This algorithm required
two auxiliary programs POWGA1 and POWGA2 to calculate, respectively,
the arrays a1[] and a2[] used to represent the powers of 2**(-1/16)
to more than machine precision.
The source code for these programs are in the files POWGA1.AUX and
POWGA2.AUX. The octal table on page 98 of Cody and Waite is in the
file POWOCT.DAT which is required on stdin by POWGA2.
.s
author
.s
Hamish Ross.
.s
date
.s
27-Jan-85
#endif
#include <math.h>
#define MAXEXP 2031 /* (MAX_EXP * 16) - 1 */
#define MINEXP -2047 /* (MIN_EXP * 16) - 1 */
static double a1[] = {
1.0,
0.95760328069857365,
0.91700404320467123,
0.87812608018664974,
0.84089641525371454,
0.80524516597462716,
0.77110541270397041,
0.73841307296974966,
0.70710678118654752,
0.67712777346844637,
0.64841977732550483,
0.62092890603674203,
0.59460355750136054,
0.56939431737834583,
0.54525386633262883,
0.52213689121370692,
0.50000000000000000
};
static double a2[] = {
0.24114209503420288E-17,
0.92291566937243079E-18,
-0.15241915231122319E-17,
-0.35421849765286817E-17,
-0.31286215245415074E-17,
-0.44654376565694490E-17,
0.29306999570789681E-17,
0.11260851040933474E-17
};
static double p1 = 0.833333333333332114e-1;
static double p2 = 0.125000000005037992e-1;
static double p3 = 0.223214212859242590e-2;
static double p4 = 0.434457756721631196e-3;
static double q1 = 0.693147180559945296e0;
static double q2 = 0.240226506959095371e0;
static double q3 = 0.555041086640855953e-1;
static double q4 = 0.961812905951724170e-2;
static double q5 = 0.133335413135857847e-2;
static double q6 = 0.154002904409897646e-3;
static double q7 = 0.149288526805956082e-4;
static double k = 0.442695040888963407;
double pow(x, y)
double x, y;
{
double frexp(), g, ldexp(), r, u1, u2, v, w, w1, w2, y1, y2, z;
int iw1, m, p;
if (y == 0.0)
return(1.0);
if (x <= 0.0) {
if (x == 0.0) {
if (y > 0.0)
return(x);
cmemsg(FP_POWO, &y);
return(HUGE);
}
else {
cmemsg(FP_POWN, &x);
x = -x;
}
}
g = frexp(x, &m);
p = 0;
if (g <= a1[8])
p = 8;
if (g <= a1[p + 4])
p += 4;
if (g <= a1[p + 2])
p += 2;
p++;
z = ((g - a1[p]) - a2[p / 2]) / (g + a1[p]);
z += z;
v = z * z;
r = (((p4 * v + p3) * v + p2) * v + p1) * v * z;
r += k * r;
u2 = (r + z * k) + z;
u1 = 0.0625 * (double)(16 * m - p);
y1 = 0.0625 * (double)((int)(16.0 * y));
y2 = y - y1;
w = u2 * y + u1 * y2;
w1 = 0.0625 * (double)((int)(16.0 * w));
w2 = w - w1;
w = w1 + u1 * y1;
w1 = 0.0625 * (double)((int)(16.0 * w));
w2 += (w - w1);
w = 0.0625 * (double)((int)(16.0 * w2));
iw1 = 16.0 * (w1 + w);
w2 -= w;
while (w2 > 0.0) {
iw1++;
w2 -= 0.0625;
}
if (iw1 > MAXEXP) {
cmemsg(FP_POWO, &y);
return(HUGE);
}
if (iw1 < MINEXP) {
cmemsg(FP_POWU, &y);
return(0.0);
}
m = iw1 / 16;
if (iw1 >= 0)
m++;
p = 16 * m - iw1;
z = ((((((q7*w2 + q6)*w2 + q5)*w2 + q4)*w2 + q3)*w2 + q2)*w2 + q1)*w2;
z = a1[p] + a1[p] * z;
return(ldexp(z, m));
}
You have all sorts of ints in there. When you do that, the decimal gets truncated. You should make your power functions return floats, and use a float base.

Rotating a 2d array -> reversing a loop

I use the following code to rotate a bitmask (a packed 2d array). To be honest I do not have a firm grip of the algorithm used, I copied and modified the rotation code from the pygame library (where it was used to rotate surfaces). Due to the implementation of bitmask I can speed this rotation up a lot by reversing the inner loop. With that I mean, instead of doing foreach y { foreach x { ... } } I need to do foreach x { foreach y { ... } }. I have trouble reversing the loop because the trigonometry has to be adapted in a way I don't currently see at this moment.
Here's the code:
typedef struct bitmask {
int w,h;
BITMASK_W bits[1];
} bitmask_t;
bitmask_t* bitmask_rotate(const bitmask_t *mask, float angle) {
bitmask_t *newmask = NULL;
double radangle, sangle, cangle;
int isin, icos;
double cx, cy, sx, sy;
int x, y, ax, ay, xd, yd, dx, dy;
int nxmax, nymax, xmaxval, ymaxval;
radangle = angle * DEG_TO_RAD;
sangle = sin(radangle);
cangle = cos(radangle);
isin = (int)(sangle * 65536);
icos = (int)(cangle * 65536);
x = mask->w;
y = mask->h;
cx = cangle*x;
cy = cangle*y;
sx = sangle*x;
sy = sangle*y;
nxmax = (int) (MAX (MAX (MAX (fabs (cx + sy), fabs (cx - sy)), fabs (-cx + sy)), fabs (-cx - sy)));
nymax = (int) (MAX (MAX (MAX (fabs (sx + cy), fabs (sx - cy)), fabs (-sx + cy)), fabs (-sx - cy)));
newmask = bitmask_create(nxmax, nymax, 0);
if (!newmask) return NULL;
cy = newmask->h / 2;
xd = ((mask->w - newmask->w) << 15);
yd = ((mask->h - newmask->h) << 15);
ax = ((newmask->w) << 15) - (int)(cangle * ((newmask->w - 1) << 15));
ay = ((newmask->h) << 15) - (int)(sangle * ((newmask->w - 1) << 15));
xmaxval = ((mask->w) << 16) - 1;
ymaxval = ((mask->h) << 16) - 1;
for (y = 0; y < newmask->h; y++) {
dx = (ax + (isin * (cy - y))) + xd;
dy = (ay - (icos * (cy - y))) + yd;
for (x = 0; x < newmask->w; x++) {
if (!(dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)) {
if (bitmask_getbit(mask, dx >> 16, dy >> 16)) {
bitmask_setbit(newmask, x, y);
}
}
dx += icos;
dy += isin;
}
}
return newmask;
}
Before people are going to ask "what have you tried?", I looped up rotating matrices on Wikipedia, and I could see what is going on there and how they implemented it in this algorithm (precalculate a starting dx and dy and then increment with icos and isin), but the bitshifts and parameters I don't understand (ax for example) make it hard for me to follow.
for (x = 0; x < newmask->w; x++) {
dx = (ax + (isin * cy + icos * x)) + xd;
dy = (ay - (icos * cy - isin * x)) + yd;
for (y = 0; y < newmask->h; y++) {
if (!(dx < 0 || dy < 0 || dx > xmaxval || dy > ymaxval)) {
if (bitmask_getbit(mask, dx >> 16, dy >> 16)) {
bitmask_setbit(newmask, x, y);
}
}
dx -= isin;
dy += icos;
}
}

Resources