summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/lib/__dtostr.c
diff options
context:
space:
mode:
Diffstat (limited to 'mdk-stage1/dietlibc/lib/__dtostr.c')
-rw-r--r--mdk-stage1/dietlibc/lib/__dtostr.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/mdk-stage1/dietlibc/lib/__dtostr.c b/mdk-stage1/dietlibc/lib/__dtostr.c
index f54bde22b..580258936 100644
--- a/mdk-stage1/dietlibc/lib/__dtostr.c
+++ b/mdk-stage1/dietlibc/lib/__dtostr.c
@@ -1,7 +1,16 @@
#include <stdlib.h>
#include <endian.h>
+#include <math.h>
/* convert double to string. Helper for sprintf. */
+static int copystring(char* buf,int maxlen, const char* s) {
+ int i;
+ for (i=0; i<3&&i<maxlen; ++i)
+ buf[i]=s[i];
+ if (i<maxlen) { buf[i]=0; ++i; }
+ return i;
+}
+
int __dtostr(double d,char *buf,unsigned int maxlen,unsigned int prec,unsigned int prec2) {
#if 1
unsigned long long *x=(unsigned long long *)&d;
@@ -16,13 +25,16 @@ int __dtostr(double d,char *buf,unsigned int maxlen,unsigned int prec,unsigned i
#endif
/* unsigned long long m=*x & ((1ull<<52)-1); */
/* step 2: exponent is base 2, compute exponent for base 10 */
- signed long e10=1+(long)(e*0.30102999566398119802); /* log10(2) */
+ signed long e10;
/* step 3: calculate 10^e10 */
unsigned int i;
double backup=d;
double tmp;
char *oldbuf=buf;
+ if (isnan(d)) return copystring(buf,maxlen,"nan");
+ if ((i=isinf(d))) return copystring(buf,maxlen,i>0?"inf":"-inf");
+ e10=1+(long)(e*0.30102999566398119802); /* log10(2) */
/* Wir iterieren von Links bis wir bei 0 sind oder maxlen erreicht
* ist. Wenn maxlen erreicht ist, machen wir das nochmal in
* scientific notation. Wenn dann von prec noch was übrig ist, geben
@@ -37,7 +49,7 @@ int __dtostr(double d,char *buf,unsigned int maxlen,unsigned int prec,unsigned i
return i;
}
- if (d < 0.0) { d=-d; *buf='-'; --maxlen; buf++; }
+ if (d < 0.0) { d=-d; *buf='-'; --maxlen; ++buf; }
/*
Perform rounding. It needs to be done before we generate any
@@ -48,6 +60,7 @@ int __dtostr(double d,char *buf,unsigned int maxlen,unsigned int prec,unsigned i
for (i = 0; i < prec2; i++) { tmp *= 0.1; }
d += tmp;
+ if (d < 1.0) { *buf='0'; --maxlen; ++buf; }
/* printf("e=%d e10=%d prec=%d\n",e,e10,prec); */
if (e10>0) {
int first=1; /* are we about to write the first digit? */