/*
 * FreeAdhocUDF - copyright (c) 2004 - 2006 adhoc dataservice GmbH
 * Christoph Theuring / Peter Mandrella / Georg Horn
 * based on FreeUDFLibC - copyright (c) 1999 Gregory Deatz
 *
 * File Name:	math_functions.c
 * Description: this module contains UDFs to generate and manipulate math functions
 *
 * for exported functions list see math_functions.h
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
*****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef __BORLANDC__
#include <process.h>
#else
pid_t getpid(void);
#endif

#include "global.h"
#include "math_functions.h"


// ct 2006-11-26 implementation of NULL for FB 2.0
char *converttodollar(double *value)
{
    if (!value)
	return NULL;
    else {
	char *sz_result = (char *) MALLOC(LOC_STRING_SIZE + 1);
	sprintf(sz_result, "$%.2f", (*value));
	return sz_result;
    }
}


// ct 2006-11-26 implementation of NULL for FB 2.0
double *doubleabs(double *value)
{
    if (!value)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*value < 0)
	    *result = -*value;
	else
	    *result = *value;
	return result;
    }
}


// ct 2006-11-26 implementation of NULL for FB 2.0
char *fixedpoint(double *value, int *decimals)
{
    if (!value || !decimals)
	return NULL;
    else {
	char *sz_result = MALLOC(LOC_STRING_SIZE + 1);
	sprintf(sz_result, "%.*f", *decimals, *value);
	return sz_result;
    }
}


// pm 21.08.04
// ct 2006-11-26 implementation of NULL for FB 2.0
char *fixedpointlang(double *value, int *nk, char *comma, char *tsep)
{
    if (!value || !nk || !comma || !tsep)
	return NULL;
    else {
	char buffer[128];
	double val = *value;
	char *sz_result = MALLOC(LOC_STRING_SIZE + 1);
	char *resp = sz_result;
	char *pc;
	int i, bl;

	// Vorkommastellen
	if (*nk <= 0)
	    val += 0.5;
	bl = sprintf(buffer, "%.0f", floor(val));
	for (i = 0; i < bl; i++) {
	    *resp++ = buffer[i];
	    if (*tsep && i > 0 && i < bl - 1 && ((bl - 1 - i) % 3) == 0) {
		// Tausender-Separator
		pc = tsep;
		while (*pc)
		    *resp++ = *pc++;
	    }
	}

	if (*nk > 0) {
	    // Dezimalpunkt
	    pc = comma;
	    while (*pc) {
		*resp = *pc;
		resp++, pc++;
	    }

	    // Nachkommastellen
	    for (i = *nk; i > 1; i--) {
		val = 10 * (val - floor(val));
		*resp = (char) (floor(val) + '0');
		resp++;
	    }
	    *resp = (char) (floor(10 * (val - floor(val)) + 0.5) + '0');
	    resp++;
	}
	// terminate
	*resp = '\0';
	return sz_result;
    }
}


// ct 2006-11-26 implementation of NULL for FB 2.0
int *integerabs(int *value)
{
    if (!value)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	if (*value < 0)
	    *result = -*value;
	else
	    *result = *value;
	return result;
    }
}


// pm 21.08.04
// ct 2006-11-26 implementation of NULL for FB 2.0
double *maxnum(double *d1, double *d2)
{
    if (!d1 || !d2)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*d1 >= *d2)
	    *result = *d1;
	else
	    *result = *d2;
	return result;
    }
}


// pm 21.08.04
// ct 2006-11-26 implementation of NULL for FB 2.0
double *minnum(double *d1, double *d2)
{
    if (!d1 || !d2)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*d1 <= *d2)
	    *result = *d1;
	else
	    *result = *d2;
	return result;
    }
}


// ct 2006-11-26 implementation of NULL for FB 2.0
int *modulo(int *numerator, int *denominator)
{
    if (!numerator || !denominator)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	if (*denominator == 0)
	    *result = 0;
	else
	    *result = *numerator % *denominator;
	return result;
    }
}


// pm 21.08.04
// ct 2006-11-26 implementation of NULL for FB 2.0
int *isdivisibleby(int *numerator, int *denominator)
{
    if (!numerator || !denominator)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	if (*denominator == 0)
	    *result = 0;
	else
	    *result = ((*numerator % *denominator) == 0);
	return result;
    }
}


// ct 2006-11-14
// ct 2006-11-26 implementation of NULL for FB 2.0
int *evenodd(int *numerator)
{
    if (!numerator)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = ((*numerator % 2) == 0);
	return result;
    }
}


// ct 2006-11-26 implementation of NULL for FB 2.0
// ct 2006-12-15 changed name from round to f_round because of function name in c
int *f_round(double *value)
{
    if (!value)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	if (*value < 0)
	    *result = (int) (*value - 0.5);
	else
	    *result = (int) (*value + 0.5);
	return result;
    }                  
}


// ct 2006-11-26 implementation of NULL for FB 2.0
// ct 2006-12-15 changed name from truncate to f_truncate because of function name in c
int * f_truncate(double *value)
{
    if (!value)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (int) (*value);
	return result;
    }
}


// pm 21.08.04
// ct 2006-11-26 implementation of NULL for FB 2.0
double *fact(int *n)
{
    if (!n)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*n < 1 || *n > 100)
	    *result = 0.0;
	else {
	    int i;
	    double f = 1.0;
	    for (i = 2; i <= *n; i++)
		f *= i;
	    *result = f;
	}
	return result;
    }
}


// pm 03.10.05
// ct 2006-11-26 implementation of NULL for FB 2.0
int *equalfloat(double *d1, double *d2)
{
    if (!d1 || !d2)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*d1 == *d2);
	return result;
    }
}


// pm 03.10.05
// ct 2006-11-26 implementation of NULL for FB 2.0
int *equalinteger(int *i1, int *i2)
{
    if (!i1 || !i2)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*i1 == *i2);
	return result;
    }
}


#ifndef __BORLANDC__
double trunc (double x);
#endif

// pm 03.10.05
// ct 2006-11-26 implementation of NULL for FB 2.0
// gh 2006-12-21 new code because bug? of floor under linux
double *zahlrunden(double *d1, int *nk)
{
    if (!d1 || !nk) {
	return NULL;
    } else {
	double *result = MALLOC(sizeof(double));
	double res = *d1;
	int n = *nk;
	int sign = (res < 0.0);

	if (sign) {
	    res *= -1.0;
	}
	while (n--) {
	    res *= 10.0;
	}
#ifdef __BORLANDC__
	res = floor(res + 0.5);
#else
	res = trunc(res + 0.5);
#endif
	n = *nk;
	while (n--) {
	    res /= 10.0;
	}
	if (sign) {
	    res *= -1.0;
	}

	*result = res;
	return result;
    }
}


// ct 2006-12-21 similar to zahlrunden and softround from rfunc
double *softround(double *d1, int *nk)
{
    if (!d1 || !nk)
	return NULL;
    else {
   	double *result = MALLOC(sizeof(double));
	double res = *d1;
	int n = *nk;
	int sign = (res < 0.0);

        if(*d1 * pow(10, *nk) >= 1) {
	    if (sign) {
	        res *= -1.0;
	    }
	    while (n--) {
	        res *= 10.0;
	    }
#ifdef __BORLANDC__
	    res = floor(res + 0.5);
#else
	    res = trunc(res + 0.5);
#endif
	    n = *nk;
	    while (n--) {
	        res /= 10.0;
	    }
	    if (sign) {
	        res *= -1.0;
	    }

	    *result = res;
        }
        else {
            *result = *d1;
        }
	return result;
    }
}

// pm 22.10.05
// ct 2006-11-26 implementation of NULL for FB 2.0
double *roundfloat(double *d1, double *d2)
{
    if (!d1 || !d2)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*d2 == 0)
	    *result = 0;
	else
	    *result = floor((*d1 / *d2) + 0.5) * *d2;
	return result;
    }
}


// pm 03.10.05
// ct 2006-11-26 implementation of NULL for FB 2.0
// ct2006-12-10 bug
double *prozente(double *d1, double *d2)
{
    if (!d1 || !d2)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = *d1 * *d2 / 100.0;
	return result;
    }
}


// gh 14.10.06
/* integer random number in range a <= x <= e */
// ct 2006-11-26 implementation of NULL for FB 2.0
int *intrandom(int *a, int *e)
{
    if (!a || !e)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	static time_t t = 0;
	double r = *e - *a + 1;

	if (t == 0)
	    srand((unsigned) time(&t) + getpid());
	*result = *a + (int) (r * rand() / (RAND_MAX + 1.0));
	return result;
    }
}

