The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error in Velmurugan's notes on floating point precision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Error in Velmurugan Watch "Error in Velmurugan New topic

Error in Velmurugan's notes on floating point precision

natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
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).

Sun Certified Programmer for Java 2 Platform
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
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

------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="" TARGET=_blank rel="nofollow"></A><BR>------------------------------------------------------------------------------------
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
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 )
I agree. Here's the link:
subject: Error in Velmurugan's notes on floating point precision
It's not a secret anymore!