Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should printf("%.15e", 1e23); print?

I am experimenting with optimizing double->text conversion (trying to beat grissu, ryu etc...).

While doing so, I am comparing my results with sprintf outputs. Now I have encountered the above interesting case.

printf("%.15e", 1e23);

(e.g. glibc) prints

9.999999999999999e+22

while my routine prints

1.000000000000000e+23

Now both numbers are the same distance from the "true value" and converting both these values back (e.g. with atof) yields the same double.

However, I believe my result satisfies "round to even" rule (which is the reason it went this way).

Which result is more correct?

like image 516
Mirek Fidler Avatar asked Aug 22 '21 07:08

Mirek Fidler


People also ask

What is the output of the next printf statement?

The next printf statement in the program, if any, will begin its output at this position. Note that there are no spaces between these names. This is because the output of a printf statement appears immediately after the output of previous printf statement.

What is the value of a/B in printf?

The value of expression a/b is printed in a field of eight characters and three digits after the decimal point, as specified in the format 8. 3f. The ordinary (i. e., non-formatting) characters in the format string of a printf function call may include graphic as well as non-graphic characters.

What does printf return in C programming?

The printf function returns an integer value indicating the number of bytes printed. This value is usually ignored in the printf function call.. Consider the printf statement given below. The format string in this call does not contain any conversion specifications.

What is the third printf statement in C?

Third printf statement uses the %c format specification to print values of type int and char. Note that character constants are printed without single quotes and integer value 65 is printed as A (character representing ASCII value 65). The last printf statement prints the strings “Hello,” and “world” using the %s format specification.


1 Answers

1e23 is typically not exactly represented as a double.

The 2 closest choices are:

// %a           v     %f 
0x1.52d02c7e14af6p+76  99999999999999991611392.000000
0x1.52d02c7e14af7p+76 100000000000000008388608.000000

There are both 8388608.0 from 100000000000000000000000.0, one above, one below.

Typically the selected one in tie cases is the even one. (See last bit in hex format.)

99999999999999991611392.000000 is the winner and so an output of "9.999999999999999e+22" is expected.

When printing with more the DBL_DIG (15) significant digits, ("%.15e" prints 16 significant digits) this problem is possible as code is effectively doing a text-double-text round-trip and exceeding what double can round-trip.


I am experimenting with optimizing double->text conversion

I recommend to also use "%a" to gain deeper understanding.

like image 181
chux - Reinstate Monica Avatar answered Sep 28 '22 09:09

chux - Reinstate Monica