Archive

Archive for the ‘C/C++’ Category

New eXtend Standard Library 1.6.3

February 4th, 2012 Rashaud Teague No comments

Removed the lists.c functions and the data types that belonged. Project Page

Categories: C/C++, Linux, UNIX Tags:

eXtended Standard Library final big release.

January 27th, 2012 Rashaud Teague No comments

1.6.x is the last big release of this library. Documentation is updated and ongoing. Future releases will be bug fixes and minor enhancements only.

View the project page.

Categories: C/C++, Development Tools, Linux, Mac OS, UNIX Tags:

eXtend C Standard Library Overhaul

January 26th, 2012 Rashaud Teague No comments

The upcoming 1.6 and final branch of the 1.x versions will a change in the way functions generated new data within them. No more memory allocations and returning with the newly allocated memory.

For example:

char * substr(const char *str, unsigned int start, unsigned int length) {
    int str_size = (int) strlen(str);
   
    if (start >= str_size) return (char *)str;
    if (start + length >= str_size) length = str_size - start;
   
    static char *rstr;
    if (rstr != NULL) free(rstr);
   
    if (length > 0) {
        rstr = (char *) malloc(length * sizeof(char));
        int i, j;
        for (i = start, j = 0; j < length; i++, j++) rstr[j] = str[i];
        rstr[length] = '\0';
    } else {
        rstr = (char *) malloc(1 * sizeof(char));
        strcpy(rstr, "");
    }
   
    return rstr;
}

Will be rewritten as:

char * substr(const char *str, unsigned int start, unsigned int length, char *buffer) {
    int str_size = (int) strlen(str);
   
    if (start >= str_size) return (char *)str;
    if (start + length >= str_size) length = str_size - start;
   
    if (length > 0) {
        int i, j;
        for (i = start, j = 0; j < length; i++, j++) buffer[j] = str[i];
        buffer[length] = '\0';
    } else {
        strcpy(buffer, "");
    }
   
    return buffer;
}

And its usage, in short:

char search[delimiter_size];

substr("Hello World", 0, 5, search);

printf("%s\n", search);
Categories: C/C++, Development Tools, Linux, Mac OS, UNIX Tags:

eXtended Standard Library Update

January 15th, 2012 Rashaud Teague No comments

Had to make a quick change to the broken get_slope function. Please download version 1.1.1 ASAP.

View the wiki page for it’s new documentation.

Categories: C/C++ Tags:

xround, dec2frac and reduce_frac updates!!!

January 7th, 2012 Rashaud Teague No comments

I fixed all the broken shit in these functions!!! And they are now available for use in the new xstdlib 1.0.8

/********************************************************************
 * Name: dectest.c
 * Author: rashaudteague
 * Date: 01/06/2012
 * License: GNU GPL <http://www.gnu.org/licenses/>
 * Description: <description>
 ********************************************************************/


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

float xround(const float n, int precision) {
    return floor(n * pow(10, precision) + 0.5) / pow(10, precision);
}

void dec2frac(float decimal, float *le_fraction) {
    float mixed        = floor(decimal);
    if (decimal < 0) mixed = ceil(decimal);
    if (mixed == -0) mixed = 0;
   
    float denominator  = 100.0;
    float numerator    = xround(decimal - mixed, 2) * denominator;

    if (mixed <= -1) numerator *= -1;
   
    le_fraction[0]     = mixed;
    le_fraction[1]     = numerator;
    le_fraction[2]     = denominator;
}

void reduce_frac(float *le_fraction, float *reduce) {
    int i;
    int gcd = 1;
    int increment = ((int)le_fraction[1] % 2 == 0) ? 1 : 2;

    int num = (int)le_fraction[1];
   
    if (le_fraction[1] < 0) num *= -1;
   
    for (i = 1; i <= num; i += increment) {
        if (num % i == 0 && (int)le_fraction[2] % i == 0) {
            if (i > gcd) gcd = i;
        }
    }
   
    reduce[0] = le_fraction[0];
    reduce[1] = le_fraction[1] / (float)gcd;
    reduce[2] = le_fraction[2] / (float)gcd;
}

int main(void) {
    float n = 0.5;
   
    float fraction[3];
    float reduced[3];

    dec2frac(n, fraction);
   
    printf("%.4f fractioned, mixed = %.1f, numerator = %.1f, denominator = %.1f\n", n, fraction[0], fraction[1], fraction[2]);

    reduce_frac(fraction, reduced);

    printf("The above reduced, mixed = %.1f, numerator = %.1f, denominator = %.1f\n", reduced[0], reduced[1], reduced[2]);
   
    return 0;
}
Categories: C/C++ Tags:

midpoint function change

January 6th, 2012 Rashaud Teague No comments

I’ve made an update for the midpoint function change. I was foolishly creating a memory leak.

