Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-06-25 Thread Marc Nieper-Wißkirchen
Hi Bruno,

thank you very much.

Best regards,

Marc

Am Do., 25. Juni 2020 um 21:19 Uhr schrieb Bruno Haible :
>
> > Marc Nieper-Wißkirchen wrote:
> > > Please see the attached patch file, my first attempt (and first
> > > contribution to Gnulib).
> >
> > The patch looks all correct.
> >
> > The only (tiny) issue is a matter of style: Two of the new module files 
> > don't
> > end in a newline.
>
> I've pushed Marc's patch in his name, with the newlines fixed.
>
>
> 2020-06-25  Marc Nieper-Wißkirchen  
>
> c-dtoastr, c-ldtoastr: new modules
> These modules provide the same functionality as the modules
> dtoastr and ldtoastr except for the formatting taking place in the
> C locale.
> * MODULES.html.sh: Add c-dtoastr and c-ldtoastr.
> * lib/c-dtoastr.c, lib/c-ldtoastr.c: New files.
> * lib/ftoastr.c: Prefix exported functions when the macro C_LOCALE is
> defined.  Use c_snprintf and c_strtod/c_strtold instead of
> snprintf and strtod/strtold whhen the macro C_LOCALE is defined.
> * lib/ftoastr.h: Add prototypes for c_dtoastr and c_ldtoastr.
> * modules/c-dtoastr, modules/c-dtoastr-tests, modules/c-ldtoastr,
> modules/c-ldtoastr-tests: New files.
> * tests/test-c-dtoastr.c, tests/test-c-dtoastr.sh,
> tests-c-ldtoastr.c tests-c-ldtoastr.sh: New files.
>
>



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-06-25 Thread Bruno Haible
> Marc Nieper-Wißkirchen wrote:
> > Please see the attached patch file, my first attempt (and first
> > contribution to Gnulib).
> 
> The patch looks all correct.
> 
> The only (tiny) issue is a matter of style: Two of the new module files don't
> end in a newline.

I've pushed Marc's patch in his name, with the newlines fixed.


2020-06-25  Marc Nieper-Wißkirchen  

c-dtoastr, c-ldtoastr: new modules
These modules provide the same functionality as the modules
dtoastr and ldtoastr except for the formatting taking place in the
C locale.
* MODULES.html.sh: Add c-dtoastr and c-ldtoastr.
* lib/c-dtoastr.c, lib/c-ldtoastr.c: New files.
* lib/ftoastr.c: Prefix exported functions when the macro C_LOCALE is
defined.  Use c_snprintf and c_strtod/c_strtold instead of
snprintf and strtod/strtold whhen the macro C_LOCALE is defined.
* lib/ftoastr.h: Add prototypes for c_dtoastr and c_ldtoastr.
* modules/c-dtoastr, modules/c-dtoastr-tests, modules/c-ldtoastr,
modules/c-ldtoastr-tests: New files.
* tests/test-c-dtoastr.c, tests/test-c-dtoastr.sh,
tests-c-ldtoastr.c tests-c-ldtoastr.sh: New files.





Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-23 Thread Marc Nieper-Wißkirchen
Am Sa., 23. Mai 2020 um 16:52 Uhr schrieb Bruno Haible :

> > In a future version, I may add a flag to select between %e, %f, and %g.
>
> Hmm, that route is likely going the path to sprintf: First a flag to
> select among %e, %f, %g. Then an optional width. Then a flag to select
> whether to fill with spaces or with zeroes.
> If you go that route, it's better to start with full sprintf and define
> a particular "precision" that would mean "as many digits as needed to
> guarantee that the result can be unambiguously read back in".

So you mean that we should hack it into
http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/vasnprintf.c?

How can we ensure that this module is (at least) in one-way sync with
glibc's (va)s(n)printf?

But it is an intriguing idea!



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-23 Thread Paul Eggert
On 5/23/20 5:26 AM, Marc Nieper-Wißkirchen wrote:
> I sent such an assignment request to the FSF a few days ago but
> haven't heard back from them yet.

It may take a few days more. If you don't hear back in a couple of weeks, please
let us know.

