0

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 |
--------------------------
kyle k
  • 5,134
  • 10
  • 31
  • 45
  • 2
    Do you mean significant digits? What do you expect to be printed and why? – dcaswell Aug 31 '13 at 00:45
  • 1
    Of course, `sizeof(double)` would give the size of double, which is 8 – SKPS Aug 31 '13 at 00:46
  • 1
    `sizeof` tells you how much memory is required, in bytes, for the number in its internal binary representation. It's not how many digits. As far as number of digits, there are "significant" digits, as well as "digits of precision". When you store the number `71281.413651` it might actually end up being, internally, `71281.41365099999983` depending upon how well it converts to binary. – lurker Aug 31 '13 at 00:47
  • 2
    Why do people downvote this? It's a legitimate question, and it displays a genuine effort to understand what's going on. – Sergey Kalinichenko Aug 31 '13 at 00:54
  • 1
    Personally I down-voted it because I have no idea what he wants the program to print, and after being asked has not answered. – dcaswell Aug 31 '13 at 01:05
  • @user814064 I added what it will be used for, printing a table that is properly formatted. – kyle k Aug 31 '13 at 01:06
  • What does 1 million have to do with 73 thousand? – dcaswell Aug 31 '13 at 01:09
  • @user814064 "72382.413651" is the natural logarithm of 1 million. – kyle k Aug 31 '13 at 01:11
  • How does 1 million format 72 thousand. – dcaswell Aug 31 '13 at 01:13

3 Answers3

4

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

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.

Community
  • 1
  • 1
SKPS
  • 5,433
  • 5
  • 29
  • 63
0

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();
}
ryyker
  • 22,849
  • 3
  • 43
  • 87