Commit 861670ba authored by stijn's avatar stijn Committed by Damien George
Browse files

py: Implement mp_format_float for doubles and use where appropriate

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.
parent f5dd6f7f
......@@ -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
......@@ -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.
......@@ -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
......@@ -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__
......@@ -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
......@@ -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)) {
......
......@@ -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;
......@@ -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) {
......
......@@ -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) {
......
......@@ -13,6 +13,7 @@ print(float("1e1"))
print(float("1e+1"))
print(float("1e-1"))
print(float("inf"))
print(float("-inf"))
print(float("INF"))
print(float("infinity"))
print(float("INFINITY"))
......
......@@ -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
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdio.h>
#include <malloc.h>
// _snprintf/vsnprintf are fine, except the 'F' specifier is not handled
int snprintf(char *dest, size_t count, const char *format, ...) {
const size_t fmtLen = strlen(format) + 1;
char *fixedFmt = alloca(fmtLen);
for (size_t i = 0; i < fmtLen; ++i)
fixedFmt[i] = format[i] == 'F' ? 'f' : format[i];
va_list args;
va_start(args, format);
const int ret = vsnprintf(dest, count, fixedFmt, args);
va_end(args);
return ret;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment