15393

Why is 65.12 - 2 not equal to 63.12 in java? [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-results-in-java" rel="nofollow">Floating point arithmetic not producing exact results in Java</a>

</blockquote>

I was recently working on a project when I came across a strange bug.

When 2 was subtracted from 65.12 the value was greater (not equal to) 63.12.

Here's the simplified code: System.out.println(65.12-2);

And the output in the console: 63.120000000000005

I'm not sure why this is the case and if anyone knows a simple fix/workaround that would be great!

Thanks.

Answer1:

It has to do with the way floating-point values are handled by computers. The recommended text for fully understanding the topic is: <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>

Recommend

  • Centering a Numpy array of images
  • Python datatime.time and arithmetic operations [duplicate]
  • Creating complex clipping path for image?
  • SQLite subtract time difference between two tables if there is a match
  • C# 6 bool and new null check with ? what to return back
  • How to modify a pandas DataFrame in a function so that changes are seen by the caller?
  • Can't fail SVN pre-commit script with Windows server
  • Excel VBA code to move worksheets with image add screen updating and it errors
  • How do I add conditionally to a selection of cells in a pandas dataframe column when the the column
  • When try to execute a cgi script, get the error: “[WinError 193] %1 is not a valid Win32 application
  • Overflow: hidden but i still want to have the empty scrollbar
  • dropzone.js form submit not sending file
  • How to parse Java properties which contains variables?
  • iPhone - UIWebView multiple locks
  • mysql select inside limit
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • reduce/reduce conflicts using ocamlyacc
  • How to write string.Contains(someText) in expression Tree
  • How to get the index of element in the List in c#
  • Rails AREL .where statement
  • How to create a new Bundle object?
  • How to get the date of next specified day of week
  • Python cosine function precision [duplicate]
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • one Local Olampyad Questions on Informatic in 2011
  • ActiveRecord query for a count of new users by day
  • Jetty Server not starting: Unable to establish loopback connection
  • RectangularRangeIndicator format like triangular using dojo
  • sending/ receiving email in Java
  • When should I choose bucket sort over other sorting algorithms?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal