C++ provides stream manipulators that set properties of a stream for specific 
data-types. In this case you would use something like:

 float floatVar   = 2.0433;
 float floatVar2 = 0.0;
 cerr << " floatVar = "   << setprecision(3) << floatVar << endl ; 
 cin >> setprecision(3)   >> floatVar2 ; // set precision for float input
 cerr << " floatVar2 = " << floatVar2 << endl;


Best Regards,
Jim Smith
--- On Sun, 5/24/09, Jos Timanta Tarigan <[email protected]> wrote:

From: Jos Timanta Tarigan <[email protected]>
Subject: [c-prog] limiting precision on float
To: [email protected]
Date: Sunday, May 24, 2009, 4:53 PM











    
            
            


      
      hi,

im currently having a problem try to calculate this:

directionZ = -cos((angleX+ 90)*PI_RADIAN) ;

most of the time it works but when im in the boundary of the coordinate, it 
gives me a little error that im not expecting if calculated correctly. i notice 
that when my angleX is 90, and my directionZ should be 0. but the problem that 
it is not since the precision given when multiplied by PI_RADIAN is not exactly 
0. i track it and it seem to be a floating point precision error. instead of 
having exactly 0, i got 0.000xxx. 

any advice to avoid this error? how can i limit eg: remove everything from 
third decimal?

first thing come up to my mind is to multiply by 1000, convert to int, divide 
by 1000, but thats too long imo.

thanks in advance :)

============ ========= ========= === 

http://www.svnstrk. blogspot. com  

============ ========= ========= ===



[Non-text portions of this message have been removed]




 

      

    
    
        
         
        
        








        


        
        


      

[Non-text portions of this message have been removed]

Reply via email to