Skip to contentSkip to conversation

Double values - what precision do I use for fprintf?

DISCUSSION Shriramana Sharma 7 participants 11 messages Jan 12, 2006 – Jan 17, 2006

lore
Share & tools
lore
#0 lore raw

Please observe the following printf output of eighteen double variables:

First I tried using "%20.15f" as the format string:

268.229220252114942 1.018511211250181
66.833038547989787 12.450505533056681

  20.512170161382819    0.334923179252419
 259.358845955701327    1.571934717666495
 201.111247720862963    0.139476171219810
 270.658029698352152   -0.612280599380678
 105.175902698424665   -0.076345132989225
 343.809699615507384   -0.219206370786681

Then I used "%30.25f":

268.2297152740335377529845573 1.0185111507334088098986058
66.8390907719954157073516399 12.4504269834231084956854829

  20.5123328842112115921736404    0.3349264346316725426966343
 259.3596100022817267927166540    1.5719383906021777708161835
 201.1113154335791932680876926    0.1394751696317115263745734
 270.6577319741974747557833325   -0.6122834912138217511312632
 105.1758654981413201312534511   -0.0763454696609412730712307
 343.8095929658045974974811543   -0.2192182978826520134418843

Ignoring the small differences in the two sets of data (longitude and speed of eight celestial bodies computer at two different instants) I wonder - if I keep increasing the number of decimal places where will it end?

Signature · 7 lines
Shriramana Sharma  http://samvit.org
Running SUSE Linux 10.0 with KDE 3.5

-- 

Penguin #395953 resides at http://samvit.org
subsisting on SUSE Linux 10.0 with KDE 3.5
#1 lore raw

On Thu, 12 Jan 2006 18:00:19 +0530
Shriramana Sharma <Shriramana Sharma <[email protected]>> wrote:

 | First I tried using "%20.15f" as the format string:
 |
 | [...]
 | 
 | Then I used "%30.25f":
 |
 | [...] 
 | 
 | Ignoring the small differences in the two sets of data (longitude and speed
 | of eight celestial bodies computer at two different instants) I wonder - if

I | keep increasing the number of decimal places where will it end? At some point you will get only zeroes. This point is reached when the mantissa length is exceeded.

Leslie

Signature · 1 line
gpg --keyserver pgp.mit.edu --recv-keys 0x52D70289
#2 lore raw
In reply to Shriramana Sharma
Quoted 20 lines · Shriramana Sharma

Please observe the following printf output of eighteen double variables:

First I tried using "%20.15f" as the format string:

268.229220252114942 1.018511211250181
66.833038547989787 12.450505533056681
20.512170161382819 0.334923179252419
259.358845955701327 1.571934717666495
201.111247720862963 0.139476171219810

 270.658029698352152   -0.612280599380678
 105.175902698424665   -0.076345132989225
 343.809699615507384   -0.219206370786681

Then I used "%30.25f":

268.2297152740335377529845573 1.0185111507334088098986058
66.8390907719954157073516399 12.4504269834231084956854829
20.5123328842112115921736404 0.3349264346316725426966343
259.3596100022817267927166540 1.5719383906021777708161835
201.1113154335791932680876926 0.1394751696317115263745734

Quoted 7 lines
 270.6577319741974747557833325   -0.6122834912138217511312632
 105.1758654981413201312534511   -0.0763454696609412730712307
 343.8095929658045974974811543   -0.2192182978826520134418843

Ignoring the small differences in the two sets of data (longitude and speed of eight celestial bodies computer at two different instants) I wonder - if I keep increasing the number of decimal places where will it end?

Shriramana,

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

--

Steve Graegert <[email protected]>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

#3 lore raw
Quoted 28 lines
Quoted 20 lines

Please observe the following printf output of eighteen double variables:

First I tried using "%20.15f" as the format string:

268.229220252114942 1.018511211250181
66.833038547989787 12.450505533056681
20.512170161382819 0.334923179252419
259.358845955701327 1.571934717666495
201.111247720862963 0.139476171219810

 270.658029698352152   -0.612280599380678
 105.175902698424665   -0.076345132989225
 343.809699615507384   -0.219206370786681

Then I used "%30.25f":

268.2297152740335377529845573 1.0185111507334088098986058
66.8390907719954157073516399 12.4504269834231084956854829
20.5123328842112115921736404 0.3349264346316725426966343
259.3596100022817267927166540 1.5719383906021777708161835
201.1113154335791932680876926 0.1394751696317115263745734

Quoted 7 lines
 270.6577319741974747557833325   -0.6122834912138217511312632
 105.1758654981413201312534511   -0.0763454696609412730712307
 343.8095929658045974974811543   -0.2192182978826520134418843

Ignoring the small differences in the two sets of data (longitude and speed of eight celestial bodies computer at two different instants) I wonder - if I keep increasing the number of decimal places where will it end?

Quoted 9 lines

Shriramana,

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

If it is a 64-bit-floating-point data type, then isn't one supposed to use the "long" modifier, i.e. "%.52f" -> "%.52lf"? Or is that not portable?

I am not concerned so much about being portable. However, I have always followed the idea that "%f" is for 4 Byte floats and "%lf" is for 8 Byte floats or doubles...

Is that compiler specific or ????

~ Nate

"Do or do not or delegate to somebody else. There is no try."

#8 lore raw
In reply to Steve Graegert
Quoted 9 lines · Steve Graegert

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

Steve Graegert <[email protected]>

Ummm, I'm no expert and in fact completely out of my element here, but how would 52 bits yield 52 decimal numerals following the decimal point? Wouldn't it be true that 52 bits could represent at most an integer of 2^52 (4503599627370496)? So I am at a loss as to how such a format could accurately represent more than 16 significant decimal digits....

Scott Swanson

#4 lore raw
In reply to Nate Jenkins
Quoted 48 lines · Nate Jenkins
Quoted 29 lines
Quoted 20 lines

Please observe the following printf output of eighteen double variables:

First I tried using "%20.15f" as the format string:

268.229220252114942 1.018511211250181
66.833038547989787 12.450505533056681
20.512170161382819 0.334923179252419
259.358845955701327 1.571934717666495
201.111247720862963 0.139476171219810

 270.658029698352152   -0.612280599380678
 105.175902698424665   -0.076345132989225
 343.809699615507384   -0.219206370786681

Then I used "%30.25f":

268.2297152740335377529845573 1.0185111507334088098986058
66.8390907719954157073516399 12.4504269834231084956854829
20.5123328842112115921736404 0.3349264346316725426966343
259.3596100022817267927166540 1.5719383906021777708161835
201.1113154335791932680876926 0.1394751696317115263745734

Quoted 7 lines
 270.6577319741974747557833325   -0.6122834912138217511312632
 105.1758654981413201312534511   -0.0763454696609412730712307
 343.8095929658045974974811543   -0.2192182978826520134418843

Ignoring the small differences in the two sets of data (longitude and speed of eight celestial bodies computer at two different instants) I wonder - if I keep increasing the number of decimal places where will it end?

Quoted 9 lines

Shriramana,

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

If it is a 64-bit-floating-point data type, then isn't one supposed to use the "long" modifier, i.e. "%.52f" -> "%.52lf"? Or is that not portable?

I am not concerned so much about being portable. However, I have always followed the idea that "%f" is for 4 Byte floats and "%lf" is for 8 Byte floats or doubles...

Is that compiler specific or ????

~ Nate

Quoted 9 lines

"Do or do not or delegate to somebody else. There is no try."

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

According to man 3 printf, we have:
"
l (ell) A following integer conversion corresponds to a long int or unsigned long int argument, or a following n
conversion corresponds to a pointer to a long int argument, or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument.

