User
User

Reputation: 399

How to display the full length of a double with printf in C

How can I use printf in C to display the full length of a double.

Consider

int main(){

double x = 0.00000000000001;

printf("x: %f \n", x);

}

The result I get is "i: 0.000000", however from what I read %f is the correct format specifier and doubles should have 15-17 significant figures therefore I don't understand why I am getting a rounded result. I have also tried things like %15f and %lf.

I am using Ubuntu if that helps.

Upvotes: 3

Views: 9775

Answers (6)

pmg
pmg

Reputation: 108938

The best you can manage is to print in hexadecimal notation

printf("i: %a \n", i);

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html

a, A A double argument representing a floating-point number shall be converted in the style "[-]0xh.hhhhp±d", where there is one hexadecimal digit (which shall be non-zero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision; if the precision is missing and FLT_RADIX is a power of 2, then the precision shall be sufficient for an exact representation of the value; if the precision is missing and FLT_RADIX is not a power of 2, then the precision shall be sufficient to distinguish values of type double, except that trailing zeros may be omitted; if the precision is zero and the '#' flag is not specified, no decimal-point character shall appear. The letters "abcdef" shall be used for a conversion and the letters "ABCDEF" for A conversion. The A conversion specifier produces a number with 'X' and 'P' instead of 'x' and 'p'. The exponent shall always contain at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the value is zero, the exponent shall be zero.

A double argument representing an infinity or NaN shall be converted in the style of an f or F conversion specifier.

Upvotes: 0

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16026

From http://linux.die.net/man/3/printf:

f, F

The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.

Upvotes: 2

CiaPan
CiaPan

Reputation: 9570

Try using %g instead of %f. See the printf specification.

Upvotes: 1

deb_rider
deb_rider

Reputation: 580

You can print as much digits after decimal point as you want with %.<num>f. e.g;

printf("i: %.15f \n", i);

It prints 15 digits after decimal point.

Hope it helped...

Upvotes: 0

LearningC
LearningC

Reputation: 3162

use,

printf("i: %.15f\n", i);

for precision after decimal point specify as .15. number you specify after '.' is for precision.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122383

If you want to output 15 digits after the decimal points, it should be %.15f instead of %15f:

printf("i: %.15f \n", i);

Upvotes: 2

Related Questions