libamos / math.c

/*
    Name:                string.c
    Purpose:             string operation rutines
    Author:              Kevin Veroneau __chronoboy__
    Original Author:     Viola Roberto __vroby__
    Copyright:           (c) 2003 Viola Roberto <__vroby__@libero.it>
                         (c) 2011 Kevin Veroneau <__kveroneau__@gmail.com>
    Licence:             LGPL
*/
//_______________________________________________________________________________________________________________________

//header

#include <math.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include "libamos.h"


/* bin: returns binary representation of number */
char *bin(int i)
{
    int l;
    char    *buffer ,*ret;

    buffer = (char *)malloc(1024);
    strcpy(buffer,"");

    if (i==0)strcat(buffer,"0");

    while(i>0){
	if ((i%2)==0)
	    strcat(buffer,"0");
	else
	    strcat(buffer,"1");
	i=i /2;
    }
    l=strlen(buffer);

    ret = (char *)malloc(l+1);
    for (i=0;i<l;i++)
	ret[i]=buffer[l-i-1];
    ret[l]='\0';
    free(buffer);
    return ret;
}
//_______________________________________________________________________________________________________________________

/* hex: returns hexidecimal representation of number */
char *hex(int i)
{
    char *buffer;

    buffer = (char *)malloc(16);
    sprintf( buffer, "%x", i);
    return buffer;
}
//_______________________________________________________________________________________________________________________

/* randomize: reseed the random number generator */
void randomize(int seed)
{
    if (seed<0)
        seed = time(NULL);
    srand( seed );
}
//_______________________________________________________________________________________________________________________

/* rnd: return an random number */
double rnd(int upper)
{
    int result;

    result = rand();
    if (upper>0) {
        result = (result % (upper-1))+1;
    }
    return( (float)result );
}
//_______________________________________________________________________________________________________________________

/* round: round to nearest integer */
double round(double n1)
{
    double n2;
    n2 = floor(n1);
    if (n2 == 0) {
        /* leave it alone */

    } else if (n2 > 0) {
        if ((n1 - n2) > 0.5) {
            n2++;
        }
    } else {
        if (-(n2 - n1) > 0.5) {
            n2++;
        }
    }
    return( n2 );
}
//_______________________________________________________________________________________________________________________

/* sgn: returns sign of numeric expression */
int sgn(double  n)
{
    if (n < 0) {
        return(-1);
    } else if (n > 0) {
        return(1);
    } else {
        return(0);
    }
}
//_______________________________________________________________________________________________________________________

/* min: return min number of the operands */
double Min(double a, double b)
{
    if (b>a)
	return a;
    else
	return b;
}
//_______________________________________________________________________________________________________________________

/* max: return max number of the operands */
double Max(double a, double b)
{
    if (b<a)
	return a;
    else
	return b;
}
//_______________________________________________________________________________________________________________________

/* bitwiseand: return the logic and of the operands */
int bitwiseand(int a, int b)
{
    return(a & b);
}
//_______________________________________________________________________________________________________________________

/* bitwiseor: return the logic or of the operands */
int bitwiseor(int a, int b)
{
    return(a | b);
}
//_______________________________________________________________________________________________________________________

/* bitwisexor: return the logic xor of the operands */
int bitwisexor(int a, int b)
{
    return(a ^ b);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.