//LogFloat.c //Logs max, min and precision of a floating point value #include #include "LogFloat.h" struct LogFloatEntry { char id[128]; float min, max, precision; int counter; }; #define MAX_LF_ENTRIES 1000 struct LogFloatEntry lf_entries[MAX_LF_ENTRIES]; int lf_total=0; _inline struct LogFloatEntry *_lfAdd(char *id) { //Adds a new entry. //NULL == Array full. if (lf_total==MAX_LF_ENTRIES) return NULL; strcpy(lf_entries[lf_total].id, id); lf_entries[lf_total].max=lf_entries[lf_total].min=0.0f; lf_entries[lf_total].precision=1.0f; lf_entries[lf_total].counter=0; return &lf_entries[lf_total++]; } _inline struct LogFloatEntry *_lfFind(char *id) { //Finds an existing entry. //NULL == No matching id struct LogFloatEntry *cur=&lf_entries[0], *last=&lf_entries[lf_total]; while (cur<=last) { //Change this is case insensitive matching is desired. if (!strcmp(id, cur->id)) return cur; cur++; } return NULL; } _inline struct LogFloatEntry *_lfGet(char *id) { //Gets an entry. Returns an entry if it already exists, otherwise a new entry is created. struct LogFloatEntry *cur=_lfFind(id); if (!cur) cur=_lfAdd(id); return cur; } int LogFloatInit() { lf_total=0; return 1; } void LogFloatClose() { lf_total=0; } void LogFloatResults(char *fn) { int i; FILE *f=fopen(fn, "w+"); if (f) { fprintf(f, "ID\tMin\tMax\tPrecision\tCount\n\n"); for (i=0; i=0 && <= 32, return value is f truncated to the fixed point precision "precision". //otherwise return value == f. struct LogFloatEntry *cur; float tmp; if (index>=0) { char newId[256]; sprintf(newId, "%s[%d]", id, index); cur=_lfGet(newId); } else cur=_lfGet(id); if (cur) { cur->counter++; if (cur->maxmax=f; if (cur->min>f) cur->min=f; tmp=(float)fabs(f); if (tmp<1.0f&&f!=0.0f) if (tmpprecision) cur->precision=tmp; } //else Error: we ran out of entry storage if (precision>=0) { float f2=(float)(2^precision); tmp=f*f2; return tmp/f2; } return f; }