// ct 2006-12-15
double *doublerandom(int *a, int *e)
{
    if (!a || !e)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	static time_t t = 0;
	double r = *e - *a + 1;

	if (t == 0)
	    srand((unsigned) time(&t) + getpid());
	*result = *a + (double) (r * rand() / (RAND_MAX + 1.0));
	return result;
    }
}

// ct 2006-12-15
double * sin_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = sin(*a);
        return result;
    }
}

// ct 2006-12-15
double * cos_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = cos(*a);
        return result;
    }
}

// ct 2006-12-15
double * tan_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = tan(*a);
        return result;
    }
}

// ct 2006-12-15
double *cot_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = 1.0 / (tan(*a));
        return result;
    }
}

// ct 2006-12-15
double * arcsin_rad(double *a)
{
    if (!a || *a < -1 || *a > 1)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = asin(*a);
        return result;
    }
}

// ct 2006-12-15
double * arccos_rad(double *a)
{
    if (!a || *a < -1 || *a > 1)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = acos(*a);
        return result;
    }
}

// ct 2006-12-15
double * arctan_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = atan(*a);
        return result;
    }
}

// ct 2006-12-15
double * arctan2_rad(double *a, double *b)
{
    if (!a || !b)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = atan2(*a, *b);
        return result;
    }
}

// ct 2006-12-15
double * arccot_rad(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = 1.0 / atan(*a);
        return result;
    }
}

// ct 2006-12-15
double * sin_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = sin(*a / 180.0 * M_PI);
        return result;
    }
}

