-
full form of a number
hello .
I have a program in c++ language that takes a digit and put it in a formula and gives the answer .
for example if I enter 23 it gives 46.356894 but the complete answer is 46.35689399 .
is there any way for me to have the complete form of the answer ?
tnx99
-
The variable type you use determines the precision possible. I'm no expert on this stuff, but I would say, use the double type. It can hold a hell of a lot. Eg:
Code:
//Instead of:
int a;
//Use:
double a;
-
In addition to using double, you must format the output correctly. I assume when you say "it gives 46.356894" that you mean your program prints the result to the console or a file and that is what it displays. You must specify the format of your output to be something other than the default. If you are using an iostream or fstream, then I believe setprecision will help you (it is in the header <iomanip>). Here is an example program to point you in the right direction:
Code:
#include <iostream>
#include <iomanip>
int main()
{
double testVal = 46.35689399;
std::cout << "Value = " << testVal << std::endl;
std::cout << "Value = " << std::setiosflags(std::ios::fixed) << std::setprecision(8) << testVal << std::endl;
}
If that doesn't help you (and nobody else can), then posting the code that you are referring to would help. Also note that you should only set the precision to 8 digits if you are sure that your calculations will not lose significance that low. Again I assume that it won't or you wouldn't expect the longer answer, but I just wanted you to be aware of it.
-
tnx that u help me .
I try double but it didnt help me .
jlou
i also use the code that u had given me but I gave this errors :
type qualifier 'std' must be a struct or class name .
what can I do ?
-
You can use <iostream> not <iostream.h>, etc.
And, BTW, the part of me that is an engineer cries in pain screaming "significant figures" when I see this :P
-
The part of me that is a mathematician cries in pain screaming "Leave it as a fraction!" when I see this :p
Just kidding.
-
arian, the point of that code was to give you a program you can compile and run separately from your own program. That way you can look at the code and see what it does, and once you understand it you can use similar techniques in your own code.
Here is another version that might be a little easier to understand:
Code:
#include <iostream>
#include <iomanip>
using namepsace std;
int main()
{
double testVal = 46.35689399;
cout << "Value = " << testVal << endl;
cout << "Value = " << setiosflags(ios::fixed) << setprecision(8) << testVal << endl;
return 0;
}
When you compile this as a separate program and then run it, notice how the first line output doesn't display all the digits, but the second line does. In the code the difference between the two lines of code is the setiosflags and the setprecision functions. So I suggest you look these up to see what they do. They should be in your book, and you should be able to search the web or this site to find more information about them. I'm not guaranteeing that they will solve your problem, but they might help.
And as I said before, posting your code would help us help you (don't forget to use code tags).