Slow week, boring life...

by Codewiz51 10. October 2008 22:54

I've been working on the test program for dcdflib this week. I have six more test routines to complete, plus I need to replace some timing code for reporting the speed of the calculations. The old code depended on Windows API time code.

I've run into some semi-interesting formatting problems with Console.WriteLine. If you use a specification similar to {0,-10,#0.0######} in a format string, the decimal points won't line up because the leading # is not filled in if the number does not take up 2 digits to the left of the decimal point. I was hoping the # would be replaced by a space, but no such luck. After doing some cursory research, I think a custom formatting function is required.

I want numbers to line up like this using Console.WriteLine:

 1.0234567   1.0234567
21.0111111  21.0111111

But all I've been able to achieve is the following:

1.0234567  1.0234567
21.0111111 21.0111111

Formatting strings need some way to specify that leading and trailing # symbols should be replaced with a space when a digit is not required.



Console Output and Numeric Formatting

by Codewiz51 6. October 2008 22:10

Not much emphasis is placed on console output when programming with a .Net language like c#. However, console output has a number of useful purposes when performing a variety of numeric calculations. The number one reason I like using console output is being able to redirect the output to a file. Another nice feature of console output is speed. There is very little overhead associated with dumping large amounts of data to the console. Coupled with I/O redirection in the command window, you can save yourself a lot of coding and debugging time.

I've been spending time learning the in's and out's of console output because I am porting some native FORTRAN and C++ code using cout. You can reproduce most of the FORTRAN and C++ output functionality with very little effort.

In native C++,  the console output fragment

cout << setw(10) << x
     << setw(10) << y
     << setw(10) << a
     << setw(10) << b
     << setw(14) << cdf_lookup
     << setw(14) << cdf_compute << endl;

might easily be translated to something like the following when porting to managed code:

Object[] o = { x, y, a, b, cdf_lookup, cdf_compute };
Console.WriteLine("{0,10:0.000000} {1,10:0.000000} {2,10:0.000000} {3,10:0.000000} {4,14:0.000000} {5,14:0.000000}", o);


It's worth it to take some time and experiment with formatting strings and console output.

For instance, if we change the output field {0,10:0.000000} to {0,-10:0.000000}, then the number will be left aligned when it is printed. Changing the custom format string to 0.0##### will cause the numeric output to display at least 1 digit past the decimal point; the runtime will use as many digits as necessary (up to six) for the decimal fraction if necessary. If the numeric field is right aligned, then decimal points may not line up properly when using a custom string like 0.0#####. It is generally better to use a format string like 0.000000 when printing rows of numbers where the decimal points should line up and right aligned formatting is used.

Tags: , , ,



This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011