Hello Jose,
Attaching herewith *"routine.c".
*
It contains an implementation of pdf_fp_real_to_string().
Kindly review it and let me know if it is OK to merge.
NOTE : You can compile it using command *"$ gcc routine.c"*
Regards
*Bhaskar*
On Mon, Jul 26, 2010 at 5:43 PM, Jose E. Marchesi <[email protected]> wrote:
>
> *Regarding "pdf_fp_real_to_string ()",*
>
> *- If the precision is 0, we need to take the max precision.
> My question is we need to find the precision from the number given or
> do
> we have a macro defined for max precision ?*
>
> Since we are using float variables to implement pdf_realt_t, we can
> use the FLT_DIG constant from float.h as the maximum precision.
>
> FLT_DIG
>
> This is the number of decimal digits of precision for the float
> data type. Technically, if p and b are the precision and base
> (respectively) for the representation, then the decimal precision
> q is the maximum number of decimal digits such that any floating
> point number with q base 10 digits can be rounded to a floating
> point number with p base b digits and back again, without change
> to the q decimal digits.
>
> The value of this macro is supposed to be at least 6, to satisfy ISO C.
>
> BTW, it would good to make to make it clear in the manual what happens
> if the requested precision is bigger than the max precision.
> Something like:
>
> @item precision
> The number of decimal digits to include in the textual representation.
> The maximum precision is used if the requested precision is @code{0}
> or if it exceeds the maximum precision.
>
> --
> Jose E. Marchesi [email protected]
> GNU Project http://www.gnu.org
>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <float.h>
int double_to_string(double number, char* buffer, int size, int precision)
{
if(precision >= size-2)
return -1;
char *str = NULL;
int radixPosition = 0;
int sign = 0;
int length = 0, currentLength = 0;
int i = 0;
if(precision == 0)
precision = FLT_DIG; /* Maximum floating point precision */
str = fcvt(number, precision, &radixPosition, &sign);
printf("%s\n", str);
printf("%d\n", radixPosition);
printf("%d\n", sign);
while(str[i++] != '\0');
length = i - 1;
length++; /* For decimal point */
if(sign)
{
length++; /* For the sign */
strcat(buffer, "-");
currentLength++;
}
if(radixPosition == 0)
{
length++; /* For leading zero */
strcat(buffer, "0.");
currentLength += 2;
}
printf("%d\n", length);
if(length > size)
return -1;
if(currentLength >= 2)
{
strcat(buffer, str);
}
else
{
strncat(buffer, str, radixPosition);
strcat(buffer, ".");
currentLength = currentLength + radixPosition + 1;
for(i = radixPosition; i < strlen(str); i++)
buffer[currentLength++] = str[i];
}
/*printf("%s\n", buffer);*/
return 0;
}
int main()
{
char *buff = NULL;
buff = (char*)malloc(15);
double_to_string(-0.3228377646, buff, 14, 12);
printf("%s\n", buff);
free(buff);
return 0;
}