You are here:

# C++/C

Question
Dear sir,

My name is D.Prince.
While studying C,I came across the word
'precision,double precision..'.

What is a 'precision' in C,sir?
Why do we need/use it,sir?

regards
Prince

'Precision' is not just a C/C++ term. It is used in many areas some of which are computer languages.

The meaning in such computer languages concerns the precision of values, either as stored in memory or displayed or written to file etc. It is usually applied to floating point numeric types that are used to approximate real (decimal) numbers.

Consider the value of pi. This is an irrational number whose exact value can never realistically be fully stated. So to 7 significant digits of precision pi has the value 3.141593. To 4 significant digits of precision it has the value 3.142. Both are correct for the precision stated; neither is totally accurate in the absolute sense.

You could say that the first number has double the precision of the second.

So precision is the number of significant digits used to express a value. In the world of real numbers there is a format, sometimes known as scientific, that expresses the value as a value between 1 and less than 10, followed by a scaling factor: x 10 to the power n. Some examples:

3.142 x 10 to the 0
2.3456 x 10 to the 23 (a rather large number)
6.03 x 10 to the -23 (a rather small number)

Apologies, the answer text format is too simple (as far as I know) to show proper superscripted power values so I have used 'to the' instead. A short hand is to replace the 'x 10 to the' part is to use E:

3.142E+0
2.3456E+23
6.03E-23

Now, as mentioned, computers can approximate real numbers using a format known as floating point. This splits the number representation into two parts: a fractional part (or mantissa), and a scaling factor (or exponent). Unlike real numbers used by humans these values are stored as binary values and the exponent part scales the mantissa by 2 to the power n instead of 10 to the power n. This is similar to the scientific format for real number representation except that:

- all values are in binary
- all values are finite
- the mantissa typically represents values between a
1 and less than 2, not 1 and less than 10
- the exponent part is a power of 2 not a power of 10

As all values in computers are finite the precision of the values stored by floating point types depends on the number of bits used to store the fractional part, which in turn is related to the number of bits used to store the whole floating point value. It is common for computer languages that support floating point values to support at least two floating point value sizes: A small, single precision floating point type (typically 32-bits in size, using 23 bits for the fractional part), and a double precision (typically 64 bits in size using 52 bits for the fraction). C and C++ support a long double float type, which may be the same size as a double for many implementations but could be an even larger sized value - maybe using 80 or 128 bit for a whole floating point value.

23 bit of precision equates to about 7 significant decimal digits. 52 bit equates to around 15 significant decimal digits.

The exponent is used to scale the values. A 32-bit single precision floating point value can represent value from 10 to -38 to 10 to +38. A 64-bit double precision value can represent a scale from 10 to the -308 to 10 to the +308.

Larger precision floating point values take up more space and can be slower of perform computations with. Lower precision floating point values are less precise, handle a smaller range of values and can cause more significant rounding errors that may be unacceptable to the problem at hand. So which to use depend on your requirements.

C++

Volunteer

#### Ralph McArdell

##### Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

##### Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials