jChen jChen - 3 years ago 193
C Question

How to keep numbers' precision in C

I am trying to store as user input a large number in C in the variable "num" using this piece of code.

scanf("%e",&num); //input: 340282346638528859811704183484516925440
printf("%1.20e\n", num); // output: 3.40282346638528860000e+038


However it loses precision after "886000000", it rounds up to 0.

I thought that in C, it stores the number in double format. How do I keep this precision for larger numbers?

Answer Source

On most implementations, double has 53 bits of precision. That's about 17 decimal digits, which is what you seem to have.

You can use long double if available; on Intel platforms, that will give you another 11 bits of precision.

Beyond that, you'll need to find a multiprecision library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download