Skip to content

Commit

Permalink
py: Implement mp_format_float for doubles and use where appropriate
Browse files Browse the repository at this point in the history
This allows using (almost) the same code for printing floats everywhere,
removes the dependency on sprintf and uses just snprintf and
applies an msvc-specific fix for snprintf in a single place so
nan/inf are now printed correctly.
  • Loading branch information
stinos authored and dpgeorge committed May 17, 2015
1 parent f5dd6f7 commit 861670b
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 112 deletions.
90 changes: 79 additions & 11 deletions py/formatfloat.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
* THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdint.h>
#include "py/formatfloat.h"

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
/***********************************************************************
formatfloat.c - Ruutine for converting a single-precision floating
point number into a string.
Routine for converting a single-precision floating
point number into a string.
The code in this funcion was inspired from Fred Bayer's pdouble.c.
Since pdouble.c was released as Public Domain, I'm releasing this
Expand All @@ -39,15 +44,6 @@
***********************************************************************/

#include <stdlib.h>
#include <stdint.h>

#include "py/mpconfig.h"

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT

#include "py/formatfloat.h"

// 1 sign bit, 8 exponent bits, and 23 mantissa bits.
// exponent values 0 and 255 are reserved, exponent can be 1 to 254.
// exponent is stored with a bias of 127.
Expand Down Expand Up @@ -341,4 +337,76 @@ int mp_format_float(float f, char *buf, size_t buf_size, char fmt, int prec, cha
return s - buf;
}

#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE

#include <errno.h>
#include <stdio.h>

#ifdef _MSC_VER
// For msvc we need to address some quirks in the snprintf implementation:
// - there is no standard snprintf, it is named _snprintf instead
// - 'F' format isn't handled so use 'f'
// - nan and inf are printed as 1.#QNAN and 1.#INF
#include <math.h>
#include <string.h>

STATIC int copy_with_sign(char *dest, size_t bufSize, const char *value, char sign) {
if (bufSize == 0) {
return 0;
}
size_t numSignChars = 0;
if (sign) {
*dest = sign;
++numSignChars;
}
// check total length including terminator
size_t length = strlen(value) + 1 + numSignChars;
if (length > bufSize) {
length = bufSize;
}
// length without terminator
--length;
if (length > numSignChars) {
memcpy(dest + numSignChars, value, length - numSignChars);
}
dest[length] = 0;
return length;
}

#define snprintf _snprintf
#endif

int mp_format_float(double value, char *buf, size_t bufSize, char fmt, int prec, char sign) {
if (!buf) {
errno = EINVAL;
return -1;
}
#ifdef _MSC_VER
if (isnan(value)) {
return copy_with_sign(buf, bufSize, "nan", sign);
} else if (isinf(value)) {
return copy_with_sign(buf, bufSize, "inf", value > 0.0 ? sign : '-');
} else {
if (fmt == 'F') {
fmt = 'f';
}
#endif
char fmt_buf[6];
char *fmt_s = fmt_buf;

*fmt_s++ = '%';
if (sign) {
*fmt_s++ = sign;
}
*fmt_s++ = '.';
*fmt_s++ = '*';
*fmt_s++ = fmt;
*fmt_s = '\0';

return snprintf(buf, bufSize, fmt_buf, prec, value);
#ifdef _MSC_VER
}
#endif
}

#endif
6 changes: 5 additions & 1 deletion py/formatfloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#ifndef __MICROPY_INCLUDED_PY_FORMATFLOAT_H__
#define __MICROPY_INCLUDED_PY_FORMATFLOAT_H__

int mp_format_float(float f, char *buf, size_t bufSize, char fmt, int prec, char sign);
#include "py/mpconfig.h"

#if MICROPY_PY_BUILTINS_FLOAT
int mp_format_float(mp_float_t f, char *buf, size_t bufSize, char fmt, int prec, char sign);
#endif

#endif // __MICROPY_INCLUDED_PY_FORMATFLOAT_H__
25 changes: 2 additions & 23 deletions py/mpprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#include "py/objint.h"
#include "py/runtime.h"

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
#include <stdio.h>
#endif