Thanks for writing that, by the way. It's a good improvement.



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-23 Thread Bruno Haible
Hello Marc,

> In a future version, I may add a flag to select between %e, %f, and %g.

Hmm, that route is likely going the path to sprintf: First a flag to
select among %e, %f, %g. Then an optional width. Then a flag to select
whether to fill with spaces or with zeroes.
If you go that route, it's better to start with full sprintf and define
a particular "precision" that would mean "as many digits as needed to
guarantee that the result can be unambiguously read back in".

Bruno




Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-23 Thread Marc Nieper-Wißkirchen
Hi Bruno,

[...]

> The only (tiny) issue is a matter of style: Two of the new module files don't
> end in a newline. In Gnulib, we try to let every non-empty text file end in a
> newline, since "cat FILE" and "sed -e ... FILE" work better this way.

I will correct this when I send you the final patch. In a future
version, I may add a flag to select between %e, %f, and %g.

> I would apply this patch. But first, we need a copyright assignment of yours.
> As mentioned in [1], I think a simple copyright assignment is enough. You 
> start
> the process by taking the gnulib/doc/Copyright/request-assign.future template
> and sending it the FSF.

I sent such an assignment request to the FSF a few days ago but
haven't heard back from them yet.

Marc



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-23 Thread Bruno Haible
Hi Marc,

Marc Nieper-Wißkirchen wrote:
> Please see the attached patch file, my first attempt (and first
> contribution to Gnulib).

The patch looks all correct.

The only (tiny) issue is a matter of style: Two of the new module files don't
end in a newline. In Gnulib, we try to let every non-empty text file end in a
newline, since "cat FILE" and "sed -e ... FILE" work better this way.

I would apply this patch. But first, we need a copyright assignment of yours.
As mentioned in [1], I think a simple copyright assignment is enough. You start
the process by taking the gnulib/doc/Copyright/request-assign.future template
and sending it the FSF.

Bruno

[1] https://lists.gnu.org/archive/html/bug-gnulib/2020-05/msg00204.html




Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-20 Thread Marc Nieper-Wißkirchen
Please see the attached patch file, my first attempt (and first
contribution to Gnulib).

Am Di., 19. Mai 2020 um 17:51 Uhr schrieb Paul Eggert :
>
> On 5/19/20 8:35 AM, Marc Nieper-Wißkirchen wrote:
> >> It is, however, locale-dependent, and there is no "c_dtoastr"
> >> version as there is a "c_strtod". (Could we get such a wrapper?)
> > Or a version "c_dtoastr" that uses "c_snprintf" and "c_strtod" internally.
>
> Yes, that'd be good to have. Could you write that?
From f4308c51cc73a8b1397c436f8dd667e15d6d0b9b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc=20Nieper-Wi=C3=9Fkirchen?= 
Date: Wed, 20 May 2020 13:59:31 +0200
Subject: [PATCH] New module to accurately print (long) doubles in C locale.

---
 ChangeLog| 18 +
 MODULES.html.sh  |  2 ++
 lib/c-dtoastr.c  |  3 +++
 lib/c-ldtoastr.c |  3 +++
 lib/ftoastr.c| 23 +++-
 lib/ftoastr.h|  6 +
 modules/c-dtoastr| 27 +++
 modules/c-dtoastr-tests  | 18 +
 modules/c-ldtoastr   | 27 +++
 modules/c-ldtoastr-tests | 18 +
 tests/test-c-dtoastr.c   | 58 
 tests/test-c-dtoastr.sh  | 15 +++
 tests/test-c-ldtoastr.c  | 58 
 tests/test-c-ldtoastr.sh | 15 +++
 14 files changed, 285 insertions(+), 6 deletions(-)
 create mode 100644 lib/c-dtoastr.c
 create mode 100644 lib/c-ldtoastr.c
 create mode 100644 modules/c-dtoastr
 create mode 100644 modules/c-dtoastr-tests
 create mode 100644 modules/c-ldtoastr
 create mode 100644 modules/c-ldtoastr-tests
 create mode 100644 tests/test-c-dtoastr.c
 create mode 100644 tests/test-c-dtoastr.sh
 create mode 100644 tests/test-c-ldtoastr.c
 create mode 100644 tests/test-c-ldtoastr.sh

