# Thread: C Double Float and printf problem

1. ## C Double Float and printf problem

I have been searching the forum for over an hour trying to find a solution to what i'm seeing, as well as the rest of the internet, but theres nothing i can find. I initialize a variable as a double, however when i print it, it limits the value to 16 digits, and fills in the rest with zeros. This reflects the e38 of a float, not the e308 of a double.

Code:
```#include <stdio.h>
#include <float.h>

int main()
{
double x;
double y;
int lim44;
printf( "Enter the number of cycles: ");
scanf( "%d", &lim44);

x = 1.0;
y = 1.0;

do {
/* "Hello, world!" is printed at least one time
even though the condition is false*/
printf( "%.1Lf\n", x );
x = x+y;
lim44 = lim44-1;
printf( "%.1Lf\n", y);
y = y+x;
lim44 = lim44-1;
} while ( lim44 > 0 );
getchar();
}```
The Output for sequences 80 to 100:
Code:
```37889062373143904.0
61305790721611584.0
99194853094755488.0
160500643816367070.0
259695496911122560.0
420196140727489660.0
679891637638612220.0
1100087778366101900.0
1779979416004714000.0
2880067194370816000.0
4660046610375530500.0
7540113804746346500.0
12200160415121877000.0
19740274219868226000.0
31940434634990100000.0
51680708854858326000.0
83621143489848426000.0
135301852344706760000.0
218922995834555200000.0
354224848179262000000.0```

As seen, it gives up after 16 digits, and with rounding this throws the math off more and more every level. Is there a way to correct this? Or am i missing something stupidly simple?

Any help will be appreciated.

2. You are lucky you are getting what appears to be valid numbers at all. %Lf is used to print long doubles, not doubles. Change your format specifier to %f, and see if that works for you.

3. Try declaring your x and y as long doubles. Thats the highest precision offered in C.

4. A float has 7 digits, and a double has 16. (Approximately: A float has 24 binary digits, which is about 7.22 decimal digits of accuracy, and a double has 53 binary digits, which is 16 decimal digits.) As to the size, the bit you show appears to have the right order of magnitude if I understand what you are trying to do.

5. Originally Posted by bithub
%Lf is used to print long doubles, not doubles. Change your format specifier to %f, and see if that works for you.
I tried that to begin with, to no avail. I've read the printf page, but i i think the issue isn't printf but the float/double part. its not using the right limits, and i haven't yet found any reason why.

6. Originally Posted by tabstop
A float has 7 digits, and a double has 16. (Approximately: A float has 24 binary digits, which is about 7.22 decimal digits of accuracy, and a double has 53 binary digits, which is 16 decimal digits.) As to the size, the bit you show appears to have the right order of magnitude if I understand what you are trying to do.
Using float or double or long double all releases the same result as i posted.

7. Originally Posted by SlyVixsky
I tried that to begin with, to no avail. I've read the printf page, but i i think the issue isn't printf but the float/double part. its not using the right limits, and i haven't yet found any reason why.
Well, if you mean the limits in float.h, that's true -- you didn't use them anywhere. Let's start here: What do you think you're supposed to be getting?

8. Originally Posted by tabstop
Well, if you mean the limits in float.h, that's true -- you didn't use them anywhere. Let's start here: What do you think you're supposed to be getting?
I want to calculate the fibonacci sequence for 'q' iterations, but after 16 digits it doesn't show the numbers, which means the program is only accurate up to about the 83 iteration. I've looked into the #define command for the float.h library, but changing the exponent limit, or number of digits hasn't made any difference.

9. This is the last 5 iterations I get using GCC:

Using floats:
Code:
```51680688087983194112.0
83621113846783016960.0
135301801934766211072.0
218922915781549228032.0
354224717716315439104.0```
Using doubles:
Code:
```51680708854858326016.0
83621143489848426496.0
135301852344706760704.0
218922995834555203584.0
354224848179261997056.0```
Using long double:
Code:
```51680708854858323072.0
83621143489848422976.0
135301852344706746048.0
218922995834555169024.0
354224848179261915072.0```
I'm not sure why you are getting the same values using all 3 types.

The best question though is why are you using floating point arithmetic? Use a type like long long instead and you will actually get the correct results.

EDIT: Actually, I may be underestimating how large of numbers you need here. If you need something larger than a 64 bit integer can handle, then you should look into using GMP.

10. Originally Posted by bithub
This is the last 5 iterations I get using GCC:

I'm not sure why you are getting the same values using all 3 types.

The best question though is why are you using floating point arithmetic? Use a type like long long instead and you will actually get the correct results.
my result with long
Code:
```572466946
-2079590721
-1507123775
708252800
-798870975
-90618175
-889489150
-980107325```

11. GMP???

12. Originally Posted by SlyVixsky
I want to calculate the fibonacci sequence for 'q' iterations, but after 16 digits it doesn't show the numbers, which means the program is only accurate up to about the 83 iteration. I've looked into the #define command for the float.h library, but changing the exponent limit, or number of digits hasn't made any difference.
You can't change the values in float.h; they describe what the hardware does, not dictate to the hardware what it should do.

If you really need twenty-odd decimal digits, then you need to either (a) make sure you are on a system with a 128-bit long long type, (b) write your own routines for extra-long integers, or (c) use somebody else's bignum library (like the GSL for instance).

13. my result with long
I said long long, not long.

GMP???
It is a library for doing arithmetic with very large numbers.

14. Originally Posted by SlyVixsky
my result with long
Code:
```572466946
-2079590721
-1507123775
708252800
-798870975
-90618175
-889489150
-980107325```
Note that "long" and "long long" are not the same thing.

GMP is an option, yes. (I think I erroneously just put GSL in my last reply.)

15. Originally Posted by bithub
I said long long, not long.
.
same result