Then:

float * mid_point(float *p1, float *p2) {
    float *m = (float *) malloc(2 * sizeof(float));
    m[0] = (p1[0] + p2[0]) / 2;
    m[1] = (p1[1] + p2[1]) / 2;
    return m;
}

Now (with example usage):

float * mid_point(float *p1, float *p2, float *new_point) {
    new_point[0] = (p1[0] + p2[0]) / 2;
    new_point[1] = (p1[1] + p2[1]) / 2;

    return new_point;
}

int main(void) {

    float a[] = {2, 4};
    float b[] = {5, 8};

    float mid[2];

    mid_point(a, b, mid);

    printf("(%.2f, %.2f)\n", mid[0], mid[1]);
   
    return 0;
}

The update will be added to the eXtended C Standard Library.

Cheers, bitches!

Categories: C/C++ Tags:

Dec-Fracing, dec2frac.

January 4th, 2012 Rashaud Teague No comments

Pronounced as “Deck Fracking”.

Convert a decimal to a simple mixed fraction (i.e. 1.50 to 1 1/2), in C programming.

The function we are looking at:

void dec2frac(float decimal, float *le_fraction);

Yes, the second argument is “Le fraction” to make it sound French…

/********************************************************************
 * Name: dec2frac.c
 * Author: rashaudteague
 * Date: 01/03/2012
 * License: GNU GPL <http://www.gnu.org/licenses/>
 * Description: <description>
 ********************************************************************/


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

float xround(const float n, int precision) {
    char precision_to_string[2];
    sprintf(precision_to_string, "%d", precision);

    char precision_format[6] = "%.";
    strcat(precision_format, precision_to_string);
    strcat(precision_format, "f");
   
    char s[32];
    sprintf(s, precision_format, n);

    return atof(s);
}

void dec2frac(float decimal, float *le_fraction) {
    float mixed        = floor(decimal);
    float denominator  = 100.0;
    float numerator    = xround(decimal - mixed, 2) * denominator;
    le_fraction[0]     = mixed;
    le_fraction[1]     = numerator;
    le_fraction[2]     = denominator;
}

int main(void) {

    float i = 1.6689;

    float fraction[3];

    dec2frac(i, fraction);
   
    printf("%.4f fractioned, mixed = %.1f, numerator = %.1f, denominator = %.1f\n", i, fraction[0], fraction[1], fraction[2]);
    printf("* The rest is up to YOU to reduce! *\n");
   
    return 0;
}
Categories: C/C++ Tags:

Rounding with precision in C

January 3rd, 2012 Rashaud Teague No comments

Splash!

Introducing:

float xround(const float n, int precision);

We all know what argument const float n is, it is the number we need to round. What about the other effin argument? Well int precision is how many decimal places you want to round. If precision is 0, let us say 0.56, would be rounded to 1.0 or if the precision is set to 1, 0.56 will be rounded to 0.60.

An example of how to use this function:

/********************************************************************
 * Name: round.c
 * Author: rashaudteague
 * Date: 01/02/2012
 * License: GNU GPL <http://www.gnu.org/licenses/>
 * Description: <description>
 ********************************************************************/


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

float xround(const float n, int precision) {
    char precision_to_string[2];
    sprintf(precision_to_string, "%d", precision);

    char precision_format[6] = "%.";
    strcat(precision_format, precision_to_string);
    strcat(precision_format, "f");
   
    char s[32];
    sprintf(s, precision_format, n);

    return atof(s);
}

int main(void) {
    float i = 0.56;

    float rounded = xround(i, 1);

    printf("%.2f rounded = %.2f\n", i, rounded);
   
    return 0;
}
Categories: C/C++ Tags:

C Programming Language standard update (ISO)

December 24th, 2011 Rashaud Teague No comments
Categories: C/C++ Tags:

basename example? no?

December 8th, 2011 Rashaud Teague No comments
/********************************************************************
 * Name: basename.c
 * Author: rashaudteague
 * Date: 12/08/2011
 * License: GNU GPL <http://www.gnu.org/licenses/>
 * Description: <description>
 ********************************************************************/


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

char * basename(const char *path, char *buffer) {
    size_t path_size = strlen(path);
   
    int i, j;
    int slash_pos = -1;
    int substr_length = 0;
   
    for (i = path_size; i >= 0; i--) if (path[i] == '/') { slash_pos = i; break; }
    if (slash_pos == -1) { strcpy(buffer, path); return buffer; }

    substr_length = path_size - slash_pos;
   
    strncpy(buffer, path + slash_pos + 1, substr_length);

    return buffer;
}

int main(void) {
    char buffer[64];
    printf("%s\n", basename("/home/rashaud/test.c", buffer));
   
    return 0;
}
Categories: C/C++, Linux Tags: