bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accurate reading of floating point numbers


From: Bruno Haible
Subject: Re: Accurate reading of floating point numbers
Date: Mon, 26 Aug 2024 12:37:27 +0200

Hi Marc,

> Gnulib
> offers strtod replacements, but these refer to conversion routines of the
> host systems and may lack the accuracy described in Clinger's well-known
> paper [1] IIUC.
> 
> Wouldn't it make sense to include an accurate conversion algorithm in
> Gnulib like [2] with the small improvement from [3]?

If you have time to do so, and would like to contribute decent unit tests
accompanying the code, please do so!

> Maybe code from glibc can be reused.

The problem with the glibc code is that they have specialized code for each
format:
  - IEEE 754 single-precision,
  - IEEE 754 double-precision,
  - 'long double' with LDBL_MANT_DIG == 106 (a.k.a. "double double"),
  - 'long double' with LDBL_MANT_DIG == 113 (a.k.a. "quad precision").
That makes for a lot of code and a lot of required testing, and is not
future-proof (regarding new floating-point types).

If possible, in Gnulib, we would prefer a single implementation, even if
it is a bit slow.

> Let me also mention what I feel is an inconvenience of the standard C
> library functions: they mix parsing/composing the strings with the actual
> conversion routines.

That's because the "actual conversion routines" have a big-integer type
as input or output, and standard C does not have these types.

> Routines that take the sign, the (integer) mantissa,
> and the exponent separately (or return them separately) are more
> fundamental.

Yes, and you find such routines in GMP or possibly in minigmp.

For the "composing" part, Gnulib has such routines in vasnprintf.c,
lines 443..1650.

Bruno






reply via email to

[Prev in Thread] Current Thread [Next in Thread]