Getting length of float

21.5k Views Asked by At

I am trying to get the length of a float, but it is saying the length is 8. I want to know how many digits there are. why is it saying that there are only 8 digits?

#include <stdio.h>

int main()
{
    double n = 72382.413651;
    int len;

    len = sizeof(n);
    printf("%d\n", len);
}

I am needing to know the length of the float because it will be beneficial in making a table table of logarithms like this.

--------------------------
| 1000000 | 72382.413651 |
--------------------------
3

There are 3 best solutions below

0
On

Springboarding off of dasblinkenlight and Eric - An example of what you can do is simply edit your code to look like this:

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

int main()
{
    double n = 72382.413651;
    //int len;

    //len = sizeof(n);
    //printf("%d\n", len);
    printf("%5.6f\n", n);
    getchar();
}

This will print the value to look exactly as it does in the declaration.

Or, say you have to deal with numbers with decimal points in various positions but want the formatting to be in neat columns. You could do something like this:

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

//varible format codes for varying decimal point positions    
char *format[]={"%0.9f\n",
                    "%0.8f\n",

                "%0.7f\n",
                "%0.6f\n",
                "%0.5f\n",
                "%0.4f\n",
                "%0.3f\n",
                "%0.2f\n",
                "%0.1f\n",
                "%0.0f.\n"};

int main()
{
    char val[25];
    char dp[]={"."};//will search for position of decimal point in string
    //for the purposes of illustration, create some floating point values
    double a = .723824136;
    double s = 7.23824136;
    double d = 72.3824136;
    double f = 723.824136;
    double g = 7238.24136;
    double h = 72382.4136;
    double j = 723824.136;
    double k = 7238241.36;
    double l = 72382413.6;
    double m = 723824136.;

    //put in string of standard length,   
    //format output according to decimal position
    sprintf(val, "%0.12f", a); printf( format[strcspn(val, dp)], a);
    sprintf(val, "%0.12f", s); printf( format[strcspn(val, dp)], s);
    sprintf(val, "%0.12f", d); printf( format[strcspn(val, dp)], d);
    sprintf(val, "%0.12f", f); printf( format[strcspn(val, dp)], f);
    sprintf(val, "%0.12f", g); printf( format[strcspn(val, dp)], g);
    sprintf(val, "%0.12f", h); printf( format[strcspn(val, dp)], h);
    sprintf(val, "%0.12f", j); printf( format[strcspn(val, dp)], j);
    sprintf(val, "%0.12f", k); printf( format[strcspn(val, dp)], k);
    sprintf(val, "%0.12f", l); printf( format[strcspn(val, dp)], l);
    sprintf(val, "%0.12f", m); printf( format[strcspn(val, dp)], m);
    getchar();
}
0
On

Use of sizeof(n) is somewhat misleading, because the value of n does not play into the answer: you always get the equivalent of sizeof(double), which appears to be 64 bits or 8 bytes on your computer. Recall that sizeof is a compile-time operation; only the type of the parameter matters, but not its value.

The number of digits in a float's representation is often a meaningless value, because in general the representation is imprecise. Only the number of digits before the decimal separator can be determined reliably; the digits after the separator are usually truncated to the number that you set to your liking.

I am needing to know the length of the float because i am trying to make a table table of logarithms like this.

Rather than finding out the size of your floats, force them all to a specific length. Specify width and precision in your calls of printf to make sure that everything is formatted to the same length, and looks good in a table.

0
On

If you are looking for counting number of digits in the double, you could probably use sprintf and convert as string. Then you can count the characters in string.

It is already discussed here.