Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ variable types precision?

I'm writing a program for C++ that finds a square root approximation. Here's the program:

#include <cstdio>

#include <cstdlib>

#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

cout << "Radicand?" << endl;

int radicand;

cin >> radicand;

cout << "Guess?" << endl;

double guess;

cin >> guess;

double error;

error = radicand-(guess*guess);

while(-.000001>error>.000001);

{ guess=(.5*(guess + radicand/guess));

error = radicand - (guess*guess);

}

cout << "The square root approximation of " << radicand << " is " << guess << endl;

cout << guess << " squared is " << guess*guess <<endl;

system("PAUSE");

return 0;

}

_____________________________

The problem is that the answer it returns is only 5 decimal points precision. I specified that it's a double. What's up with that?

6 Answers

Relevance
  • Pfo
    Lv 7
    1 decade ago
    Favourite answer

    while(-.000001>error>.000001);

    That's a problem. First, you can't do this: -.000001>error>.000001. At least not on most compilers, also you have a semicolon after that loop, so the loop wouldn't do anything. Try this:

    while (abs(error) > 0.00001)

    {

    // code

    }

    Your while loop isn't executing, and the body will execute only once.

    * include math.h for the abs (absolute value) function

  • 1 decade ago

    6 is default for double, if you need more you have to specify it

    this example would give you 10 decimal point precision you insert it before your cout statements at the end

    like this:

    cout. precision(11);

    cout << "The square root approximation of " << radicand << " is " << guess << endl;

    cout << guess << " squared is " << guess*guess << endl;

    should give you 10 decimal places, you can edit the number as needed

    hope that helps

  • Erika
    Lv 4
    4 years ago

    For int and lengthy, you're waiting to favor to seem on the record "limits.h". you do now no longer favor to #comprise it as you'll do with <stdio.h> or <stdlib.h>, you want to open it in an editor and bypass to the Numerical Limits section. that allow you to carry close the major significant integer fee that is valid and from that, you'll get the variety of bytes of the variable. in case you want to code, you're waiting to write down a application which will #comprise <limits.h> and which will print the barriers.

  • ?
    Lv 7
    1 decade ago

    As well as all the other mistakes, radicand should be a double.

  • Paul W
    Lv 5
    1 decade ago

    Use the printf function for output and you can specify how many decimal points precision you want displayed.

  • 1 decade ago

    Above guy is correct!

    here is the way:

    change code that cout an "guess" with printf(), with format %[flags][width] [.precision]

    the flags is "d", so the code become:

    printf("%4.8d", guess);

Still have questions? Get answers by asking now.