summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2005-08-21 22:57:15 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2005-08-21 22:57:15 +0000
commitafca65e242950cdfd382c1c0a33bcafa08896605 (patch)
treecc67536aadf0f3988b75c6f7ee737149b0d91990
parente03244849545c85a87acd0d5276fd2e7c49d431a (diff)
downloadmpfr-afca65e242950cdfd382c1c0a33bcafa08896605.tar.gz
Better locale support (for the decimal point in string conversions
and input/output). git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@3729 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--NEWS1
-rw-r--r--mpfr-impl.h3
-rw-r--r--mpfr.texi11
-rw-r--r--out_str.c3
-rw-r--r--strtofr.c6
5 files changed, 20 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index 0f6bbb037..ed8c05814 100644
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,7 @@ Changes from version 2.1.1 to version 2.2.0:
- Thread safe (if built with --enable-thread-safe).
- Logging facility.
- Change in the semantics of mpfr_out_str/mpfr_get_str when n_digits=0.
+- Better locale support.
Changes from version 2.1.0 to version 2.1.1:
- Better way to detect the GMP library.
diff --git a/mpfr-impl.h b/mpfr-impl.h
index d236e511d..a8ba46f34 100644
--- a/mpfr-impl.h
+++ b/mpfr-impl.h
@@ -875,6 +875,9 @@ do { \
(r) = _size * BITS_PER_MP_LIMB - _cnt; \
} while (0)
+/* Needs <locale.h> */
+#define MPFR_DECIMAL_POINT ((unsigned char) localeconv()->decimal_point[0])
+
/******************************************************
************** Save exponent macros ****************
******************************************************/
diff --git a/mpfr.texi b/mpfr.texi
index 26bd04fa4..d3e27f86a 100644
--- a/mpfr.texi
+++ b/mpfr.texi
@@ -770,9 +770,15 @@ valid number then @var{rop} is set to zero and the value of @var{nptr}
is stored in the location referenced by @var{endptr}.
Parsing follows the standard C @code{strtod} function. This means optional
-leading whitespace, an optional @code{+} or @code{-}, mantissa digits, and an
+leading whitespace, an optional @code{+} or @code{-}, mantissa digits with
+an optional decimal point, and an
optional exponent consisting of an @code{e} or @code{E} (if
@math{@var{base} @le{} 10}) or @code{@@}, an optional sign, and digits.
+The decimal point can be either the one defined by the current locale or
+the period (the first one is accepted for consistency with the C standard
+and the practice, the second one is accepted to allow the programmer to
+provide MPFR numbers from strings in a way that does not depend on the
+current locale).
A hexadecimal mantissa can be given with a leading @code{0x} or @code{0X}, in
which case @code{p} or @code{P} may introduce an optional binary exponent,
indicating the power of 2 by which the mantissa is to be scaled. A binary
@@ -1453,7 +1459,8 @@ The base may vary from 2 to 36. Print @var{n} significant digits exactly,
or if @var{n} is 0, enough digits so that @var{op} can be read back
exactly (see @code{mpfr_get_str}).
-In addition to the significant digits, a decimal point at the right of the
+In addition to the significant digits, a decimal point (defined by the
+current locale) at the right of the
first digit and a trailing exponent in base 10, in the form @samp{eNNN},
are printed. If @var{base} is greater than 10, @samp{@@} will be used
instead of @samp{e} as exponent delimiter.
diff --git a/out_str.c b/out_str.c
index c96e58da0..ba47f4fb6 100644
--- a/out_str.c
+++ b/out_str.c
@@ -22,6 +22,7 @@ MA 02110-1301, USA. */
#include <stdio.h>
#include <string.h>
#include <limits.h>
+#include <locale.h>
#include "mpfr-impl.h"
@@ -83,7 +84,7 @@ mpfr_out_str (FILE *stream, int base, size_t n_digits, mpfr_srcptr op,
/* outputs mantissa */
fputc (*s++, stream); e--; /* leading digit */
- fputc ('.', stream); /* decimal point */
+ fputc (MPFR_DECIMAL_POINT, stream);
fputs (s, stream); /* rest of mantissa */
(*__gmp_free_func) (s0, l);
diff --git a/strtofr.c b/strtofr.c
index 6b4449d20..3bf4a2cac 100644
--- a/strtofr.c
+++ b/strtofr.c
@@ -22,6 +22,7 @@ MA 02110-1301, USA. */
#include <string.h> /* For strlen */
#include <stdlib.h> /* For strtol */
#include <ctype.h> /* For isdigit and isspace */
+#include <locale.h> /* For MPFR_DECIMAL_POINT */
#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"
@@ -226,6 +227,9 @@ parse_string (mpfr_t x, struct parsed_string *pstr,
int point;
int res = -1; /* Invalid input return value */
const char *prefix_str;
+ int decimal_point;
+
+ decimal_point = MPFR_DECIMAL_POINT;
/* Init variable */
pstr->mantissa = NULL;
@@ -325,7 +329,7 @@ parse_string (mpfr_t x, struct parsed_string *pstr,
for (;;) /* Loop until an invalid character is read */
{
int c = *str++;
- if (c == '.')
+ if (c == '.' || c == decimal_point)
{
if (MPFR_UNLIKELY(point)) /* Second '.': stop parsing */
break;