From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/eng.c | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/eng.c (limited to 'reference/C/CONTRIB/SNIP/eng.c') diff --git a/reference/C/CONTRIB/SNIP/eng.c b/reference/C/CONTRIB/SNIP/eng.c new file mode 100755 index 0000000..ad8cf91 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/eng.c @@ -0,0 +1,50 @@ +/* ENG.C - Format floating point in engineering notation */ +/* Released to public domain by author, David Harmon, Jan. 1994 */ + +#include + +char *eng(double value, int places) +{ + const char * const prefixes[] = { + "a", "f", "p", "n", "æ", "m", "", "k", "M", "G", "T" + }; + int p = 6; + static char result[30]; + char *res = result; + + if (value < 0.) + { + *res++ = '-'; + value = -value; + } + while (value != 0 && value < 1. && p > 0) + { + value *= 1000.; + p--; + } + while (value != 0 && value > 1000. && p < 10 ) + { + value /= 1000.; + p++; + } + if (value > 100.) + places--; + if (value > 10.) + places--; + sprintf(res, "%.*f %s", places-1, value, prefixes[p]); + return result; +} + +#ifdef TEST + +#include + +main() +{ + double w; + + for (w = 1e-19; w < 1e16; w *= 42) + printf(" %g W = %sW\n", w, eng(w, 3)); + return 0; +} +#endif -- cgit v1.2.3-54-g00ecf