diff --git a/ChangeLog b/ChangeLog
index 1c39b92e6..becb40eec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2020-05-20  Marc Nieper-Wißkirchen  
+
+	c-dtoastr: new module
+	c-ldtoastr: new module
+	These modules provide the same functionality as the modules
+	dtoastr and ldtoastr except for the formatting taking place in the
+	C locale.
+	* MODULES.html.sh: Add c-dtoastr and c-ldtoastr.
+	* lib/c-dtoastr.c, lib/c-ldtoastr.c: New files
+	* lib/ftoastr.c: Prefix exported functions when the macro C_LOCALE is
+	defined.  Use c_snprintf and c_strtod/c_strtold instead of
+	snprintf and strtod/strtold whhen the macro C_LOCALE is defined.
+	* lib/ftoastr.h: Add prototypes for c_dtoastr and c_ldtoastr.
+	* modules/c-dtoastr, modules/c-dtoastr-tests, modules/c-ldtoastr,
+	modules/c-ldtoastr-tests: New files.
+	* tests/test-c-dtoastr.c, tests/test-c-dtoastr.sh,
+	tests-c-ldtoastr.c tests-c-ldtoastr.sh: New files.
+
 2020-05-19  Paul Eggert  
 
 	ftoastr: fix ifndef typo
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 318a15a1d..280bd14da 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2319,6 +2319,8 @@ func_all_modules ()
   func_echo "$element"
 
   func_begin_table
+  func_module c-dtoastr
+  func_module c-ldtoast
   func_module dtoastr
   func_module ftoastr
   func_module intprops
diff --git a/lib/c-dtoastr.c b/lib/c-dtoastr.c
new file mode 100644
index 0..b57524fb1
--- /dev/null
+++ b/lib/c-dtoastr.c
@@ -0,0 +1,3 @@
+#define LENGTH 2
+#define C_LOCALE 1
+#include "ftoastr.c"
diff --git a/lib/c-ldtoastr.c b/lib/c-ldtoastr.c
new file mode 100644
index 0..5446fc3e7
--- /dev/null
+++ b/lib/c-ldtoastr.c
@@ -0,0 +1,3 @@
+#define LENGTH 3
+#define C_LOCALE 1
+#include "ftoastr.c"
diff --git a/lib/ftoastr.c b/lib/ftoastr.c
index 7a7d4113c..47a83152e 100644
--- a/lib/ftoastr.c
+++ b/lib/ftoastr.c
@@ -33,20 +33,28 @@
 #include 
 #include 
 
+#ifdef C_LOCALE
+# include "c-snprintf.h"
+# include "c-strtod.h"
+# define PREFIX(name) c_ ## name
+#else
+# define PREFIX(name) name
+#endif
+
 #if LENGTH == 3
 # define FLOAT long double
 # define FLOAT_DIG LDBL_DIG
 # define FLOAT_MIN LDBL_MIN
 # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND
-# define FTOASTR ldtoastr
+# define FTOASTR PREFIX (ldtoastr)
 # define PROMOTED_FLOAT long double
-# define STRTOF strtold
+# define STRTOF PREFIX (strtold)
 #elif LENGTH == 2
 # define FLOAT double
 # define FLOAT_DIG DBL_DIG
 # define FLOAT_MIN DBL_MIN
 # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND
-# define FTOASTR dtoastr
+# define FTOASTR PREFIX (dtoastr)
 # define PROMOTED_FLOAT double
 #else
 # define LENGTH 1
@@ -54,7 +62,7 @@
 # define FLOAT_DIG FLT_DIG
 # define FLOAT_MIN FLT_MIN
 # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND
-# define FTOASTR ftoastr
+# define FTOASTR PREFIX (ftoastr)
 # define PROMOTED_FLOAT double
 # if HAVE_STRTOF
 #  define STRTOF strtof
