#include #include #include #include #include "fixpt.h" char *fp2str(FIXPT_Q_TYPE x, int dec) { char *buf; buf = malloc(FIXPT_MAX_STRLEN+1); fixpt_2str(buf, sizeof buf, x, dec); /* yes, we're leaking memory but it doesn't matter */ return buf; } void dump_fp(FIXPT_Q_TYPE x) { int i; unsigned char *b = (unsigned char*)&x; printf("%11jd : ", x); for (i = 0; i < sizeof x; i++) printf("%2x ", *b++); printf("\n"); } int main() { FIXPT_Q_TYPE a, b, c, t; printf("Library configured as Q%d.%d\n", sizeof(FIXPT_Q_TYPE)*8 - (2 * FIXPT_Q_BITS) - FIXPT_Q_SIGN_BITS, FIXPT_Q_BITS); a = make_fixpt(1, 0); dump_fp(a); b = make_fixpt(10, 0); dump_fp(b); c = fixpt_div(a, b); printf("%s / %s = %s\n", fp2str(a, 2), fp2str(b, 2), fp2str(c, 2)); dump_fp(c); a = make_fixpt(2, 0); c = fixpt_div(a, b); printf("%s / %s = %s\n", fp2str(a, 2), fp2str(b, 2), fp2str(c, 2)); dump_fp(c); t = c; c = fixpt_mul(c, b); printf("%s * %s = %s\n", fp2str(t, 2), fp2str(b, 2), fp2str(c, 2)); dump_fp(c); a = make_fixpt(50, 0); b = make_fixpt(60, 0); c = fixpt_div(a, b); printf("%s / %s = %s\n", fp2str(a, 2), fp2str(b, 2), fp2str(c, 2)); dump_fp(c); t = c; b = make_fixpt(100, 0); c = fixpt_mul(c, b); printf("%s * %s = %s\n", fp2str(t, 2), fp2str(b, 2), fp2str(c, 2)); dump_fp(c); t = c; c = fixpt_sqrt(b); printf("sqrt(%s) = %s\n", fp2str(b, 2), fp2str(c, 2)); a = fixpt_add(t, c); c = fixpt_sqrt(a); printf("sqrt(%s) = %s\n", fp2str(a, 2), fp2str(c, 2)); t = c; c = fixpt_intpow(c, 2); printf("pow(%s, 2) = %s\n", fp2str(t, 2), fp2str(c, 2)); return 0; }