ll (ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument,
or a following n conversion corresponds to a pointer to a long long int argument.
"
But there are OS-es that do not recognize the ll.
Today I had a small problem displaying the fields of a "struct stat" variable. It was all messed up, until i saw that dev_t is declared as "unsigned long long". So I did
printf( "%llu %u %u %u", st.st_dev, st.st_inode, st.st_size, st.st_atime );

M.D.

Signature · 2 lines
This message was scanned for spam and viruses by BitDefender.
For more information please visit http://www.bitdefender.com/
#9 lore raw
In reply to Scott
Quoted 9 lines · Scott
Quoted 8 lines · Steve Graegert

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

Quoted 8 lines
Quoted 1 line

Steve Graegert <[email protected]>

Ummm, I'm no expert and in fact completely out of my element here, but how would 52 bits yield 52 decimal numerals following the decimal point? Wouldn't it be true that 52 bits could represent at most an integer of 2^52 (4503599627370496)? So I am at a loss as to how such a format could accurately represent more than 16 significant decimal digits....

As usual, brain engages immediately *after* pressing 'send'. Wouldn't it be more accurate to say that the format can represent:

(2^52)*(2^11) = 323170060713110073007148766886699519604441026697154840321303454275246\
551388678908931972014115229134636887179609218980194941195591504909210\
950881523864482831206308773673009960917501977503896521067960576383840\
675682767922186426197561618380943384761704705816458520363050428875758\
915410658086075523991239303855219143333896683424206849747865645694948\
561760353263220580778056593310261927084603141502585928641771167259436\
037184618573575983511523016459044036976132332872312271256847108202097\
251571017269313234696785425806566979350459972683529986382155251663894\
37335543602135433229604645318478604952148193555853611059596230656

Which is just slightly more than 52 decimal places....

Or am I way off base here...?

Scott Swanson

#10 lore raw
In reply to Scott
Quoted 9 lines · Scott
Quoted 8 lines · Steve Graegert

Double precision numbering format is standardized by IEEE 754 with an 8 byte encoding. 1 bit is used for the sign, 11 bits for the exponent and the remaining 52 bits are used for the precision, which means precision "ends" at %.52f.

\Steve

Quoted 8 lines
Quoted 1 line

Steve Graegert <[email protected]>

Ummm, I'm no expert and in fact completely out of my element here, but how would 52 bits yield 52 decimal numerals following the decimal point? Wouldn't it be true that 52 bits could represent at most an integer of 2^52 (4503599627370496)? So I am at a loss as to how such a format could accurately represent more than 16 significant decimal digits....

You're perfectly right, but that's not what I meant. I just jumped on the printf analogy to make clear that it does not _print_ any numbers (except zero) beyond %.52f. From this POV printf precision "ends" there, although we know that such numbers are not accurately displayed beyond 16 significant digits.

\Steve

--

Steve Graegert <[email protected]>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

#5 lore raw
In reply to Nate Jenkins
Quoted 2 lines · Nate Jenkins

If it is a 64-bit-floating-point data type, then isn't one supposed to use the "long" modifier, i.e. "%.52f" -> "%.52lf"? Or is that not portable?

You cannot pass a float to printf(); a float will automatically be cast to double.

More generally, a float argument will be cast to double if the function doesn't have a prototype, or if the prototype doesn't specify a type for the argument (i.e. for a variadic function). Similarly, char and short values are automatically converted to int in such circumstances.

Signature · 1 line
Glynn Clements <[email protected]>
#6 lore raw
Quoted 7 lines
Quoted 2 lines · Nate Jenkins

If it is a 64-bit-floating-point data type, then isn't one supposed to use the "long" modifier, i.e. "%.52f" -> "%.52lf"? Or is that not portable?

Quoted 12 lines

You cannot pass a float to printf(); a float will automatically be cast to double.

More generally, a float argument will be cast to double if the function doesn't have a prototype, or if the prototype doesn't specify a type for the argument (i.e. for a variadic function). Similarly, char and short values are automatically converted to int in such circumstances.

Signature · 1 line
Glynn Clements <[email protected]>

So for a short example:
(this should compile but with a few casting warnings)

<.......code.......>

float fNum = 3.1415926535897932384626433832795; // will get truncated double dNum = fNum; // will get some extra garbage trailing char cNum = '*'; // 42 in ASCII
short sNum = cNum;
int nNum = cNum;

//////////// floating point number types ////////////

printf("%f \n", fNum);
// IIRC, 7 digits --> "3.141593

printf("%f \n", dNum);
// IIRC, 15 digits --> "3.14159200000261"
// or something like that due to extra garbage
// trailing without formatting?

printf("%lf \n", dNum);
// same --> "3.14159200000261" Should not work but it does?

//////////// integer number types ////////////

printf("%c \n", cNum);
printf("%c \n", (char)(sNum));
printf("%c \n", (char)(nNum));
// so all of these would show --> "*" ?

printf("%hi \n", cNum);
printf("%hi \n", sNum);
printf("%hi \n", (short)(nNum));
// so all of these would show --> "42" ?

printf("%d \n", cNum);
printf("%d \n", sNum);
printf("%d \n", nNum);
// so all of these would show --> "42" ?

<......./code.......>

Are my comment assumptions wrong? I have been using "%lf" for years... Could the old compiler I use be made to consider "%lf" the same as "%f" ?

Nate

#7 lore raw
Quoted 7 lines · Nate Jenkins
Quoted 7 lines
Quoted 3 lines

If it is a 64-bit-floating-point data type, then isn't one supposed to use
the "long" modifier, i.e. "%.52f" -> "%.52lf"? Or is that not portable?

You cannot pass a float to printf(); a float will automatically be cast to double.

Quoted 8 lines
Quoted 5 lines

More generally, a float argument will be cast to double if the function doesn't have a prototype, or if the prototype doesn't specify a type for the argument (i.e. for a variadic function). Similarly, char and short values are automatically converted to int in such circumstances.

So for a short example:
(this should compile but with a few casting warnings)

Regarding the lack of trailing garbage: the default precision for "%f" is 6 decimal places, i.e. "%.6f". If you use e.g. "%.50f", you will get:

	3.14159274101257324218750000000000000000000000000000 

in all three cases. Everything after the 6th decimal place is garbage due to the value having been truncated to float precision in the first place. It doesn't matter whether the conversion from float to double is done by copying fNum to dNum or by passing fNum to printf().

Quoted 2 lines

Are my comment assumptions wrong? I have been using "%lf" for years... Could the old compiler I use be made to consider "%lf" the same as "%f" ?

Technically, "%lf" is undefined for printf() etc (although it is meaningful for sscanf() etc). In practice, most implementations simply ignore the "l", so "%lf" and "%f" are equivalent.

Signature · 1 line
Glynn Clements <[email protected]>