faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 45 people (62%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

What is round Off Error

Jul 24th, 2006 06:37
Suneeth Kumar Akkera, John KIng,


The IEEE Standard 754, has been widely adopted standard for 
the representation of real numbers in computers(as floats and 
doubles). It specifies formats for single precision and double 
precision numbers. The single precision format allows 32 binary digits 
(known as bits) for a floating point number with 23 of these bits 
allocated to the mantissa. In the double precision format the values 
are 64 and 52 bits, respectively. 
           On conversion from binary to decimal, it turns out that any 
IEEE Standard 754 single precision number has an accuracy of about six 
or seven decimal digits, and a double precision number an accuracy of 
about 15 or 16 decimal digits.
           To reduce the number of decimal digits,chopping method was 
used by many early computers(nothing but ignoring the unwanted digits) 
But a more common and better procedure is "rounding", which involves 
adding 5 to the first unwanted digit and then chopping the remaining.
However in both the cases the actual real number is bound to loose 
some minute value.Therefore the error involved in the reduction of the 
number of digits is called round-off error.
To have a feel of how exactly it(round-off error) happens try to 
excute the following program :
#include <iostream>
#include <iomanip>
#include <cmath>
#include <windows.h>
using namespace std;
float roundingReal32(float testOutput, int precision)
{
     float j = 2.0;
     float epsilon = 0.0;
     float diff = 0.0, f = 0.0;
     f = testOutput;
     int i = (int) f;
     cout<<"\t 2 Testoutput : "  << fixed <<  testOutput << endl;
     int count = precision  ;
	while(count >= 0)
	{
          diff = f - i;	
          //cout << "\t diff : " << fixed << diff ;
	  f = diff*10.0 ;
          //cout << "\t f : " << fixed << f ;
          i = (int)f;  
	  //cout << "\t i : " << i << endl ;
	  count-- ;
	 }
   if(i==5)
    {
	  float aaa = pow( (float)10,(int)(precision+1) );
	  epsilon    =  j /aaa;
	  testOutput =  testOutput + epsilon;
   	  //cout<< "\n \t Epsilon :" << fixed << epsilon << endl;
    }
	 return testOutput;
}
int main ()
{
	//Sleep(15000);
	float testOutputX =0.0;
	int precisionX = 2;
	int counter = 0;
	srand((unsigned) time(NULL));
    //while(counter < 50)
    //{
	cout << " \n \n \t Enter A float Value : "  ;
	cin.width(12);
	cin >> testOutputX;
	cout << " \n \t Enter the precision : " ;
	cin >> precisionX ;
	float testOutput = roundingReal32(testOutputX, precisionX);
	cout<< "\n \t Testoutput : " << setprecision(precisionX) << 
fixed << testOutput << endl;
	cout<< "\t 3 Testoutput : "  <<  testOutput << "\n" << endl;
		counter++ ;
     //}
	return 0;
}
Note: Try to input "491545.625" first and then 5491545.625 with   
precision '2'.