C++ Precision and Accuracy in floating point.


C and C++ precision and accuracy in floating point value

In C and C++ precision and accuracy is a problem which must be dealt with while using floating point in mathematical evaluation or any other operations.Although C++ provide some standard functions to handle these issues,however it can only handle it to some extend,when the digits gets very large the functions fail to provide the exact value.Nevertheless,in this post we will see what are precision and accuracy in floating point and also find a way to handle them correctly-although we may require the help of an external library.

Precision :It is the total number of digits in any number.For example 2.45 has 3 precision value,0.3 has one precision.A detail discussion on precision is given below.
 
Accuracy :It refers to the degree of exactness or correctness of a number.

Link:C++ floating point data type


Precision in floating point type

In C++ any floating point type can represent a value up to 6 digits precision,after that the value gets rounded off.Consider the program below.

Example 1

#include <iostream>

using namespace std ;

int main( )
{
float f=3.455311213 ;
double d=3.34545724363 ;
long double ld=123.3254235 ;

cout<< f << endl
<< d << endl
<< ld ;

cin.get( ) ;
return 0 ;

}

 
The output is,

3.45531
3.34546
123.325

After six digits the value gets rounded off.In case of d variable since the 7th digit is greater than 5 the 6th digit value is increased by 1.If the 7th digit is less than 5 the 6th digit remains as it is.The normal Mathematics rule of rounding off the digit is helpful to some extend in mathematical evaluation but in programming it can give rise to some unexpected result.Consider the program given below.

//Example 2

#include <iostream>

using namespace std ;

int main( )
{
float f=56.7837 ;
f=f*f ;

if( f==3224.38858569 )
{
cout<<“f is ” << f ;
}
else
cout<< “Something is wrong \n” ;

cin.get();
return 0;

}

 
The output is

Something is wrong.

Although 56.7837 * 56.7837 is 3224.38858569 the value is rounded off so,f value is stored as 3224.39 which is not same as 3224.38858569 and hence the unexpected output.In such program where a number is used directly,the number should be casted(cast simply means changing the type) to the same type as the variable with which the operation is performed.In this case cast the number 3224.38858569 to float type and you will get the expected result.Look at the code below.

Link :C++ type casting

if( f== float(3224.38858569) )
{
cout << “f is ” ;
}
else
cout<< “Something is wrong “;

 
Now the output is

f is 3224.39

By casting the number 3224.38858569 to float type it got rounded off and the resultant value is same as f and the output is what we wanted. But in scientific calculation we cannot always round off the value because we might want a precise measurement of the experiment.For such implementation C++ provides us with some functions through which we can set the precision to as many digits as we want.



There are two standard functions through which we can set the precision to as many digits as we desire.
 
i)setprecision( x ):This function is included in <iomanip> header’s file.The parameter x is an int type and represent the precision value.

ii)ios_base::precision( x ):This function is included under <iostream> header’s file.

The program below uses the two functions to set the precision value up to 13 digits.

Example 3

#include <iostream>
#include <iomanip>

using namespace std ;

int main( )
{
float f=9.36748573593632392334 ;
cout<<“Before setting the precision \n”;
cout<< f << endl ;

cout<< “After setting the precision up to 13 digits \n” ;
cout << setprecision(13) << f << endl ;
cout.precision(13) ;
cout<< f << endl ;

cin.get( ) ;
return 0 ;
}

The output is,

Before setting the precision
9.36749

After setting the precision up to 13 digits,

9.367486000061
9.367486000061

The two functions does give us a number having up to 13 digits precision value but it is far from equal with the original value,that means it is not accurate.The next section shows how to solve the accuracy problem in C++.


 


Accuracy in floating point type

By accuracy we mean how close is the value to the correct value and from the example 3(above) we have seen that being precise does not mean the value is always accurate.Being accurate is important.Take an example in banking application when Compound interest is calculated,inaccurracy can lead to incorrect transaction of money.Since C++ does not provide us with any standard library to make a number precise and accurate at the same time,the solution is you can either develop a library of your own that can give a precise and accurate value or you can use an outside library like boost to get a very precise and accurate value.The program given below shows how to obtain a precise and accurate value using Boost library.

#include <iostream>
#include <iomanip>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

using namespace std ;

int main( )
{
using boost::multiprecision::cpp_dec_float_50 ;

cpp_dec_float_50 num=cpp_dec_float_50( “1234.56593838732623723832”) ;

cout << num << endl ;
cout<< setprecision(20) << num << endl ;

///Calculating Area of a circle
cpp_dec_float_50 radius=cpp_dec_float_50(“2.145”) ;

cout<<“Area =” << boost::math::constants::pi<cpp_dec_float_50>() * radius * radius << endl ;

///Setting the precision up to 50 digits
cout<< “Area =” << setprecision(50) << boost::math::constants::pi<cpp_dec_float_50>() * radius * radius << endl ;

cin.get();
return 0;
}

 
The output is

1234.57
1234.5659383873262372
Area =14.454546338982978435
Area =14.45454633898297843499758377255357475776328133576

Boost is not a bad library(in fact it is the best C++ library) so you can install the library and check the output yourself.And also if you are planning to make a Calculator that can yield a high precision and very accurate value then Boost might come in handy.


Conclusion

Precision and accuracy in floating point type is not a petty issues and it must be given an equal weightage of importance as the other C++ features.Since C++ does not provide any library to handle this problem we must turn to outside library for solution and Boost is one such library.If you love C++ having this library in your computer is worth it in every way.


Related Link

->Precedence and Associativity in C and C++.
 
->Float or floating point internal format(IEEE 754 format)(explains why float supports decimal point value while int doesn’t).