In Linux, when I use printf("%1.22f\n", (double)0.12345678901234567890) I get about 17 decimal places worth of precision, on mine I get about 7 decimal places of precision. I typecast on both Linux and my OS to (double) to ensure that one isn't using "float" or "long double".
Here's my code for printing the float (and compensate for 2 digits worth of rounding), it's a mish-mash of different OSS/PD code.
Code: Select all
static void print_double(double d, size_t precision)
{
double di, df, pr, tmp;
/* if it's zero, just print the '0' and exit */
if(d == 0.0)
{ print_char('0'); return; }
/* if it's a negative integer, print '-' and make the integer absolute */
else if (d < 0)
{
print_char('-');
d = fabs(d);
}
/* break the double into fraction (df) and integer (di) parts */
df = modf(d, &di);
/* print the integer portion */
print_int((size_t)floor(di));
/* round up if necessary */
if (df != 0)
{
print_char('.');
pr = df * pow(10, precision+1);
modf(pr, &pr);
/* we need to extract the last+1 decimal place and see if we should round up */
while(pr > 10.0){ pr /= 10; }
/* if it's 4, we should check the next decimal place to see if it's >= 5 */
if(pr == 4)
{
pr = df * pow(10, precision+2);
modf(pr, &pr);
while(pr > 10.0){ pr /= 10; }
if(pr >= 5){ df = df + (0.1 / pow(10, precision)); }
}
if(pr >= 5){ df = df + (0.1 / pow(10, precision)); }
}
/* calculate the fractional portion to print */
while (df != 0 && precision--)
{
df = df * 10;
df = modf(df, &di);
print_int((int)di);
}
}
Any ideas?