Hello, I got this exercise which has a very big number, and I cant get to display it all without those e things. What I need to get is 18446744073709550000, but instead I get 1.84467e+019.
Use the integer unsignedlonglong type instead of double.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
int main()
{
unsignedlonglong k = 64;
unsignedlonglong b = 0;
unsignedlonglong c = 1;
for(unsignedlonglong i = 1; i <= k; i++)
{
b += c;
c *= 2;
}
std::cout << b;
return 0;
}
18446744073709551615
longlong was introduced in C++11 so you will need a recent compiler.
Looks like an exercise from Programming Principles and Practices using C++, Chapter 4 Exercise 8 or 9 perhaps? If so you really should stick with the int or double data type as required by the exercise. These exercises are meant to illustrate some of the pitfalls of using both fixed point and floating point numbers in C++. (Hint you're not meant to be able to show the numbers correctly.) Even using the "fixed" precision specifier of the double with a large "width" you should notice problems displaying these numbers.
The OP may want to look into the fixed format flag along with the setw() function.
This exercise is from a bit different book. I tried unsigned long long, but it doesnt seem to be giving correct numbers. Im using Visual Studio 10.0 as a compiler, so I guess i need a more recent one ?
My compiler seems to be working with it as well, but, numbers doesnt seem to be right. I need to get 18446744073709550000, but im getting 18446744073709551615. Maybe theres something wrong with my code ?
You'll probably be interested in both the integer and floating point limits.
Also could you share the name of the book and post the actual program requirements?
You'll also need to insure that you're using a fairly recent version of your compiler to insure that the long long int is actually available. This type was added to the language with the C++11 standard.
My book is written in another language, but I can try translate the exercise. There are 64 blocks. 1st block is 1, 2nd block is 2, 3rd block is 4, 4th block is 8 etc. Increases each time by *2. And i just saw that answer that you got is the same as mine.
I just checked max value of my long long, its the same number, 18446744073709551615. But its still higher than 18446744073709550000, so it shouldnt be a problem to get this number.
I edited my code a bit, I seem to get a bit closer to the result I need. This time its 18446744073709552000, but I have no idea from where that 2 came; 4th number from the end. How my code looks now:
The value of long long has no bearing on your program since you're not using a long long. What you need to look into is the limits of your double. It appears that your double has 15 significant digits. That 2 is probably a result of rounding.
I was using long long before, but the result wasnt as similiar as with double. Exercise also wanted me to use it. I think ill leave that 2 there, ill ask later my teacher about it later. Thank you for your help very much ! :)