-3

It is not about printing. It is about storing the value.

int x = 1234;
double y = 0.3456;

double z = x + y;

Currently z contains 1234.35. I want z to contain 1234.3456

What can be done to achieve this?

Community
  • 1
  • 1
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

5

It does contain 1234.3456. Check for yourself:

#include <cstdio>

int main()
{
    int x = 1234;
    double y = 0.3456;
    double z = x + y;

    printf("%.8f\n", z);
}

Output:

1234.34560000
rustyx
  • 80,671
  • 25
  • 200
  • 267
2

The double accuracy contains a bit less than 16 decimal digits. The printed value of z is less precise than the stored one. If you want more than 16 digits, you should consider specific libraries like mpfr

Franck
  • 1,635
  • 1
  • 8
  • 11