Replacement for snprintf

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Oct 30 17:41:26 UTC 2019


On Wed, Oct 30, 2019 at 01:44:52PM +0000, berni44 via Digitalmars-d wrote:
> In PR 7222 [1] Robert Schadek suggested replacing the call to snprinf
> in std.format with an own method written in D. During the last days I
> took a deeper look into this and meanwhile I've got a function that
> works for floats (and probably also doubles, but I havn't tested that
> yet and it should also work with reals if ucent would be available;
> without ucent I need a workaround for real or fall back to BigInt).
> 
> I only implemented f qualifier yet, but it shouldn't be difficult to
> add e and g qualifiers and the uppercase versions. Also some work
> needs to be done, to implement the flags (-,+,0,<space>,#), but again,
> I think, this will not be very difficult. Unfortunately I'll be busy
> with some other (non-D) stuff for some time. I'll probably continue
> work on this someday in november.

If you haven't already, please read:

	https://www.zverovich.net/2019/02/11/formatting-floating-point-numbers.html

especially the papers linked in the first paragraph.

Formatting floating-point numbers is not a trivial task. It's easy to
write up something that works for common cases, but it's not so easy to
get something to gives the best results in *all* cases. You probably
should use the algorithms referenced above for your implementation,
instead of coming up with your own that may have unexpected corner cases
that don't produce the right output.


T

-- 
Valentine's Day: an occasion for florists to reach into the wallets of
nominal lovers in dire need of being reminded to profess their
hypothetical love for their long-forgotten.


More information about the Digitalmars-d mailing list