@@ -65,13 +73,16 @@
may generate one or two extra digits, but that's better than not
working at all.  */
 #ifndef STRTOF
-# define STRTOF strtod
+# define STRTOF PREFIX (strtod)
 #endif
 
 /* On hosts where it's not known that snprintf works, use sprintf to
implement the subset 

Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-20 Thread Marc Nieper-Wißkirchen
Am Mi., 20. Mai 2020 um 00:37 Uhr schrieb Bruno Haible :

> Marc Nieper-Wißkirchen wrote:
> > for output, the shortest rounded
> > representation that still reads back accurately has to be selected.
> > ...
> > A simple algorithm is given by Aubrey Jaffer in [1].
> >
> > [1] http://people.csail.mit.edu/jaffer/III/EZFPRW

This was just one, which I liked because of its simplicity. (And
because it is written in my favorite programming language.)

[...]

> There are several other algorithms for this purpose.

There is also the new Ryu algorithm by Ulf Adams, see [2].
Interestingly, he has found some problems in Jaffer's algorithm, see
the paper [3].

[...]

> Then there's also the algorithm, by Michael Stoll and me (1990),
> in GNU clisp and CLN (based on multiprecision arithmetic, not hardware 
> floats).

I didn't know this; I will check this out.

The initial version of "c_dtoastr" will, however just work as "dtoastr" does.

Marc

--

[2] https://github.com/ulfjack/ryu

[3] https://dl.acm.org/doi/10.1145/3296979.3192369



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Bruno Haible
Marc Nieper-Wißkirchen wrote:
> for output, the shortest rounded
> representation that still reads back accurately has to be selected.
> ...
> A simple algorithm is given by Aubrey Jaffer in [1].
> 
> [1] http://people.csail.mit.edu/jaffer/III/EZFPRW

There are several other algorithms for this purpose.

There is an overview by RyanJuckett at
http://www.ryanjuckett.com/programming/printing-floating-point-numbers/

The papers I know of are:

* Guy L. Steele Jr, Jon L White
  "How to Print Floating-Point Numbers Accurately"
  1990

* Robert G. Burger, R. Kent Dybvig
  "Printing Floating-Point Numbers Quickly and Accurately"
  1996

* Florian Loitsch
  "Printing Floating-Point Numbers Quickly and Accurately with Integers"
  2010

* Marc Andrysco, Ranjit Jhala, Sorin Lerner
  "Printing Floating-Point Numbers"
  2016

Then there's also the algorithm, by Michael Stoll and me (1990),
in GNU clisp and CLN (based on multiprecision arithmetic, not hardware floats).

Bruno




Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Tim Rühsen
On 19.05.20 21:10, Marc Nieper-Wißkirchen wrote:
> Am Di., 19. Mai 2020 um 19:23 Uhr schrieb Tim Rühsen :
> 
>> "copyright" here means more "Nutzungsrecht" which you can transfer also
>> in Germany. You still stay the "Urheber".
> 
> If transferring the "Nutzungsrecht" is enough, everything should be fine.

The first step is to follow ./doc/Copyright/request-assign.future. That is

Please email the following information to ass...@gnu.org, and we
will send you the assignment form for your past and future changes.

Please use your full legal name (in ASCII characters) as the subject
line of the message.
--
REQUEST: SEND FORM FOR PAST AND FUTURE CHANGES

[What is the name of the program or package you're contributing to?]


[Did you copy any files or text written by someone else in these changes?
Even if that material is free software, we need to know about it.]


[Do you have an employer who might have a basis to claim to own
your changes?  Do you attend a school which might make such a claim?]


[For the copyright registration, what country are you a citizen of?]


[What year were you born?]


[Please write your email address here.]


[Please write your postal address here.]





[Which files have you changed so far, and which new files have you written
so far?]




signature.asc
Description: OpenPGP digital signature


Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Marc Nieper-Wißkirchen
Am Di., 19. Mai 2020 um 19:23 Uhr schrieb Tim Rühsen :

> "copyright" here means more "Nutzungsrecht" which you can transfer also
> in Germany. You still stay the "Urheber".

If transferring the "Nutzungsrecht" is enough, everything should be fine.

Marc



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Tim Rühsen
On 19.05.20 18:11, Marc Nieper-Wißkirchen wrote:
> Am Di., 19. Mai 2020 um 17:51 Uhr schrieb Paul Eggert :
>>
>> On 5/19/20 8:35 AM, Marc Nieper-Wißkirchen wrote:
 It is, however, locale-dependent, and there is no "c_dtoastr"
 version as there is a "c_strtod". (Could we get such a wrapper?)
>>> Or a version "c_dtoastr" that uses "c_snprintf" and "c_strtod" internally.
>>
>> Yes, that'd be good to have. Could you write that?
> 
> I should be able to. But please note that I haven't filled out an
> assignment form for copyright transfer yet (which is, by the copyright
> law of Germany, where I live, technically not possible, by the way). I
> hope this is not a major problem.
> 
> Marc

Hi Marc,

"copyright" here means more "Nutzungsrecht" which you can transfer also
in Germany. You still stay the "Urheber".

The german Urheberrecht has been changed a few years ago to be pretty
much in sync with international laws.

I am not an expert in this area and can't give you any advice here :-)

Regards, Tim



signature.asc
Description: OpenPGP digital signature


Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Marc Nieper-Wißkirchen
Am Di., 19. Mai 2020 um 17:51 Uhr schrieb Paul Eggert :
>
> On 5/19/20 8:35 AM, Marc Nieper-Wißkirchen wrote:
> >> It is, however, locale-dependent, and there is no "c_dtoastr"
> >> version as there is a "c_strtod". (Could we get such a wrapper?)
> > Or a version "c_dtoastr" that uses "c_snprintf" and "c_strtod" internally.
>
> Yes, that'd be good to have. Could you write that?

I should be able to. But please note that I haven't filled out an
assignment form for copyright transfer yet (which is, by the copyright
law of Germany, where I live, technically not possible, by the way). I
hope this is not a major problem.

Marc



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Paul Eggert

On 5/19/20 8:35 AM, Marc Nieper-Wißkirchen wrote:

It is, however, locale-dependent, and there is no "c_dtoastr"
version as there is a "c_strtod". (Could we get such a wrapper?)

Or a version "c_dtoastr" that uses "c_snprintf" and "c_strtod" internally.


Yes, that'd be good to have. Could you write that?



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Marc Nieper-Wißkirchen
Am Di., 19. Mai 2020 um 17:10 Uhr schrieb Marc Nieper-Wißkirchen
:

> I have just discovered the "dtoastr" module, which seems to do what I
> want. It is, however, locale-dependent, and there is no "c_dtoastr"
> version as there is a "c_strtod". (Could we get such a wrapper?)

Or a version "c_dtoastr" that uses "c_snprintf" and "c_strtod" internally.

Marc

P.S. Could we also get a flag for "(c_)strtod", with which one can
choose the %e or %f format explicitly? At the moment, it always uses
"%g".



Re: Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Marc Nieper-Wißkirchen
Am Di., 19. Mai 2020 um 09:44 Uhr schrieb Marc Nieper-Wißkirchen
:

> Such input and output routines for floats and doubles would also be
> very helpful in the C language. Do you think this could become a
> Gnulib module?

I have just discovered the "dtoastr" module, which seems to do what I
want. It is, however, locale-dependent, and there is no "c_dtoastr"
version as there is a "c_strtod". (Could we get such a wrapper?)

Marc



Easy Accurate Reading and Writing of Floating-Point Numbers

2020-05-19 Thread Marc Nieper-Wißkirchen
It is often important to be able to read back a written floating-point
number accurately so it has to be output with high enough precision.
The Scheme standard even demands this for the number->string and
string->number procedures. Moreover, for output, the shortest rounded
representation that still reads back accurately has to be selected.

Such input and output routines for floats and doubles would also be
very helpful in the C language. Do you think this could become a
Gnulib module?

A simple algorithm is given by Aubrey Jaffer in [1].

Marc

[1] http://people.csail.mit.edu/jaffer/III/EZFPRW