File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in Velmurugan's notes on floating point precision

 
natarajan meghanathan
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Velmurugan,
You have done a great work by collecting all the information required for the exam. I am taking the exam on Feb 9th.
I would like to point out an error in your notes:
-----------------------------------------------------
Floating point arithmetic always loses precision. The following code fragment returns false.
float f = 1.0 F / 3.0 F; if ( A * 3.0 F == 1.0 F ) return true; else return false;
-----------------------------------------------------
I assume A in the above expression is a typo. it is f.
If that is the case, then as such the above expression will return true, because even though u r making f to get a less precisioned value when u multiply by 3.0F u get back the same 1.0F u used to obtian f.
If it was just 3.0 then i agree with u that it is false (since LHS is a double and the RHS when promoted to double will not match it).
thanks
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing it out. I agree with you, I noticed this sometime back, but forgot to change the notes.
Best wishes.

------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Cherry Mathew
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
itz not only floating point arithmetic even integer airthmetic can lose precission. But is it that floating point arithmetic always loses precission.
Tell me what happens here
float f = 1.0F / 3.0F;
if ( A * 3.0F == 1.0F )
and here
float f = 1.0F / 3.0F;
if ( A * 3.0 == 1.0F )
Cherry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic