[PD] why does PD round numbers? (in tables, in messageboxes, etc)

Martin Peach martin.peach at sympatico.ca
Mon Apr 9 06:07:54 CEST 2012


On 2012-04-08 20:45, Hans-Christoph Steiner wrote:
>
> On Apr 8, 2012, at 3:17 PM, katja wrote:
>
>> On Sun, Apr 8, 2012 at 8:43 PM, Hans-Christoph Steiner<hans at at.or.at>  wrote:
>>>
>>> The main reason why this is still like this is because no one has written better code, then done thorough testing in order to prove that the new code doesn't break anything.  People have written better code for this before, no one has done the thorough testing part...
>>
>> Hans, can you point to such code, is there still something available
>> somewhere? I may be able to test it using the testtools templates.
>> Such code could be a very useful precision improvement, even though
>> it's not the same as double precision.
>
> Hmm, can't think of any off hand, but its not too hard.  My thought would be to sprintf("%.06f"), then strip off trailing zeros and decimal points.  The rounding stuff is harder, but I am not sure that the current sprintf("%g") handles the rounding any differently:
>
> (from man 3 printf)
>       gG      The double argument is converted in style f or e (or F or E for G
>               conversions).  The precision specifies the number of significant
>               digits.  If the precision is missing, 6 digits are given; if the
>               precision is zero, it is treated as 1.  Style e is used if the expo-
>               nent from its conversion is less than -4 or greater than or equal to
>               the precision.  Trailing zeros are removed from the fractional part
>               of the result; a decimal point appears only if it is followed by at
>               least one digit.

I get this on WinXP:

float pi as %g: 3.14159 as %f: 3.141593 as  %0.24f: 
3.141592741012573200000000
double pi as %g: 3.14159 as %f: 3.141593 as %0.24f: 
3.141592653589793100000000
Pi to 24 places: 3.141592653589793238462643.

using this code:

#include <stdio.h>
#include<stdlib.h>
#include <math.h>

int main (void)
{
     float fpi = 4.0*atan(1.0);
     double dpi = 4.0*atan(1.0);

     fprintf(stdout, "float pi as %%g: %g as %%f: %f as  %%0.24f: 
%0.24f\n", fpi, fpi, fpi);
     fprintf(stdout, "double pi as %%g: %g as %%f: %f as %%0.24f: 
%0.24f\n", dpi, dpi, dpi);
     fprintf(stdout, "Pi to 24 places: 3.141592653589793238462643.\n");
     exit (EXIT_SUCCESS);
}


Martin



More information about the Pd-list mailing list