diff options
author | Joshua Haberman <joshua@reverberate.org> | 2009-07-08 12:06:47 -0700 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2009-07-08 12:06:47 -0700 |
commit | 462b26c1cc041a8fa26deb62cf12f1f351a5b2f6 (patch) | |
tree | de5a58f8d66d11c13b349448a970f84d57d16cad /tests/test_util.h | |
parent | c7ee14f8ef38a8bc90c0f1db1ad47b2e06612fa3 (diff) |
Directory restructuring.
Diffstat (limited to 'tests/test_util.h')
-rw-r--r-- | tests/test_util.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_util.h b/tests/test_util.h new file mode 100644 index 0000000..a998a19 --- /dev/null +++ b/tests/test_util.h @@ -0,0 +1,53 @@ +/* Function for printing numbers using si prefixes (k, M, G, etc.). + * From http://www.cs.tut.fi/~jkorpela/c/eng.html */ + +#define PREFIX_START (-24) +/* Smallest power of then for which there is a prefix defined. + If the set of prefixes will be extended, change this constant + and update the table "prefix". */ + +#include <stdio.h> +#include <math.h> + +static char *eng(double value, int digits, int numeric) +{ + static const char *prefix[] = { + "y", "z", "a", "f", "p", "n", "u", "m", "", + "k", "M", "G", "T", "P", "E", "Z", "Y" + }; +#define PREFIX_END (PREFIX_START+\ +(int)((sizeof(prefix)/sizeof(char *)-1)*3)) + + int expof10; + static char result[100]; + char *res = result; + + if (value < 0.) + { + *res++ = '-'; + value = -value; + } + + expof10 = (int) log10(value); + if(expof10 > 0) + expof10 = (expof10/3)*3; + else + expof10 = (-expof10+3)/3*(-3); + + value *= pow(10,-expof10); + + if (value >= 1000.) + { value /= 1000.0; expof10 += 3; } + else if(value >= 100.0) + digits -= 2; + else if(value >= 10.0) + digits -= 1; + + if(numeric || (expof10 < PREFIX_START) || + (expof10 > PREFIX_END)) + sprintf(res, "%.*fe%d", digits-1, value, expof10); + else + sprintf(res, "%.*f %s", digits-1, value, + prefix[(expof10-PREFIX_START)/3]); + return result; +} |