/** *************************************************************************** * @remark exploring the storage of a decimal number as a double * * * * for easy reference, this program begins with a 50-digit decimal * * 0.12345678901234567890123456789012345678901234567890 * * * * the true valued is stored in a double and printed * * to various decimal places, * * * * @author Henry M. Walker * * @file double-storage.c * * @date Summer, 2023 * * * * @remark References * * @remark Consequences of Data Representation on Programming * https://blue.cs.sonoma.edu/~hwalker/courses/415-sonoma.sp25/readings/data-rep-consequences.php * * *****************************************************************************/ #include #include int main ( ) { char * decimalString = "0.1234567890123456789012345678901234567890 "; // digits for easy counting //char * decimalString = "0.2424242424242424242424242424242424242424" ; // all digits < 5 //char * decimalString = "0.6868686868686868686868686868686868686868" ; // all digits > 5 printf ("Program to explore the storage of a 40-digit decimal number\n"); printf ("original number (stored as a sring) %s\n", decimalString); double decimalDouble = 0.1234567890123456789012345678901234567890 ; //double decimalDouble = 0.2424242424242424242424242424242424242424 ; //double decimalDouble = 0.6868686868686868686868686868686868686868 ; printf ("double variable 'decimalDouble' initialized to 40 decimal places\n"); printf ("printing of double variable to successive decimal places\n"); printf ("decimal places of double, with 15th decimal place marked (^)\n"); printf ("places actual decimal\n"); char formatString [30] = {0}; for (int i = 11; i <= 25; i++) { // format and double to i decimal places sprintf (formatString, "%%.%dlf", i); printf ("%3d (double) ", i); printf (formatString, decimalDouble); printf ("\n"); // print i+1 decimal places of actual pi string char decimalSubstring [30] = {0}; strncpy(decimalSubstring, decimalString, i+2); printf (" (actual) %s\n", decimalSubstring); // highlight 15th decimal place with '^' character printf (" %15c\n\n", '^'); } return 0; }