// ct 2006-12-15
double * cos_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = cos(*a / 180.0 * M_PI);
        return result;
    }
}

// ct 2006-12-15
double * tan_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = tan(*a / 180.0 * M_PI);
        return result;
    }
}

// ct 2006-12-15
double * cot_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = 1.0 / (tan(*a / 180.0 * M_PI));
        return result;
    }
}

// ct 2006-12-15
double * arcsin_deg(double *a)
{
    if (!a || *a < -1 || *a > 1)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = asin(*a) / M_PI * 180.0;
        return result;
    }
}

// ct 2006-12-15
double * arccos_deg(double *a)
{
    if (!a || *a < -1 || *a > 1)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = acos(*a) / M_PI * 180.0;
        return result;
    }
}

// ct 2006-12-15
double * arctan_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = atan(*a) / M_PI * 180.0;
        return result;
    }
}

// ct 2006-12-15
double * arccot_deg(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = (1.0 / atan(*a)) / M_PI * 180.0;
        return result;
    }
}

// ct 2006-12-15
double * sinhyp(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = sinh(*a);
        return result;
    }
}

// ct 2006-12-15
double * coshyp(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = cosh(*a);
        return result;
    }
}

// ct 2006-12-15
double * tanhyp(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = tanh(*a);
        return result;
    }
}

// ct 2006-12-15
double *cothyp(double *a)
{
    if (!a || *a == 0)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = 1.0 / (tanh(*a));
        return result;
    }
}

// ct 2006-12-15
double * arsinh(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = log(*a + sqrt((*a * *a) + 1));
        return result;
    }
}

// ct 2006-12-15
double * arcosh(double *a)
{
    if (!a || *a < 1)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = log(*a + sqrt((*a * *a) - 1));
        return result;
    }
}

// ct 2006-12-15
double * artanh(double *a)
{
    if (!a || *a <= -1 || *a >= 1)
	return NULL;
    else {
        double *result = MALLOC(sizeof(double));
        *result = (log((1 + *a) / (1 - *a))) / 2;
        return result;
    }
}

// ct 2006-12-15
double * arcoth(double *a)
{
    if (!a || (*a >= -1 && *a <= 1))
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));

	*result = (log((*a + 1) / (*a - 1))) / 2;
        return result;
    }
}

// ct 2006-12-15
double * f_rand()
{
    double *result = MALLOC(sizeof(double));
    static time_t t = 0;

    if (t == 0) {
	srand((unsigned) time(&t) + getpid());
    }
    *result = (double) rand() / (double) RAND_MAX;
    return result;
}

// ct 2006-12-15
int * f_sign( double *a)
{
    if (!a)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	if (*a == 0) {
	    *result = 0;
        }
	if (*a > 0) {
	    *result = 1;
        }
	if (*a < 0) {
            *result = -1;
        }
        return result;
    }
}

// ct 2006-12-15
double * f_sqrt( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = sqrt(*a);
        return result;
    }
}

// ct 2006-12-15
double * f_ln( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = log(*a);
        return result;
    }
}

// ct 2006-12-15
double * f_log( double *a, double *b)
{
    if (!a || !b)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = log(*a) / log(*b);
        return result;
    }
}

// ct 2006-12-15
double * f_log10( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = log10(*a);
        return result;
    }
}

// ct 2006-12-15
double * f_floor( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = floor(*a);
        return result;
    }
}

// ct 2006-12-15
double * f_ceiling( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = ceil(*a);
        return result;
    }
}

// ct 2006-12-15
double * f_frac( double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	if (*a > 0)
	    *result = *a - floor(*a);
	if (*a < 0)
	    *result = *a - ceil(*a);
	if (*a == 0)
  	    *result = 0;
        return result;
    }
}

// ct 2006-12-15
int * f_bin_and(int *a, int *b)
{
    if (!a || !b)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*a & *b);
        return result;
    }
}

// ct 2006-12-15
int * f_bin_or(int *a, int *b)
{
    if (!a || !b)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*a | *b);
        return result;
    }
}

// ct 2006-12-15
int * f_bin_xor(int *a, int *b)
{
    if (!a || !b)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*a ^ *b);
        return result;
    }
}

// ct 2006-12-15
int * f_bin_not(int *a)
{
    if (!a)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = ~(*a);
        return result;
    }
}

// ct 2006-12-15
int * f_bin_shl(int *a, int *b)
{
    if (!a || !b)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*a << *b);
        return result;
    }
}

// ct 2006-12-15
int * f_bin_shr(int *a, int *b)
{
    if (!a || !b)
	return NULL;
    else {
	int *result = MALLOC(sizeof(int));
	*result = (*a >> *b);
        return result;
    }
}

// ct 2006-12-15
double * f_power(double *a, double *b)
{
    if (!a || !b)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = pow(*a, *b);
        return result;
    }
}

// ct 2006-12-15
double * f_exp(double *a)
{
    if (!a)
	return NULL;
    else {
	double *result = MALLOC(sizeof(double));
	*result = exp(*a);
        return result;
    }
}
