1

I'm new to c++ and i want to represent a value as accurately as possible using "double" data type , why pi & area values aren't represented fully here ?enter image description here

expected that float and double will have different value representations in my code but they are showing the same accuracy with different size

Here is the code

#include <iostream>
using namespace std;
int main()
{
    const double PI = 3.14159265359;
    int radius = 6;
    cout << "Since radius is " << radius << " Cm" << '\n';
    cout << "and pi is " << PI << '\n';
    double area = PI * radius;
    cout << "Then the Area of the circle will equal (pi x radius)= " << area << '\n';
    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • First, post text, not images of code. Second, you are relying what the default behavior of `std::cout` is showing you. That is not how to determine what the real internal values are. – PaulMcKenzie Dec 24 '22 at 20:33
  • See also [here](https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout) or [here](https://stackoverflow.com/questions/4217510/how-to-cout-the-correct-number-of-decimal-places-of-a-double-value) or [here](https://stackoverflow.com/questions/68293886/double-is-not-printing-more-than-6-significant-digits-even-after-setprecision) – Nathan Pierson Dec 24 '22 at 20:47
  • Apologies , here is the code but i believe writing it here will be kind of messy #include using namespace std; int main(){ const double PI=3.14159265359; int radius=6; cout<<"Since radius is "< –  MrasksAlot Dec 24 '22 at 21:10
  • 2
    @MrasksAlot — re: “it will be messy” — indeed. Edit the **question** to add the code. – Pete Becker Dec 24 '22 at 21:23
  • 2
    By default, stream inserters for floating point types show six digits. Read about [`std::setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision). – Pete Becker Dec 24 '22 at 21:26
  • 1
    See [editing help](https://stackoverflow.com/editing-help#code) if you need help making the code less messy when you edit it into your question. – JaMiT Dec 24 '22 at 21:30
  • Yup this answers my question .. thank you guys Edited the code format in the question , appreciate your patience guys i will read more about std::setprecision and utilize it –  MrasksAlot Dec 24 '22 at 21:41
  • To easily form a code block and keep your indentation type ``` press enter paste code exactly as it is in your IDE. Press Enter. Type ``` and press enter again. See the edit I made to the question. – drescherjm Dec 24 '22 at 23:23

0 Answers0