Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard C way to print floating-point values "perfectly" a la Dragon4?

Reading Here be dragons: advances in problems you didn’t even know you had I've noticed that they compare the new algorithm with the one used in glibc's printf:

Grisu3 is about 5 times faster than the algorithm used by printf in GNU libc

But at the same time I've failed to find any format specifier for printf which would automatically find the best number of decimal places to print. All I tried either have some strange defaults like 6 digits after decimal point for %f or 2 after point for %g or 6 after point for %e.

How do I actually make use of that algorithm implementation in glibc, mentioned in the article? Is there really any such implementation in glibc and is it even discussed in any way by the Standard?

like image 537
Ruslan Avatar asked Jun 03 '15 10:06

Ruslan


1 Answers

This is the actual article. The blog post is referring to the results in section 7 (in other words, “they” are not comparing anything in the blog post, “they” are regurgitating the information from the actual article, omitting crucial details):

table of results with information

Implementations of Dragon4 or Grisu3 can be found in implementations of modern programming languages that specify this “minimal number of decimal digits” fashion (I recommend you avoid calling it “perfect”). Java uses this type of conversion to decimal in some contexts, as does Ruby. C is not one of the languages that specify “minimal number of decimal digits” conversion to decimal, so there is no reason for a compiler or for a libc to provide an implementation for Dragon4 or Grisu3.

like image 92
Pascal Cuoq Avatar answered Sep 24 '22 00:09

Pascal Cuoq