Browse Source

dtostrf support nan & inf (Frank Boesing)

main
PaulStoffregen 8 years ago
parent
commit
eae654412c
1 changed files with 52 additions and 1 deletions
  1. +52
    -1
      teensy3/nonstd.c

+ 52
- 1
teensy3/nonstd.c View File

#include "avr_functions.h" #include "avr_functions.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h>




char * ultoa(unsigned long val, char *buf, int radix)
char * ultoa(unsigned long val, char *buf, int radix)
{ {
unsigned digit; unsigned digit;
int i=0, j; int i=0, j;
} }
} }


#define DTOA_UPPER 0x04

char * fcvtf(float, int, int *, int *); char * fcvtf(float, int, int *, int *);
int isnanf (float x);
int isinff (float x);


char * dtostrf(float val, int width, unsigned int precision, char *buf) char * dtostrf(float val, int width, unsigned int precision, char *buf)
{ {
const char *s, *e; const char *s, *e;
char *p; char *p;


int awidth = abs(width);
if (isnanf(val)) {
int ndigs = (val<0) ? 4 : 3;
awidth = (awidth > ndigs) ? awidth - ndigs : 0;
if (width<0) {
while (awidth) {
*buf++ = ' ';
awidth--;
}
}
if (copysignf(1.0f, val)<0) *buf++ = '-';
if (DTOA_UPPER) {
*buf++ = 'N'; *buf++ = 'A'; *buf++ = 'N';
} else {
*buf++ = 'n'; *buf++ = 'a'; *buf++ = 'n';
}
while (awidth) {
*buf++ = ' ';
awidth--;
}
*buf = 0;
return buf;
}
if (isinff(val)) {
int ndigs = (val<0) ? 4 : 3;
awidth = (awidth > ndigs) ? awidth - ndigs : 0;
if (width<0) {
while (awidth) {
*buf++ = ' ';
awidth--;
}
}
if (val<0) *buf++ = '-';
if (DTOA_UPPER) {
*buf++ = 'I'; *buf++ = 'N'; *buf++ = 'F';
} else {
*buf++ = 'i'; *buf++ = 'n'; *buf++ = 'f';
}
while (awidth) {
*buf++ = ' ';
awidth--;
}
*buf = 0;
return buf;
}

s = fcvtf(val, precision, &decpt, &sign); s = fcvtf(val, precision, &decpt, &sign);
if (precision == 0 && decpt == 0) { if (precision == 0 && decpt == 0) {
s = (*s < '5') ? "0" : "1"; s = (*s < '5') ? "0" : "1";

Loading…
Cancel
Save