Thread: full form of a number

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    183

    Question 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

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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;
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    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 ?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The part of me that is a mathematician cries in pain screaming "Leave it as a fraction!" when I see this

    Just kidding.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  5. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM