2

I am trying to implement http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/

I have read through it quite a few times and feel very comfortable with it. The first step is assuming the value is in a string format. My implementation is assuming that the value is in a double format.

Is it possible to obtain the number of digits that make up the fractional portion of a float/double?

For example, the value 3.24325 (I hope that cannot be represented exactly in binary, because I just randomly chose it). I would want to know that the decimal portion is 5 digits long so that I can continue with the algorithm in the link above.

Using something like modf results in what appears to be a correct fractional value at first glance, but its actually rounded (assuming the fractional portion cannot be exactly represented in binary). Subtracting a typecasted int of the original value from the original value results in the same rounded decimal. Recording the value in a string with sprintf results in a similar issue (I believe), but I havent had enough time to confirm that - I just started playing with it.

Is there any solution to step 1 of the algorithm in the link above?

Thank you.

halexh
  • 3,021
  • 3
  • 19
  • 19
  • You may be interested in [this answer](http://stackoverflow.com/a/8455604/968261). – Alexey Frunze Jan 22 '13 at 20:50
  • 3
    As you know, you cannot have the value 3.24325 in a double. The closest you can typically have is 3.243250000000000188293824976426549255847930908203125. What do you want your program to do when given this value? Should it figure that all the digits are correct, that it was given exactly the value it should work on? Or should it figure that only the first ten digits should be used, that the value it was given should be rounded to 10 decimal places (counting from the decimal point) or perhaps to 11 significant digits (counting from the first digit) or something else? – Eric Postpischil Jan 22 '13 at 21:36
  • I don't understand. If your input is already a double, why do you need to use a "convert to double" algorithm? – Rick Regan Jan 23 '13 at 14:16
  • @RickRegan I was attempting to use this algorithm as a guideline for converting from double to a different double format. I have since realized that this method was way over the top, and unnecessary for what I was doing. – halexh Jan 30 '13 at 18:13

2 Answers2

0

One option is to use sprintf with strchr. Below is one example:

int main() {
    double d = 3.24325;
    char str[20];
    sprintf(str, "%.10f", d); 
    puts(str); 
    int digits = strchr(str, '0') - strchr(str, '.') - 1;
    printf("%d", digits);
}

Output

3.2432500000
5

In the above code, strchr search for the positions of . and (first trailing) 0, and calculate the digits based on it. For simplicity of demonstration, I have appended some extra 0s with %.10f. You can instead adjust the calculation for the case where there is no trailing 0, by comparing whether the strchr returns pointer to the end of the string.

Masked Man
  • 1
  • 7
  • 40
  • 80
0

Following is a naive approach to problem. In most case this works but errors may result from floating point integer representation.

#include <stdio.h>
#include <math.h>
    #define eps 0.000001
    int main(){

        double inp=3.12456789;

        int fdigits=0;
        while(fabs(round(inp)-inp)>eps){
            printf("dif %f\n",fabs(round(inp)-inp));
            inp=inp*10;
            fdigits++;

        }
        printf("%d",fdigits);
    return 0;
    }
woryzower
  • 956
  • 3
  • 15
  • 22