Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :Fast logarithmic in
// teger scaling function
//**************************************
//
/* +++Date last modified: 05-Jul-1997 */
/*
** SNIPMATH.H - Header file for SNIPPETS math functions and macros
*/
#ifndef SNIPMATH__H
#define SNIPMATH__H
#include
#include "sniptype.h"
#include "round.h"
/*
** Callable library functions begin here
*/
voidSetBCDLen(int n); /* Bcdl.C */
longBCDtoLong(char *BCDNum); /* Bcdl.C */
voidLongtoBCD(long num, char BCDNum[]);/* Bcdl.C */
double bcd_to_double(void *buf, size_t len, /* Bcdd.C */
int digits);
int double_to_bcd(double arg, char *buf, /* Bcdd.C */
size_t length, size_t digits );
DWORDncomb1 (int n, int m);/* Combin.C*/
DWORDncomb2 (int n, int m);/* Combin.C*/
voidSolveCubic(double a, double b, double c, /* Cubic.C*/
double d, int *solutions,
double *x);
DWORDdbl2ulong(double t); /* Dbl2Long.C */
longdbl2long(double t);/* Dbl2Long.C */
double dround(double x); /* Dblround.C */
/* Use #defines for Permutations and Combinations -- Factoryl.C */
#define log10P(n,r) (log10factorial(n)-log10factorial((n)-(r)))
#define log10C(n,r) (log10P((n),(r))-log10factorial(r))
double log10factorial(double N); /* Factoryl.C */
double fibo(unsigned short term);/* Fibo.C */
double frandom(int n);/* Frand.C*/
double ipow(double x, int n);/* Ipow.C */
int ispow2(int x);/* Ispow2.C*/
longdouble ldfloor(long double a);/* Ldfloor.C */
int initlogscale(long dmax, long rmax);/* Logscale.C */
longlogscale(long d); /* Logscale.C */
floatMSBINToIEEE(float f); /* Msb2Ieee.C */
floatIEEEToMSBIN(float f); /* Msb2Ieee.C */
int perm_index (char pit[], int size);/* Perm_Idx.C */
int round_div(int n, int d); /* Rnd_Div.C */
longround_ldiv(long n, long d);/* Rnd_Div.C */
double rad2deg(double rad); /* Rad2Deg.C */
double deg2rad(double deg); /* Rad2Deg.C */
#include "pi.h"
#ifndef PHI
#define PHI ((1.0+sqrt(5.0))/2.0) /* the golden number*/
#define INV_PHI (1.0/PHI) /* the golden ratio */
#endif
/*
** File: ISQRT.C
*/
struct int_sqrt {
unsigned sqrt,
frac;
};
void usqrt(unsigned long x, struct int_sqrt *q);
#endif /* SNIPMATH__H */
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
/
//
/* +++Date last modified: 05-Jul-1997 */
/*
** L O G S C A L E
**
** Logarithmically scale from long integer to long integer. The
** function domain (input) & range (output) both start from 0
** and the range MUST be smaller than the domain.
**
** this scaling function was especially designed to colour fractals.
** In order to preserve detail & to ensure that no colours are wasted,
** the function is almost linear with a slope near 1 for early values.
**
** Written by M. Stapleton of Graphic Bits
**
** public domain
**
** May 18 1997
*/
#include "snipmath.h"
/* "Shifted" logarithm = log(u+v) - log(v) */
#define shlog(u,v) (log(((double)(u) + (double)(v)) / (double)(v)))
static double alpha;/* Scaling parameter */
/*
** Calculate scaling parameter (alpha) using Newton's method,
** where rmax = alpha * shlog(dmax, alpha)
** Boolean return: 0 on failure
*/
int initlogscale(long dmax, long rmax)
{
double dm = (double) dmax;
double rm = (double) rmax;
double x = 1.0, dx, y, yy, t;
if (dm <= rm)
return 0;
do
{
t = shlog(dm, x);
y = x * t - rm;
yy = t - dm / (x + dm);
dx = y / yy;
x -= dx;
/* printf("Alpha = %f\n", x); */
} while (abs(dx) > 1E-4);
alpha = x;
return 1;
}
/*
** Logarithmically scale d, offset by alpha
*/
long logscale(long d)
{
double r = alpha * shlog(d, alpha);
return (long) floor(0.5 + r);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef TEST
#include
#include
/*
** default domain & range maxima.
*/
#define DMAX 1000
#define RMAX 255
/*
** Test logscale
*/
int main(int argc, char *argv[])
{
long d, dmax = 0, r0, r1, rmax = 0;
if (argc == 2 && *argv[1] == '?')
{
printf("Test logscale()\n Usage:\n");
printf("%s [domain] [range]\n", argv[0]);
return EXIT_FAILURE;
}
/*
** Get domain & range maxima
*/
if (argc > 1)
dmax = atol(argv[1]);
if (dmax == 0)
dmax = DMAX;
if (argc > 2)
rmax = atol(argv[2]);
if (rmax == 0)
rmax = RMAX;
if (rmax >= dmax)
{
printf("Warning: range must be smaller than domain!\n");
/*
** A real program we would exit here, but we go on to test
** initlogscale's error handling...
*/
}
/*
** Initialise scaling parameter
*/
if (!initlogscale(dmax, rmax))
{
printf("Error: cannot initialise logscale!\n");
return EXIT_FAILURE;
}
/*
** Calculate function for every domain value.
** Only print where the range value changes.
*/
for (r0 = -1, d = 0; d < dmax; d++)
{
r1 = logscale(d);
if (r1 != r0)
{
printf("%ld -> %ld\n", d, r1);
r0 = r1;
}
}
printf("%ld -> %ld\n", d, r1);
return EXIT_SUCCESS;
}
#endif /* TEST */