1

This is my program:

 #include "stdafx.h"
 #include <iostream>

 using namespace std;

 double areaofcircle(double r)
 {
 return 3.14 * r * r;
 }

 int main()
 {
 cout << areaofcircle(5);
 }

I should be getting the output of "78.5" but I keep getting "78.512". What is going on?! I've also tried float, but I still get the same output.

Also just a side question, do I really need to add "return 0;" to the main function?

One more side question, do I need to write "using namespace std;" inside every function, or can I just write it outside of everything, like how I've been doing it.

43.52.4D.
  • 950
  • 6
  • 14
  • 28
  • Possible duplicate of http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – Aiias Mar 10 '13 at 23:56
  • http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout – Mitch Wheat Mar 10 '13 at 23:57
  • Try changing `3.14` to `3.14d` – Patashu Mar 10 '13 at 23:57
  • Try to use setprecision from iomanip header. – Mihai8 Mar 10 '13 at 23:58
  • @Patashu that doesn't work... – 43.52.4D. Mar 11 '13 at 00:06
  • FYI, if you #include , you should be able to use the M_PI macro instead of 3.14. Of course then you won't be getting 78.5 for a good reason. – Tod Mar 11 '13 at 00:52
  • This “question” should be closed as not a real question. It asks multiple unrelated questions, its main question is inconsistent with the demonstrated code, and an “answer“ that does not answer the question is marked accepted. As it is, this “question” will not benefit any other Stack Overflow users. – Eric Postpischil Mar 11 '13 at 18:21

3 Answers3

1

I think you are doing something wrong. I tried the same on GCC compiler, and I do get 78.5. Which compiler are you using?

Regarding your other questions

  1. It is always a good idea to return the state of your program from main. Usually, you can return EXIT_SUCCESS if everything works okay, else you can return EXIT_FAILURE.

  2. No it is not necessary to include using namespace std. Instead, it is bad practice to pollute your standard namespace. You should include only those functions that you use very frequently.

To read more about C++. Check this link

Hope this helps.

Community
  • 1
  • 1
1

Tried a few experiments on VS 2008 to see if I could get a similar error. By changing pi to a float I do get 78.500002622604370 which is different but not the same as your issue. But I do get 78.5 when pi is a double.

I'd recommend you let us know which compiler and version you're using, then possibly someone may be able to help.

#include "stdafx.h"
#include <iostream>
const double pi = 3.14;


 double areaofcircle(double r)
 {
    return pi * r * r;
 }

int _tmain(int argc, _TCHAR* argv[])
{
    double temp = areaofcircle(5);
    std::cout << temp;
    return 0;
}
Rich
  • 4,572
  • 3
  • 25
  • 31
1

You're passing the literal for an integer (5) so somewhere an implicit conversion is required to turn it into a double. You would be better off passing 5.0. The C++ default for doubles requires no specifier so your 3.14 is fine. (specifying a float requires 3.14f). That said, I tried both with 5 and 5.0 and got 78.5 both times on my compiler.

How you're using the std namespace is fine, but as pointed out it does bring ALL of the standard namespace into scope. I see this a lot in teaching material. It is better to just use using std::cout;

or just explicitly add std::cout to all uses. However, there is nothing "wrong" from a compilation standpoint in the way you did it.

Tod
  • 8,192
  • 5
  • 52
  • 93