If not for this problem, does printf satisfy our requirements or are
there
any other problems with it? If it's OK, maybe we could just include
the
"standard" printf right into our codebase, instead of linking to a
library?
Just an idea.
Possibly, but if we do one ourselves we can be more clever with e.g. rounding behavior. Anyway, Bryce said he alraedy had some code somewhere, as I recall...
Yes, I do. I've added a task for myself to add this to the codebase.
The reason I ask this questions is because I see how Lauris is handling this in 0.33. Now instead of a single printf call, he declares a char[] buffer for each value, calls a custom conversion function, and then assembles these buffers into a string. That is extremely bulky and hard to read, and I would like to avoid this if possible, preserving the format string approach of printf. So, how close is your code to being a drop-in replacement for printf with its format strings?
_________________________________________________________________ Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2f...
On Sat, 13 Dec 2003, bulia byak wrote:
If not for this problem, does printf satisfy our requirements or are
there
any other problems with it? If it's OK, maybe we could just include
the
"standard" printf right into our codebase, instead of linking to a
library?
Just an idea.
Possibly, but if we do one ourselves we can be more clever with e.g. rounding behavior. Anyway, Bryce said he alraedy had some code somewhere, as I recall...
Yes, I do. I've added a task for myself to add this to the codebase.
The reason I ask this questions is because I see how Lauris is handling this in 0.33. Now instead of a single printf call, he declares a char[] buffer for each value, calls a custom conversion function, and then assembles these buffers into a string. That is extremely bulky and hard to read, and I would like to avoid this if possible, preserving the format string approach of printf. So, how close is your code to being a drop-in replacement for printf with its format strings?
The requirement I was following was to format numbers to be put into textedit boxes, so it doesn't seek to replace printf but rather just format numbers into strings. The idea is using it like this:
mywidget(FOO, BAR, ftos(1.0/42.0));
Docs for this follow:
// string ftos(double val[, char mode[, int sigfig[, int precision[, int options]]]]) // // DESCRIPTION // This routine is intended to replace the typical use of sprintf for // converting floating point numbers into strings. // // To one-up sprintf, an additional mode was created - 'h' mode - // which produces numbers in 'engineering notation' - exponents are // always shown in multiples of 3. To non-engineers this mode is // probably irrelevant, but for engineers (and scientists) it is SOP. // // One other new feature is an option to use 'x10^' instead of the // conventional 'E' for exponental notation. This is entirely for // aesthetics since numbers in the 'x10^' form cannot be used as // inputs for most programs. // // For most cases, the routine can simply be used with the defaults // and acceptable results will be produced. No fill zeros or trailing // zeros are shown, and exponential notation is only used for numbers // greater than 1e6 or less than 1e-3. // // The one area where sprintf may surpass this routine is in width control. // No provisions are made in this routine to restrict a number to a // certain number of digits (thus allowing the number to be constrained // to an 8 space column, for instance.) Along with this, it does not // support pre-padding a number with zeros (e.g., '5' -> '0005') and will // not post-pad a number with spaces (i.e., allow left-justification.) // // If width control is this important, then the user will probably want to // use the stdio routines, which really is well suited for outputting // columns of data with a brief amount of code. // // PARAMETERS // val - number to be converted // mode - can be one of four possible values. Default is 'g' // // e: Produces numbers in scientific notation. One digit // is shown on the left side of the decimal, the rest // on the right, and the exponential is always shown. // EXAMPLE: 1.04e-4 // // f: Produces numbers with fixed format. Number is shown // exact, with no exponent. // EXAMPLE: 0.000104 // // g: If val is greater than 1e6 or less than 1e-3 it will // be shown in 'e' format, otherwise 'f' format will be // used. // // h: Produces numbers in engineering format. Result is // identical to 'f' format for numbers between 1 and // 1e3, otherwise, the number is shown such that it // always begins with a nonzero digit on the left side // (unless number equals zero), and the exponential is // a multiple of 3. // EXAMPLE: 104e-6 // // If the mode is expressed as a capital letter (e.g., 'F') // then the exponential part of the number will also be // capitalized (e.g., '1E6' or '1X10^6'.) // // sigfig - the number of significant figures. These are the digits // that are "retained". For example, the following numbers // all have four sigfigs: // 1234 12.34 0.0001234 1.234e-10 // the last digit shown will be rounded in the standard // manner (down if the next digit is less than 5, up otherwise.) // // precision - the number of digits to show to the right of the decimal. // For example, all of the following numbers have precisions // of 2: // 1234.00 12.34 0.00 1.23e-10 123.40e-12 // // options - several options are allowed to control the look of the // output. // // FORCE_DECIMAL - require the decimal point to be shown for // numbers that do not have any fractional digits (or that // have a precision set to zero) // EXAMPLE: 1.e6 // FORCE_EXP_ZERO - pad the 10's zero in exponent if necessary // EXAMPLE: 1e06 // FORCE_HUNDRED_EXP_ZERO - pad the 100's zero in exponent if // necessary. Also pads 10's zero in exponent if necessary. // EXAMPLE: 1e006 // FORCE_EXP_PLUS - show the '+' in the exponent if exponent // is used. // EXAMPLE: 1e+6 // FORCE_EXP - force the output to display the exponent // EXAMPLE: 0e0 // FORCE_X10 - use x10^ instead of E // EXAMPLE: 1x10^6 // FORCE_PLUS - force output of the '+' for positive numbers // EXAMPLE: +1e6 // // Options can be combined using the usual OR method. For // example, // // ftos(123.456, 'f', -1, -1, FORCE_PLUS | FORCE_X10 | FORCE_EXP) // // gives "+123.456x10^0" // // RETURN VALUE // The string representation of the number is returned from the routine. // The ANSI C++ Standard "string" class was used for several important // reasons. First, because the string class manages it's own space, the // ftos routine does not need to concern itself with writing to unallocated // areas of memory or with handling memory reallocation internally. Second, // it allows return of an object, not a pointer to an object; this may not // be as efficient, but it is cleaner and safer than the alternative. Third, // the routine's return value can be directly assigned to a variable, i.e. // string var = ftos(3.1415); // which makes code much easier to comprehend and modify. // // Internally, the ftos routine uses fairly typical string operators (=, +=, // +, etc.) which pretty much any other flavor of string class will define as // well. Thus if one does not have access to the ANSI C++ Standard string // class, the user can substitute another with little difficulty. (If the // alternate class is not named "string" then redefine "string" to whatever // you wish to use. For example, // #define string CString // // November 1996 - Bryce Harrington // Created ftoa and ftos // // December 1996 - Bryce Harrington // Added engineering notation mode, added sigfig capability, added // significant debug code, added options, thoroughly debugged and // tested the code. // // // June 1999 - Bryce Harrington // Modified to run on Linux for WorldForge // // March 2003 - Bryce Harrington // Removed DTAG() macros - use of fprintf(stderr,...) instead // Broke out round/itos/ftos into separate files // Removed curses bits // /////////////////////////////////////////////////////////////////////// */
bulia byak wrote:
That is extremely bulky and hard to read, and I would like to avoid this if possible, preserving the format string approach of printf. So, how close is your code to being a drop-in replacement for printf with its format strings?
Given my druthers, I'd prefer to see something like the Taligent work on formatting that went into Java and ICU.
:-)
Although if it were only for decimal numbers, printf style might be ok.
participants (3)
-
Bryce Harrington
-
bulia byak
-
Jon A. Cruz