C: How long can it be double when printing via printf ()

I need to specify the exact line length for printing from a double value, but I do not want to limit the output more than necessary.

What is the maximum length that a six-digit double prefix will have when formatting printf ()?

In particular, what value should I give X to printf("%X.6lg",doubleValue);to ensure that the value will not be truncated?

The reason that I have to be specific about the length is because I define an MPI-based data type that consists of many string representations of double values ​​and needs to know their exact length in order to split file regions between MPI processes.

I hope this is clear. Thanks in advance for your reply.

+5
source share
3 answers

use printf("%.6g", doubleValue)or printf("%.6Lg", doubleValue)

Note that discarding leading digits ("width") in the precision specifier does not require length requirements for the integer part of a value.

Also note that the lowercase "l" indicates that your value is a long int. Upper case "L" indicates a long double value.

Also note that if you do not want this to potentially change to scientific notation (if it is a shorter representation), you should use "f" instead of "g".

See the printf link here .

+6
source

< float.h > , , DECIMAL_DIG, .

, , , 6 ...

PS: Demi . printf(), .

+1

IEEE double 1023, 1 + 1/2 + 1/4 + 1/8 +... .. * 2 ^ 1023. 318 , .

"e"?

+1

All Articles