msvcrt: New implementation of fcvt.

This commit is contained in:
Karl Relton 2007-09-24 20:10:36 +01:00 committed by Alexandre Julliard
parent 6f9bcd59be
commit 260fd8a4a9
2 changed files with 149 additions and 35 deletions

View File

@ -873,16 +873,81 @@ char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign ) char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
{ {
thread_data_t *data = msvcrt_get_thread_data(); thread_data_t *data = msvcrt_get_thread_data();
char *dec; int stop, dec1, dec2;
char *ptr1, *ptr2, *first;
char buf[80]; /* ought to be enough */
if (!data->efcvt_buffer) if (!data->efcvt_buffer)
data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */ data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
snprintf(data->efcvt_buffer, 80, "%.*e", ndigits, number); if (number < 0)
*sign = (number < 0); {
dec = strchr(data->efcvt_buffer, '.'); *sign = 1;
*decpt = (dec) ? dec - data->efcvt_buffer : -1; number = -number;
return data->efcvt_buffer; } else *sign = 0;
snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
ptr1 = buf;
ptr2 = data->efcvt_buffer;
first = NULL;
dec1 = 0;
dec2 = 0;
/* For numbers below the requested resolution, work out where
the decimal point will be rather than finding it in the string */
if (number < 1.0 && number > 0.0) {
dec2 = log10(number + 1e-10);
if (-dec2 <= ndigits) dec2 = 0;
}
/* If requested digits is zero or less, we will need to truncate
* the returned string */
if (ndigits < 1) {
stop = strlen(buf) + ndigits;
} else {
stop = strlen(buf);
}
while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
while (*ptr1 != '\0' && *ptr1 != '.') {
if (!first) first = ptr2;
if ((ptr1 - buf) < stop) {
*ptr2++ = *ptr1++;
} else {
ptr1++;
}
dec1++;
}
if (ndigits > 0) {
ptr1++;
if (!first) {
while (*ptr1 == '0') { /* Process leading zeroes */
*ptr2++ = *ptr1++;
dec1--;
}
}
while (*ptr1 != '\0') {
if (!first) first = ptr2;
*ptr2++ = *ptr1++;
}
}
*ptr2 = '\0';
/* We never found a non-zero digit, then our number is either
* smaller than the requested precision, or 0.0 */
if (!first) {
if (number > 0.0) {
first = ptr2;
} else {
first = data->efcvt_buffer;
dec1 = 0;
}
}
*decpt = dec2 ? dec2 : dec1;
return first;
} }
/*********************************************************************** /***********************************************************************

View File

@ -552,47 +552,96 @@ static void test_fcvt(void)
char *str; char *str;
int dec=100, sign=100; int dec=100, sign=100;
/* Numbers less than 1.0 with different precisions */
str = _fcvt(0.0001, 1, &dec, &sign ); str = _fcvt(0.0001, 1, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
ok( 0 == strcmp(str,""), "bad return\n"); ok( -3 == dec, "dec wrong %d\n", dec);
ok( -3 == dec, "dec wrong\n"); ok( 0 == sign, "sign wrong\n");
}
ok( 0 == sign, "dec wrong\n");
str = _fcvt(0.0001, -10, &dec, &sign ); str = _fcvt(0.0001, -10, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
ok( 0 == strcmp(str,""), "bad return\n"); ok( -3 == dec, "dec wrong %d\n", dec);
ok( -3 == dec, "dec wrong\n"); ok( 0 == sign, "sign wrong\n");
}
ok( 0 == sign, "dec wrong\n");
str = _fcvt(0.0001, 10, &dec, &sign ); str = _fcvt(0.0001, 10, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str);
ok( 0 == strcmp(str,"1000000"), "bad return\n"); ok( -3 == dec, "dec wrong %d\n", dec);
ok( -3 == dec, "dec wrong\n"); ok( 0 == sign, "sign wrong\n");
}
ok( 0 == sign, "dec wrong\n");
/* Basic sign test */
str = _fcvt(-111.0001, 5, &dec, &sign ); str = _fcvt(-111.0001, 5, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
ok( 0 == strcmp(str,"11100010"), "bad return\n"); ok( 3 == dec, "dec wrong %d\n", dec);
ok( 3 == dec, "dec wrong\n"); ok( 1 == sign, "sign wrong\n");
}
ok( 1 == sign, "dec wrong\n");
str = _fcvt(111.0001, 5, &dec, &sign ); str = _fcvt(111.0001, 5, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
ok( 0 == strcmp(str,"11100010"), "bad return\n");
ok( 3 == dec, "dec wrong\n"); ok( 3 == dec, "dec wrong\n");
} ok( 0 == sign, "sign wrong\n");
ok( 0 == sign, "dec wrong\n");
/* 0.0 with different precisions */
str = _fcvt(0.0, 5, &dec, &sign ); str = _fcvt(0.0, 5, &dec, &sign );
todo_wine { ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str);
ok( 0 == strcmp(str,"00000"), "bad return\n"); ok( 0 == dec, "dec wrong %d\n", dec);
ok( 0 == dec, "dec wrong\n"); ok( 0 == sign, "sign wrong\n");
}
ok( 0 == sign, "dec wrong\n"); str = _fcvt(0.0, 0, &dec, &sign );
ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
ok( 0 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
str = _fcvt(0.0, -1, &dec, &sign );
ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
ok( 0 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
/* Numbers > 1.0 with 0 or -ve precision */
str = _fcvt(-123.0001, 0, &dec, &sign );
ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str);
ok( 3 == dec, "dec wrong %d\n", dec);
ok( 1 == sign, "sign wrong\n");
str = _fcvt(-123.0001, -1, &dec, &sign );
ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str);
ok( 3 == dec, "dec wrong %d\n", dec);
ok( 1 == sign, "sign wrong\n");
str = _fcvt(-123.0001, -2, &dec, &sign );
ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
ok( 3 == dec, "dec wrong %d\n", dec);
ok( 1 == sign, "sign wrong\n");
str = _fcvt(-123.0001, -3, &dec, &sign );
ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
ok( 3 == dec, "dec wrong %d\n", dec);
ok( 1 == sign, "sign wrong\n");
/* Numbers > 1.0, but with rounding at the point of precision */
str = _fcvt(99.99, 1, &dec, &sign );
ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str);
ok( 3 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
/* Numbers < 1.0 where rounding occurs at the point of precision */
str = _fcvt(0.00636, 2, &dec, &sign );
ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
ok( -1 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
str = _fcvt(0.00636, 3, &dec, &sign );
ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str);
ok( -2 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
str = _fcvt(0.09999999996, 2, &dec, &sign );
ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str);
ok( 0 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
str = _fcvt(0.6, 0, &dec, &sign );
ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
ok( 1 == dec, "dec wrong %d\n", dec);
ok( 0 == sign, "sign wrong\n");
} }
START_TEST(printf) START_TEST(printf)