#if MICROPY_PY_BUILTINS_FLOAT
#include "py/formatfloat.h"
#endif
Expand Down Expand Up @@ -340,29 +336,12 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c
if (flags & PF_FLAG_SPACE_SIGN) {
sign = ' ';
}
int len;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign);
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
char fmt_buf[6];
char *fmt_s = fmt_buf;

*fmt_s++ = '%';
if (sign) {
*fmt_s++ = sign;
}
*fmt_s++ = '.';
*fmt_s++ = '*';
*fmt_s++ = fmt;
*fmt_s = '\0';

len = snprintf(buf, sizeof(buf), fmt_buf, prec, f);
int len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign);
if (len < 0) {
len = 0;
}
#else
#error Unknown MICROPY FLOAT IMPL
#endif

char *s = buf;

if ((flags & PF_FLAG_ADD_PERCENT) && (size_t)(len + 1) < sizeof(buf)) {
Expand Down
25 changes: 6 additions & 19 deletions py/objcomplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
#if MICROPY_PY_BUILTINS_COMPLEX

#include <math.h>

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#include "py/formatfloat.h"
#endif

typedef struct _mp_obj_complex_t {
mp_obj_base_t base;
Expand All @@ -53,33 +50,23 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_
mp_obj_complex_t *o = o_in;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char buf[16];
if (o->real == 0) {
mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0');
mp_printf(print, "%sj", buf);
} else {
mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0');
mp_printf(print, "(%s", buf);
if (o->imag >= 0 || isnan(o->imag)) {
mp_print_str(print, "+");
}
mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0');
mp_printf(print, "%sj)", buf);
}
const int precision = 7;
#else
char buf[32];
const int precision = 16;
#endif
if (o->real == 0) {
sprintf(buf, "%.16g", (double)o->imag);
mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0');
mp_printf(print, "%sj", buf);
} else {
sprintf(buf, "%.16g", (double)o->real);
mp_format_float(o->real, buf, sizeof(buf), 'g', precision, '\0');
mp_printf(print, "(%s", buf);
if (o->imag >= 0 || isnan(o->imag)) {
mp_print_str(print, "+");
}
sprintf(buf, "%.16g", (double)o->imag);
mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0');
mp_printf(print, "%sj)", buf);
}
#endif
}

STATIC mp_obj_t complex_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Expand Down
15 changes: 4 additions & 11 deletions py/objfloat.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,24 @@
#if MICROPY_PY_BUILTINS_FLOAT

#include <math.h>

#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#include "py/formatfloat.h"
#endif

STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_float_t *o = o_in;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char buf[16];
mp_format_float(o->value, buf, sizeof(buf), 'g', 7, '\0');
mp_print_str(print, buf);
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
// Python floats always have decimal point (unless inf or nan)
mp_print_str(print, ".0");
}
const int precision = 7;
#else
char buf[32];
sprintf(buf, "%.16g", (double) o->value);
const int precision = 16;
#endif
mp_format_float(o->value, buf, sizeof(buf), 'g', precision, '\0');
mp_print_str(print, buf);
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
// Python floats always have decimal point (unless inf or nan)
mp_print_str(print, ".0");
}
#endif
}

STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Expand Down
1 change: 1 addition & 0 deletions tests/float/float1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
print(float("1e+1"))
print(float("1e-1"))
print(float("inf"))
print(float("-inf"))
print(float("INF"))
print(float("infinity"))
print(float("INFINITY"))
Expand Down
3 changes: 0 additions & 3 deletions windows/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,4 @@ void msec_sleep(double msec);
#include <stddef.h> //for NULL
#include <assert.h> //for assert

// Functions implemented in platform code

int snprintf(char *dest, size_t count, const char *format, ...);
#endif
44 changes: 0 additions & 44 deletions windows/msvc/snprintf.c

This file was deleted.

0 comments on commit 861670b

Please sign in to comment.