diff options
author | Dave Beckett <dave@dajobe.org> | 2011-09-08 20:35:18 -0700 |
---|---|---|
committer | Dave Beckett <dave@dajobe.org> | 2011-09-08 20:35:18 -0700 |
commit | 3fdf2e6eef79b2d87889c8035203faf8ed296761 (patch) | |
tree | 5ed1443d00ab08923ab5de131c22a79a3bcba6b5 /src | |
parent | 18e3ccd3c15da50d2b1df67f3547a83fd5f0531e (diff) | |
download | raptor-3fdf2e6eef79b2d87889c8035203faf8ed296761.tar.gz |
Removed calls to trunc() and lround() and optional linking of libm
(raptor_format_float): Removed unused internal function and thus
removed the only need for trunc() and lround().
configure: Removed checks for trunc(), lround() in libc or libm.
Diffstat (limited to 'src')
-rw-r--r-- | src/raptor_internal.h | 1 | ||||
-rw-r--r-- | src/snprintf.c | 122 |
2 files changed, 0 insertions, 123 deletions
diff --git a/src/raptor_internal.h b/src/raptor_internal.h index b1dd4c60..f8024cb0 100644 --- a/src/raptor_internal.h +++ b/src/raptor_internal.h @@ -1323,7 +1323,6 @@ int raptor_rdfxmla_serialize_set_single_node(raptor_serializer* serializer, rapt int raptor_rdfxmla_serialize_set_write_typed_nodes(raptor_serializer* serializer, int value); /* snprintf.c */ -char* raptor_format_float(char *buffer, size_t *currlen, size_t maxlen, double fvalue, unsigned int min, unsigned int max, int flags); int raptor_format_integer(char* buffer, size_t bufsize, int integer, unsigned int base, int width, char padding); /* raptor_world structure */ diff --git a/src/snprintf.c b/src/snprintf.c index 1d7efcc8..dd5e7697 100644 --- a/src/snprintf.c +++ b/src/snprintf.c @@ -22,9 +22,6 @@ #include <string.h> #include <ctype.h> #include <stdarg.h> -#include <float.h> -#define __USE_ISOC99 1 -#include <math.h> #ifdef HAVE_ERRNO_H #include <errno.h> #endif @@ -37,125 +34,6 @@ -#ifndef HAVE_ROUND -/* round (C99): round x to the nearest integer, away from zero */ -#define round(x) (((x) < 0) ? (long)((x)-0.5) : (long)((x)+0.5)) -#endif - -#ifndef HAVE_TRUNC -/* trunc (C99): round x to the nearest integer, towards zero */ -#define trunc(x) (((x) < 0) ? ceil((x)) : floor((x))) -#endif - -#ifndef HAVE_LROUND -static long -raptor_lround(double d) -{ - /* Add +/- 0.5 then then round towards zero. */ - d = floor(d); - - if(isnan(d) || d > (double)LONG_MAX || d < (double)LONG_MIN) { - errno = ERANGE; - /* Undefined behaviour, so we could return anything. */ - /* return tmp > 0.0 ? LONG_MAX : LONG_MIN; */ - } - return (long)d; -} -#define lround(x) raptor_lround(x) -#endif - - -/* Convert a double to xsd:decimal representation. - * Returned is a pointer to the first character of the number - * in buffer (don't free it). - */ -char* -raptor_format_float(char *buffer, size_t *currlen, size_t maxlen, - double fvalue, unsigned int min, unsigned int max, - int flags) -{ - /* DBL_EPSILON = 52 digits */ - #define FRAC_MAX_LEN 52 - - double ufvalue; - long intpart; - double fracpart = 0; - double frac; - double frac_delta = 10; - double mod_10; - size_t exp_len; - size_t frac_len = 0; - size_t idx; - - if(max < min) - max = min; - - /* index to the last char */ - idx = maxlen - 1; - - buffer[idx--] = '\0'; - - ufvalue = fabs (fvalue); - intpart = lround(ufvalue); - - /* We "cheat" by converting the fractional part to integer by - * multiplying by a factor of 10 - */ - - - frac = (ufvalue - intpart); - - for(exp_len = 0; exp_len <= max; ++exp_len) { - frac *= 10; - - mod_10 = trunc(fmod(trunc(frac), 10)); - - if(fabs(frac_delta - (fracpart / pow(10, exp_len))) < (DBL_EPSILON * 2.0)) { - break; - } - - frac_delta = fracpart / pow(10, exp_len); - - /* Only "append" (numerically) if digit is not a zero */ - if(mod_10 > 0 && mod_10 < 10) { - fracpart = round(frac); - frac_len = exp_len; - } - } - - if(frac_len < min) { - buffer[idx--] = '0'; - } else { - /* Convert/write fractional part (right to left) */ - do { - mod_10 = fmod(trunc(fracpart), 10); - --frac_len; - - buffer[idx--] = "0123456789"[(unsigned)mod_10]; - fracpart /= 10; - - } while(fracpart > 1 && (frac_len + 1) > 0); - } - - buffer[idx--] = '.'; - - /* Convert/write integer part (right to left) */ - do { - buffer[idx--] = "0123456789"[intpart % 10]; - intpart /= 10; - } while(intpart); - - /* Write a sign, if requested */ - if(fvalue < 0) - buffer[idx--] = '-'; - else if(flags) - buffer[idx--] = '+'; - - *currlen = maxlen - idx - 2; - return buffer + idx + 1; -} - - /* * Thanks to the patch in this Debian bug for the solution * to the crash inside vsnprintf on some architectures. |