* Double values - what precision do I use for fprintf? @ 2006-01-12 12:30 Shriramana Sharma 2006-01-12 14:17 ` Patrick Leslie Polzer 2006-01-12 18:51 ` Steve Graegert 0 siblings, 2 replies; 11+ messages in thread From: Shriramana Sharma @ 2006-01-12 12:30 UTC (permalink / raw) To: linux-c-programming 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? -- 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 12:30 Double values - what precision do I use for fprintf? Shriramana Sharma @ 2006-01-12 14:17 ` Patrick Leslie Polzer 2006-01-12 18:51 ` Steve Graegert 1 sibling, 0 replies; 11+ messages in thread From: Patrick Leslie Polzer @ 2006-01-12 14:17 UTC (permalink / raw) To: Shriramana Sharma; +Cc: linux-c-programming [-- Attachment #1: Type: text/plain, Size: 631 bytes --] On Thu, 12 Jan 2006 18:00:19 +0530 Shriramana Sharma <Shriramana Sharma <samjnaa@gmail.com>> 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 -- gpg --keyserver pgp.mit.edu --recv-keys 0x52D70289 [-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 12:30 Double values - what precision do I use for fprintf? Shriramana Sharma 2006-01-12 14:17 ` Patrick Leslie Polzer @ 2006-01-12 18:51 ` Steve Graegert 2006-01-12 21:22 ` Nate Jenkins 2006-01-12 21:57 ` Scott 1 sibling, 2 replies; 11+ messages in thread From: Steve Graegert @ 2006-01-12 18:51 UTC (permalink / raw) To: linux-c-programming On 1/12/06, Shriramana Sharma <samjnaa@gmail.com> wrote: > 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? 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 <graegerts@gmail.com> Software Consultant {C/C++ && Java && .NET} Office: +49 9131 7123988 Mobile: +49 1520 9289212 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 18:51 ` Steve Graegert @ 2006-01-12 21:22 ` Nate Jenkins 2006-01-12 22:00 ` Mihai Dontu 2006-01-13 23:47 ` Glynn Clements 2006-01-12 21:57 ` Scott 1 sibling, 2 replies; 11+ messages in thread From: Nate Jenkins @ 2006-01-12 21:22 UTC (permalink / raw) To: linux-c-programming >> 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? > > 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." ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 21:22 ` Nate Jenkins @ 2006-01-12 22:00 ` Mihai Dontu 2006-01-13 23:47 ` Glynn Clements 1 sibling, 0 replies; 11+ messages in thread From: Mihai Dontu @ 2006-01-12 22:00 UTC (permalink / raw) To: linux-c-programming Nate Jenkins wrote: >>> 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? >> >> >> 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." > > > - > To unsubscribe from this list: send the line "unsubscribe > linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > 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. -- This message was scanned for spam and viruses by BitDefender. For more information please visit http://www.bitdefender.com/ ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 21:22 ` Nate Jenkins 2006-01-12 22:00 ` Mihai Dontu @ 2006-01-13 23:47 ` Glynn Clements 2006-01-14 0:35 ` Nate Jenkins 1 sibling, 1 reply; 11+ messages in thread From: Glynn Clements @ 2006-01-13 23:47 UTC (permalink / raw) To: Nate Jenkins; +Cc: linux-c-programming Nate Jenkins wrote: > 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. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-13 23:47 ` Glynn Clements @ 2006-01-14 0:35 ` Nate Jenkins 2006-01-17 5:37 ` Glynn Clements 0 siblings, 1 reply; 11+ messages in thread From: Nate Jenkins @ 2006-01-14 0:35 UTC (permalink / raw) To: Glynn Clements; +Cc: linux-c-programming > > Nate Jenkins wrote: > >> 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. > > -- > Glynn Clements <glynn@gclements.plus.com> > 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-14 0:35 ` Nate Jenkins @ 2006-01-17 5:37 ` Glynn Clements 0 siblings, 0 replies; 11+ messages in thread From: Glynn Clements @ 2006-01-17 5:37 UTC (permalink / raw) To: Nate Jenkins; +Cc: linux-c-programming Nate Jenkins wrote: > >> 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. > > 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(). > 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. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 18:51 ` Steve Graegert 2006-01-12 21:22 ` Nate Jenkins @ 2006-01-12 21:57 ` Scott 2006-01-12 22:06 ` Scott 2006-01-13 5:25 ` Steve Graegert 1 sibling, 2 replies; 11+ messages in thread From: Scott @ 2006-01-12 21:57 UTC (permalink / raw) To: linux-c-programming On Thu, Jan 12, 2006 at 07:51:08PM +0100, Steve Graegert wrote: > > 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 <graegerts@gmail.com> 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 21:57 ` Scott @ 2006-01-12 22:06 ` Scott 2006-01-13 5:25 ` Steve Graegert 1 sibling, 0 replies; 11+ messages in thread From: Scott @ 2006-01-12 22:06 UTC (permalink / raw) To: linux-c-programming On Thu, Jan 12, 2006 at 02:57:29PM -0700, Scott wrote: > On Thu, Jan 12, 2006 at 07:51:08PM +0100, Steve Graegert wrote: > > > > 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 <graegerts@gmail.com> > > 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Double values - what precision do I use for fprintf? 2006-01-12 21:57 ` Scott 2006-01-12 22:06 ` Scott @ 2006-01-13 5:25 ` Steve Graegert 1 sibling, 0 replies; 11+ messages in thread From: Steve Graegert @ 2006-01-13 5:25 UTC (permalink / raw) To: linux-c-programming On 1/12/06, Scott <drmemory@3rivers.net> wrote: > On Thu, Jan 12, 2006 at 07:51:08PM +0100, Steve Graegert wrote: > > > > 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 <graegerts@gmail.com> > > 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 <graegerts@gmail.com> Software Consultant {C/C++ && Java && .NET} Office: +49 9131 7123988 Mobile: +49 1520 9289212 ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2006-01-17 5:37 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2006-01-12 12:30 Double values - what precision do I use for fprintf? Shriramana Sharma 2006-01-12 14:17 ` Patrick Leslie Polzer 2006-01-12 18:51 ` Steve Graegert 2006-01-12 21:22 ` Nate Jenkins 2006-01-12 22:00 ` Mihai Dontu 2006-01-13 23:47 ` Glynn Clements 2006-01-14 0:35 ` Nate Jenkins 2006-01-17 5:37 ` Glynn Clements 2006-01-12 21:57 ` Scott 2006-01-12 22:06 ` Scott 2006-01-13 5:25 ` Steve Graegert
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).