Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store double with maximum precision

I'm a bit stucked with storing number with maximum available precision:

double tmp = 569.232306826889043804840184748172760009765625L;

I'm trying to print it on the screen:

printf("%0.52f\n", tmp);

And that's what I've got:

569.2323068268890400000000000000000000000000000000000000

Is this the maximum precision which I can achieve?


P.S. I'm using Visual Studio 2008

like image 809
tema Avatar asked Mar 31 '15 15:03

tema


2 Answers

A double is usually stored in IEEE 754 binary64 format.

A binary64 has 52 bits of precision, not 52 decimal digits - this is equivalent to at most 17 decimal digits, which is what you're displaying.


Is this the maximum precision which I can achieve?

Some platforms may provide a long double which is actually bigger than a double, but yours doesn't seem to.

If you want more precision, you can either use a library that exposes some larger/more precise type supported by your hardware (such as the 80-bit extended double), or an arbitrary-precision library that works in software.

like image 77
Useless Avatar answered Sep 28 '22 05:09

Useless


That is the precision of type double in your C implementation. Conformant C implementations also have a type long double, which may offer greater precision.

If you need more precision than that then there are numeric libraries that provide arbitrary-precision numeric types and functions to operate on them. The GNU Multiple Precision Arithmetic Library is one such; it works on many platforms, including the ones relevant to you -- Win32 and Win64.

like image 27
John Bollinger Avatar answered Sep 28 '22 07:09

John Bollinger