0

Guys I have a question:

39268112/50000000*4 is exactly 3.14144896

But when i write a code in c++ as such:

#include<iostream.h>
using namespace std;
int main()
{
   int a = 39268112;
   int b = 50000000;
   cout << (double)a / (double)b * 4;
}

But, I get 3.14145.

This is not the exact value.

So my question is:

how can i always display the exact value of a double division?

EDIT: I am aware of the setprecision option, the problem here is just, that I have to know the exact value of the result to know how many decimals the result will have. Since I wrote a program, which approximates PI based on dart throws, the two integers a and b can have much bigger values than 39268112 and 5000000.

If I would use

cout << setprecision(some number) << (double)a / (double)b * 4;

I need to know the exact result first, before I can even enter the argument in setprecision()

My edited question would be if there is a way to automaticly put an argument in setprecision() so that it can display the exact result, without having to calculate it myself first.

Stefan B
  • 541
  • 2
  • 6
  • 13

0 Answers0