-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from majenkotech/fix-dtostrf
Rewrite dtostrf to use snprintf with float formatting
- Loading branch information
Showing
5 changed files
with
236 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
core_esp8266_noniso.c - nonstandard (but usefull) conversion functions | ||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. | ||
This file is part of the esp8266 core for Arduino environment. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
Modified 03 April 2015 by Markus Sattler | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <math.h> | ||
#include "stdlib_noniso.h" | ||
|
||
#if _NEED_REVERSE | ||
void reverse(char* begin, char* end) { | ||
char *is = begin; | ||
char *ie = end - 1; | ||
while(is < ie) { | ||
char tmp = *ie; | ||
*ie = *is; | ||
*is = tmp; | ||
++is; | ||
--ie; | ||
} | ||
} | ||
#endif | ||
|
||
#ifdef _NEED_LTOA | ||
char* ltoa(long value, char* result, int base) { | ||
if(base < 2 || base > 16) { | ||
*result = 0; | ||
return result; | ||
} | ||
|
||
char* out = result; | ||
long quotient = abs(value); | ||
|
||
do { | ||
const long tmp = quotient / base; | ||
*out = "0123456789abcdef"[quotient - (tmp * base)]; | ||
++out; | ||
quotient = tmp; | ||
} while(quotient); | ||
|
||
// Apply negative sign | ||
if(value < 0) | ||
*out++ = '-'; | ||
|
||
reverse(result, out); | ||
*out = 0; | ||
return result; | ||
} | ||
#endif | ||
|
||
#ifdef _NEED_ULTOA | ||
char* ultoa(unsigned long value, char* result, int base) { | ||
if(base < 2 || base > 16) { | ||
*result = 0; | ||
return result; | ||
} | ||
|
||
char* out = result; | ||
unsigned long quotient = value; | ||
|
||
do { | ||
const unsigned long tmp = quotient / base; | ||
*out = "0123456789abcdef"[quotient - (tmp * base)]; | ||
++out; | ||
quotient = tmp; | ||
} while(quotient); | ||
|
||
reverse(result, out); | ||
*out = 0; | ||
return result; | ||
} | ||
#endif | ||
|
||
#ifdef _NEED_DTOSTRF | ||
char * dtostrf(double number, signed char width, unsigned char prec, char *s) { | ||
bool negative = false; | ||
|
||
if (isnan(number)) { | ||
strcpy(s, "nan"); | ||
return s; | ||
} | ||
if (isinf(number)) { | ||
strcpy(s, "inf"); | ||
return s; | ||
} | ||
|
||
char* out = s; | ||
|
||
int fillme = width; // how many cells to fill for the integer part | ||
if (prec > 0) { | ||
fillme -= (prec+1); | ||
} | ||
|
||
// Handle negative numbers | ||
if (number < 0.0) { | ||
negative = true; | ||
fillme--; | ||
number = -number; | ||
} | ||
|
||
// Round correctly so that print(1.999, 2) prints as "2.00" | ||
// I optimized out most of the divisions | ||
double rounding = 2.0; | ||
for (uint8_t i = 0; i < prec; ++i) | ||
rounding *= 10.0; | ||
rounding = 1.0 / rounding; | ||
|
||
number += rounding; | ||
|
||
// Figure out how big our number really is | ||
double tenpow = 1.0; | ||
int digitcount = 1; | ||
while (number >= 10.0 * tenpow) { | ||
tenpow *= 10.0; | ||
digitcount++; | ||
} | ||
|
||
number /= tenpow; | ||
fillme -= digitcount; | ||
|
||
// Pad unused cells with spaces | ||
while (fillme-- > 0) { | ||
*out++ = ' '; | ||
} | ||
|
||
// Handle negative sign | ||
if (negative) *out++ = '-'; | ||
|
||
// Print the digits, and if necessary, the decimal point | ||
digitcount += prec; | ||
int8_t digit = 0; | ||
while (digitcount-- > 0) { | ||
digit = (int8_t)number; | ||
if (digit > 9) digit = 9; // insurance | ||
*out++ = (char)('0' | digit); | ||
if ((digitcount == prec) && (prec > 0)) { | ||
*out++ = '.'; | ||
} | ||
number -= digit; | ||
number *= 10.0; | ||
} | ||
|
||
// make sure the string is terminated | ||
*out = 0; | ||
return s; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
stdlib_noniso.h - nonstandard (but usefull) conversion functions | ||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. | ||
This file is part of the esp8266 core for Arduino environment. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef STDLIB_NONISO_H | ||
#define STDLIB_NONISO_H | ||
|
||
//#define _NEED_REVERSE | ||
//#define _NEED_LTOA | ||
//#define _NEED_ULTOA | ||
#define _NEED_DTOSTRF | ||
|
||
#ifdef __cplusplus | ||
extern "C"{ | ||
#endif | ||
|
||
int atoi(const char *s); | ||
|
||
long atol(const char* s); | ||
|
||
double atof(const char* s); | ||
|
||
char* itoa (int val, char *s, int radix); | ||
|
||
#ifdef _NEED_LTOA | ||
char* ltoa (long val, char *s, int radix); | ||
#endif | ||
|
||
char* utoa (unsigned int val, char *s, int radix); | ||
|
||
#ifdef _NEED_ULTOA | ||
char* ultoa (unsigned long val, char *s, int radix); | ||
#endif | ||
|
||
#ifdef _NEED_DTOSTRF | ||
char* dtostrf (double val, signed char width, unsigned char prec, char *s); | ||
#endif | ||
|
||
#ifdef _NEED_REVERSE | ||
void reverse(char* begin, char* end); | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
|
||
#endif |