Web lists-archives.com

Re: [Mingw-users] msvcrt printf bug




Hello all,
Thank you Frank for your valuable contribution. It was a good read.
Eli, converting a float to string or backwards doesn't have to involve floating point operations.
I looked at the glibc implementation (printf_fp.c) and it appears to use the GMP library.
Rounding in the case of truncating significant digits appears to be done on the decimal string.
My conversion code doesn't use any floating point ops either. I haven't yet implemented rounding, but my first thought would be to do it on the decimal string, post conversion. Power of 2 rounding can be done bitwise, but I don't know about power of 10 without fp ops.
When I tested for every possible input against sprintf, I used %.160f (and _XOPEN_SOURCE macro).
According to Dr. Regan, the number of fractional bits is equal to the number of decimals in the corresponding decimal representation:
http://www.exploringbinary.com/number-of-decimal-digits-in-a-binary-fraction
http://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers
Yes, a float passed to printf will get converted to double because of stdarg, but this conversion is lossless (no rounding). If we convert this back to float we get the same input. (I had to use a macro to prevent this conversion on my microcontroller, as arm-gcc would use up a lot of code space for it.)
All the best.

--
Securely sent with Tutanota. Claim your encrypted mailbox today!
https://tutanota.com

3. Feb 2017 10:38 by mingw-users-request@xxxxxxxxxxxxxxxxxxxxx:


Message: 2
Date: Fri, 03 Feb 2017 10:38:28 +0200
From: Eli Zaretskii <eliz@xxxxxxx>
Subject: Re: [Mingw-users] msvcrt printf bug
To: MinGW Users List <mingw-users@xxxxxxxxxxxxxxxxxxxxx>
Message-ID: <83a8a3vfcr.fsf@xxxxxxx>
[...]
Your description omits one crucial detail. Printing a floating-point
value using the printf family of functions involves formatting the
value's representation as specified by the format spec. In
particular, that spec defines the number of digits after the decimal
or the number of significant digits that the program wants to produce.
In most cases, the number of those digits is way less than in the full
decimal representation of the original value. Therefore, printing a
value almost always requires rounding the value to produce a
representation that accurately approximates the value within the
specified number of digits. And rounding involves floating-point
calculations. That is the reason for talking about floating-point
calculations in general in the context of printing FP values: these
two are in practice inseparable. Anyone who ever looked at the
innards of a printf implementation where it formats FP values will
tell you that.

 ...

FP values are almost _always_ "in the gaps". Even if your program
just converts a single literal value in the sources to the internal
representation, and then converts it back while printing, this
involves FP calculations under the hood which almost always will
produce values not representable in the underlying FP system, and
almost always will produce round-off errors on the way. To avoid that
to some extent, one can do FP calculations at higher precision (wider
FP data type), but eventually you always bump into this brick wall, at
least in principle. It isn't FUD, it's a fact of life.



------------------------------

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

------------------------------

_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users


End of MinGW-users Digest, Vol 129, Issue 2
*******************************************
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe