[sac-user] print function alternatives?

Robert Bernecky bernecky at snakeisland.com
Mon Aug 2 19:45:16 CEST 2010


The ArrayFormat module, like several others in the SAC
libraries, suffers from the shoemaker's children's problem -
it was never completed to my satisfaction, so much of its
functionality is crippled or missing.

In particular, it still uses printf(), which does not
seem to do a very good job of formatting numbers,
or at least it has not done so in the past.
My intent was to make it use a precise formatter, such
as that proposed in "Printing Floating-Point Numbers Quickly and 
Accurately", by Robert G. Burger and R. Kent Dybvig.

The basic idea behind these formatters is relatively simple:
There is a set of real numbers that, if expressed in decimal
form, will map into the same machine double/float number.
Since there is no way to determine the provenance of
that machine number, there is no inherent reason to
choose one of those numbers over another as the "correct"
formatted value. Hence, we pick the number that
has the shortest representation. E.g., we would
pick 0.07 over 0.69999999999999.

Replacing sprintf with such a function
is still on my to-do list...

The other tasks that ArrayFormat is intended to perform are:

- printing higher-rank arrays in a tabular form that makes the
   array structure visible, aligns decimal points, etc.

- printing Booleans as 0 and 1.

- eliminates the array shape pictures that surround print()

At any rate, I make no guarantees, but you can try this:

use Array:all;
use StdIO:all;
use ArrayFormat:all;

int main()
  x = 0.07;
  show(format(x,18)); /* Format x to a precision of 18 decimal places */

I'd try that out on my box here to see if it works, but I am
still awaiting resolution of Bug #734, as I can't compile
much of anything until it gets resolved.

If you want to email me a sample of the offending code,
with directions on how to invoke (and break) it, I'll
take a look at it. Eventually...


ps; There is a PLDI2010 paper on formatting that
may be germane here; I'll look at that later today.

Herhut, Stephan wrote:
> Dear Michael,
> we have a module called ArrayFormat that was contributed by Robert 
> Bernecky. It uses a formatting scheme das guarantees not to loose 
> precision or, if precision is lost, to at least make this visible in the 
> output. I do not know the current state of that module, though. Maybe 
> Bob can be of help here.
> With respect to your request: This feature could certainly be made 
> available. I checked the implementation in the standard library and the 
> current implementation uses "%4f " for floats. However, I would prefer a 
> more generic solution as implemented by ArrayFormat. I will bring this 
> up during our next developer’s meeting.
> Stephan
> On 02/08/2010 17:20, "Bullington, Michael" <michael.bullington at ttu.edu> 
> wrote:
>     To whom it shall concern:
>     I was tracing some logic errors on a program that would perform
>     Singular Value Decomposition (SVD) and was using the print function
>     to print values in tracking the logic errors.  However, I had to
>     resort to more “standard” printf functions using %30.15f format
>     strings (up to 15 decimal places) to discover I had computation
>     round-off errors.  The round-off errors became extremely deceptive
>     when the print function was used in tracking the errors because the
>     rounding gave output the appearance of correctness.  The print
>     function in SAC seems to just use the “standard” %f format string
>     which has a default of 6 decimal places.
>     Is there an alternative (overloaded) form of the print function in
>     SAC where I can control some of the output formatting and not have
>     to resort to printf?
> -- 
> Stephan Herhut
> Centre for Computer Science and Informatics Research
> Science and Technology Research Institute
> University of Hertfordshire

More information about the sac-user mailing list