0

How to print big double value with precision of 10 in C-language (C99) ?

double d1 = 1100200300400500600.0000000001;
double d2 = 1100200300400500600.0000000001;
double d3 = d1 + d2;
printf("???", d3);

Expect to have output: 2200400600801001200.0000000002

Dmitry
  • 846
  • 1
  • 7
  • 20
  • I tried `%f` and got `2200400600801001216.0000000000`. – Bart Friederichs Feb 26 '16 at 12:22
  • I'd recommend to use an arbitrary precision library. – Bart Friederichs Feb 26 '16 at 12:24
  • `double` in typical environment (64-bit IEEE754) is not capable to store such value with high precision. – MikeCAT Feb 26 '16 at 12:25
  • If you ignore the decimal comma until the point where you are about to present the value to the user, everything turns so much easier. Then you can use one of the various "big int" libraries out there. – Lundin Feb 26 '16 at 12:30
  • 1
    Your question mentions “precision of 10” but seems to show that you don't understand what this means. Here is the definition: https://en.wikipedia.org/wiki/Significant_figures – Pascal Cuoq Feb 26 '16 at 12:41
  • 6 decimal digits of "precision" for `float`, 15 decimal digits for `double`, for an appropriate definition of "precision". – DevSolar Feb 26 '16 at 14:27
  • [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point#Basic_and_interchange_formats) 64-bit double-precision floats have a 53-bit mantissa, which is 15.9 decimal digits, so you can only trust the first 15 digits for accuracy. 32-bit single-precision floats have 24-bit fractions, or 7.2 decimal digits. – David R Tribble Feb 26 '16 at 16:45
  • The only *exact* string representation of a binary floating-point value is a hexadecimal floating-point form, which requires the `%a` printf format specifier. – David R Tribble Feb 26 '16 at 16:50

4 Answers4

2

You need to use an arbitrary precision library for c.

I you're going to store integers, you can use the GMP.

If you're going to store large floating-point values, I would recommend you to use the MPFR, a library which supports floating-point types. The MPFR is based on the GMP.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
1

You should check how floating points are stored in C. C99 implements IEEE754. This gives you 11 bits for exponent and 52 bits for mantissa. It's only those 52 bits you can use to represent the number. Exponent tells you where the floating point will be.

You simply can not store such big number with given precision in double.

Peter Silon
  • 457
  • 2
  • 11
1

OP's code is not exactly assigning value as perceived. double can only represent a finite set of numbers. It will use the closest representable double. Typically double can accurately represent at least the first 15 signification decimal digits, or 110020030040050xxxx.xxxx

1100200300400500600.0000000001 lies between two double:
1100200300400500480 and
1100200300400500608.

// Add d1 to the closest representable double.  This turns out to be _exactly
//          1100200300400500608
double d1 = 1100200300400500600.0000000001;
double d2 = 1100200300400500600.0000000001;

// adding those 2 result in the _exact
//          2200400600801001216
double d3 = d1 + d2;

// To print to 10 decimal places
printf("%.10f", d3);
// 2200400600801001216.0000000000

With typical floating point, to print the value exactly, use

printf("%a\n", d3);

To print a double in decimal with sufficient accuracy to distinguish all double, use

printf("%.e\n", DBL_DECIMAL_DIG - 1, d3);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

It's not that simple. I suggest you to use a library like GMP. I know it's not exactly what you're looking for, but the answer, as I said, it's not so simple as it seems to be.

Of course, you can code yourself the operations, and it's a good practice (if you want to kill some time). :)

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Paulo
  • 1,458
  • 2
  • 12
  • 26