/*
P4Math functions for use in encryption ect...
by P4ll4t0n
*/
//-------------------------------------------------------------------------------------
// BEGIN P4MATH.h
//-------------------------------------------------------------------------------------
#define P4MAX( a, b ) ( a > b ? a : b )
#define P4MIN( a, b ) ( a < b ? a : b )
#define P4ABS( a ) ( a < 0 ? -1 * a : a )
#define P4FLOOR( a ) ( (int)( a - 0.5) )
#define P4CEIL( a ) ( (int)( a + 0.5 ) )
//Gets the Greatest common divisor of a and b
int P4Gcf( long int a, long int b );
//Gets the factorial of a number
long int P4Factorial( long int a );
//Simple does a exponents only does whole number exponents
long int P4Pow( int n, int e );
//Aprox's the sqrt of a number, the > iters is the more acurate the sqrt is
float P4AproxSqrt( float num, int iters );
//------END P4Math.h
//------------------------------------------------------------------------
// Start P4Math.c
//------------------------------------------------------------------------
#include "./P4Math.h"
int P4Gcf( long int a, long int b )
{
int max = P4MAX( a, b );
int min = P4MIN( a, b );
int remain;
while( min != 0 )
{
remain = max % min;
max = min;
min = remain;
}
return max;
}
long int P4Factorial( long int a )
{
if( a == 1 || a == -1 )
return 1;
if( a < 0 )
return a * P4Factorial( a + 1 );
return a * P4Factorial( a - 1 );
}
long int P4Pow( int n, int e )
{
if( e <= 0 )
return 1;
if( e == 1 )
return n;
return n * P4Pow( n, e - 1 );
}
float P4AproxSqrt( float num, int iters )
{
if( num == 0 || num < 0 )
return 0;
float sqrt, q = num / 2;
for( int i = 0; i < iters; i++ )
{
sqrt = ( q + num / q ) / 2;
q = sqrt;
}
return sqrt;
}